6

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.

Strategy Thinker
  • 690
  • 5
  • 10

1 Answers1

7

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