[texhax] constructing labels from counters

Moshe Kamensky samvimes at fastmail.fm
Mon Aug 7 11:26:59 CEST 2006

* Uwe Lück <uwe.lueck at web.de> [29/07/06 09:00]:
> At 10:02 27.07.06, Moshe Kamensky wrote:
> >I would like to construct a label for \label and \ref from a counter, so
> >that I can do something like
> >
> >\newcommand{\foo}{\stepcounter{foo}\label{bar:\value{foo}}...}
> >
> >But the above example using \value doesn't work, nor when I replace it
> >by, say, \alph{foo}. Is there a way to achieve this?
> /Short answer:/ Try
>     \newcommand{\foo}{%
>       \stepcounter{foo}%
>       \edef\thelabel{\noexpand\label{bar:\thefoo}}%
>       \thelabel}
> Here, \thefoo is the ordinary LaTeX way; however,
> (i) \edef is not LaTeX, (ii) \thefoo may break with certain
> complex (re-)definitions of \thefoo; in the latter case
> replacing \edef by \protected at edef -- inside \makeatletter ...
> \makeatother -- (and \noexpand by \protect) may help.
> /Expanded answer:/
> I feel I am the expert who has been asked since my ednotes bundle
> does this at at least two points. The problem may be difficult to
> understand from a mere LaTeX point of view, this understanding
> may require knowing something about the underlying program TeX.
> (The ednotes package originated when Christian Tapp, then knowing
> about LaTeX only, asked me similar questions. To learn what TeX
> does beyound LaTeX, I can't refer to something better than
> D. Knuth's The TeXbook, while there are alternatives.)
> You want that your \label command uses the value of the
> `foo' counter that your \stepcounter brought about.
> One problem of your approach is that, probably counter to your
> expectation, \value{foo} does (in general) /not/ produce the
> value of the `foo' counter. I consider this a didactical shortcoming
> of LaTeX. I suppose that LaTeX's \value can't be understood
> without knowing how it communicates with the TeX program.
> (You probably get the `\endcsname inserted' error with mere \value.)
> Rather, \thefoo produces the value. However, \thefoo may
> -- due to some strange (re-)definitions, produce things that
> don't concern the /value/, rather the design of how to print it.
> (This may as well result in the `\endcsname inserted' error.)
> To get the value only indeed, \the\value{foo} is more robust.
> (\the is not LaTeX, but primitive TeX.)
> Moreover, you need that your \label writes something
> into the .aux file that uses the value of the `foo' counter
> that your \stepcounter brought about. However, the \label
> command first only stores a part of its input and writes
> something to the .aux file only when -- in general --
> your `foo' counter has got a larger value than the one
> originating from your original \foo command.
> \edef or \protected at edef ensures that the /current/ value
> of the `foo' counter will be used in writing indeed.
> Sometimes the TeX primitive \expandafter is more
> reliable than \edef or \protected at edef -- instead of
> expanding entirely, it just expands some token /for
> one step/ before a preceding token is processed.

Thank you for the explanation. It works in the simple setup I wrote, but 
not in the more complicated situation that I actually have. I was trying 
to produce an errata for my thesis. So I would like to have an 
environment that I can put around a change I made, with an argument that 
describes the change, and I want it to produce an entry in the errata 
page at the end with the text of this argument, and a ref back to the 
page where the change occurred. The way I implemented it is via the 
endnotes package. To give a reference back to the page I was trying to 
use this counter. This works for setting the label, but when I use 
\pageref{bar:\thefoo}, this expression is not expanded, but is written 
verbatim into the .ent file. Instead of all that, I currently just 
supply a label name as an extra argument to the environment. This works, 
but is slightly stupid, since I don't actually use the label anywhere 
else. Anyway, thanks for the help.


> Hope this helps,
> Uwe.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://tug.org/pipermail/texhax/attachments/20060807/3cb5b247/attachment.bin 

More information about the texhax mailing list