GeneralMacro-generated labels

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Brian_D
Posts: 28
Joined: Thu Apr 23, 2015 12:01 pm

Macro-generated labels

Post by Brian_D »

I would like to dynamically generate a label, and of course \ref and friends only work with plain text:

Code: Select all


\documentclass{article}

\begin{document}

\section{First Section}

Label page:1 is here.\label{page:1}

A line of text.

\section{Second Section}

Label labeltwo is here.\label{labeltwo}

A line of text.

\section{Third Section}

The page of label page:1 is \pageref{page:1}.

The section of label page:1 is \ref{page:1}.

The page of label labeltwo is \pageref{labeltwo}.

The section of label labeltwo is \ref{labeltwo}.

% The following yields: ! Missing \endcsname inserted.
The section of the label page:1 is \ref{page:\pageref{labeltwo}}.

\end{document}

So I'd like to find the page for labeltwo, append that to the string "page:" to generate the new label "page:1", then feed that into a \ref to find the section. The inevitable error occurs: ! Missing \endcsname inserted.

Is there a way to get this to work?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Brian_D
Posts: 28
Joined: Thu Apr 23, 2015 12:01 pm

Re: Macro-generated labels

Post by Brian_D »

I was able to get around this problem by using the zref package to create labels which record both section and page number, used in parallel with the original label which can record the figure or table number as well. The original question may be of some interest, though.
Brian Dunn
BDTechConcepts.com
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Macro-generated labels

Post by Stefan Kottwitz »

Hi Brian,

welcome to the forum, and thanks for telling us your solution!
Brian_D wrote:The original question may be of some interest, though.
Yes, it is! I tried to fix that, with some \expandafter and \csname ... \endcsname, but it did not work yet. The interesting and challenging question is still open.

Stefan
LaTeX.org admin
Post Reply