5

Suppose I have a counter, say, count. I can do something like this

\setcounter{count}{0}
\refstepcounter{count}\thecounter{count}\label{cool}
\ref{cool}

This will give me "1" in the end. Now, I would like to be able to use nameref in the following way (which doesn't quite work, and which is the reason why I ask this question)

\setcounter{count}{0}
\refstepcounter{count}\thecounter{count}
\@currentlabelname{awesome:\thecount}}\phantomsection\label{cool}
\stepcounter{count}
\nameref{cool}

I would like the result to be awesome:1, but it will be 2 in this case, because when I do nameref, \thecount gives value 2. The problem is that \thecount doesn't save the value at the point it's called. How do I fix this?

As a side note, something like \tag (for equation) might work. For example, if I have \tag{awesome:\thecount}\label{cool} and have \eqref{cool} later on, I actually get the correct result.

Werner
  • 603,163
brian
  • 1,069

1 Answers1

5

\thecount needs to be expanded for \@currentlabelname. Otherwise the reference contains \thecount and the current number is printed in the references.

\edef\@currentlabelname{awesome: \thecount}

or

\protected@edef\@currentlabelname{awesome: \thecount}

LaTeX's usual protection mechanisms (such as \protect) will work, if \protected@edef is used.

Heiko Oberdiek
  • 271,626
  • This is indeed awesome. I'm using the one without protected ('cause I don't really know how protection mechanism work, but I will do this homework in the near future). Thank you! – brian Apr 10 '14 at 22:41
  • Thanks for \protected@edef, never knew about that! Mysterious things within hyperref that needed protection were throwing errors on all changes of \@currentlabelname for me. – scorchgeek Aug 21 '22 at 02:12