4

Context: I want to store values in the aux file to use them earlier in the file on the next run, rather than creating my own solution I want to use labels (as suggest by this answer).

Problem: Whenever a label is defined it creates something like this

\newlabel{customLabel}{{MyValue}{24}{}{Doc-Start}{}}

I want to be able to extract those values. \ref does not work as its output is not just the value but some complex latex string.

  • 1
    \getrefnumber{customLabel} from package refcount -- it is expandable, \ref isn't. – user31729 Mar 8 '16 at 14:48
  • That worked for my current problem, is there any reason why you will not post this as an answer? – Strategy Thinker Mar 8 '16 at 14:55
  • I was writing an answer ;-) – user31729 Mar 8 '16 at 14:58
  • I've added an answer, with another way of writing the label -- this is not shown by you, so I assumed some approach – user31729 Mar 8 '16 at 15:10
6

With refcount and \getrefnumber it is possible to extract label values in an expandable way.

\getrefnumber{foo} will provide whatever is stored as label value, this might be a number but it is not restricted to numbers. (It can be 'anything')

The label value must be stored as second argument of \newlabel.

\documentclass{article}

\usepackage{refcount}
\usepackage{hyperref}

\newcounter{mylabelcounter}

\makeatletter
\newcommand{\labelText}[2]{%
  \refstepcounter{mylabelcounter}%
  \immediate\write\@auxout{%
    \string\newlabel{#1}{{#2}{\thepage}{{\unexpanded{#2}}}{mylabelcounter.\number\value{mylabelcounter}}{}}
  }%
}
\makeatother

\begin{document}

\labelText{customLabel}{100}

\labelText{otherLabel}{24}

\ifnum\getrefnumber{otherLabel} < 25 
Hooray!!!
\else
Sorry!!!!
\fi


\ifnum\getrefnumber{customLabel} < 25 
Hooray!!!
\else
Sorry!!!!
\fi

\end{document}

enter image description here

  • I used \def\@currentlabel{#2}\label{#1} to save the label but the net result is the same. – Strategy Thinker Mar 8 '16 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.