# How to obtain label pageref from AUX file?

I would like to read the pageref info for labels directly from the Aux file. It seems that for every label <label> there is a macro \r@<label> that contains all the information and is accessible, for instance, to be used in \typeout. However, I'm interested only in the page number, so I would use \pageref{<label>}. But this doesn't work in \typeout. This is an MWE of my scenario where labels are generated using the value of a counter (updated):

\documentclass{article}
\usepackage{lipsum,multicol,hyperref}

\makeatletter

\newcounter{mycounter}

\newcommand{\mycommand}{%
\stepcounter{mycounter}
\label{myitems@\themycounter}
\typeout{Processing Item No \themycounter}
\typeout{Label Info From Aux File Is
\csname r@myitems@\themycounter\endcsname}
\typeout{Pageref From Aux File Is
\pageref{myitems@\themycounter}}
}

\makeatother

\begin{document}

\begin{multicols}{2}[\section{Section}]
\mycommand
\lipsum[1-10]
\mycommand
\lipsum[1-5]
\mycommand
\end{multicols}

\end{document}


This produces the output

ProcessingItemNo1
LabelInfoFromAuxFileIs{1}{1}{Section}{section.1}{}
PagerefFromAuxFileIs\pageref {myitems@1}
[1]
ProcessingItemNo2
LabelInfoFromAuxFileIs{1}{3}{Section}{section.1}{}
PagerefFromAuxFileIs\pageref {myitems@2}
[2]
ProcessingItemNo3
LabelInfoFromAuxFileIs{1}{4}{Section}{section.1}{}
PagerefFromAuxFileIs\pageref {myitems@3}
[3] [4]


So it seems that the page information is already there at this point in the code (second token in \r@<labelname>) but \pageref cannot produce it.

My question is: How can I produce the page number at this point in the code (for use in \typeout or in other macro stuff)? Basically, I need a variable \tmpa that is fully expanded and just stores the page number for that label as restored from the AUX file. Is that possible?

Edit: After solving the problem I found that my MWE was not "minimal" enough to isolate the actual problem. So here is a simpler MWE showing what I was actually wondering about.

\documentclass{article}
\usepackage{lipsum,multicol,hyperref}

\begin{document}

\makeatletter
% this shows that all the info on mylabel (including page number) is available:
\typeout{Label Info From Aux File Is \r@mylabel}
% but this shows that nevertheless, \pageref does not work:
\typeout{Last time, mylabel was on page \pageref{mylabel}}
% this is, then, the solution to my problem, see my answer below:
\typeout{Last time, mylabel was on page \getpagerefnumber{mylabel}}
\makeatother

\begin{multicols}{2}[\section{Section}]
\lipsum[1-10]
\label{mylabel}
\lipsum[1-5]
\end{multicols}
\end{document}


So, I was just wondering why \pageref doesn't work in a situation where \r@mylabel is available and stores all relevant information from the AUX file.

• \refstepcounter is the macro that you need for \label, not just \stepcounter – user31729 Mar 21 '18 at 19:20
• I don't think so as the counter is only used internally to generate the name of a new label. I don't want any \ref{label} to refer to this counter's value. – LaTechneuse Mar 22 '18 at 12:07
• Oh no, your answer was helpful! And why does my labeling only work by chance?? – LaTechneuse Mar 22 '18 at 12:14
• Because the page number is always stored expanded to the .aux file, regardless whether you need the real cross-reference made by \refstepcounter or just using an ordinary incrementation. Since you rely on the page number it is just by chance that this is done anyway. (One could say: it works by a lucky 'design' decision' of the LaTeX developers) – user31729 Mar 22 '18 at 12:22
• \pageref isn't expandable, that's the reason – user31729 Mar 22 '18 at 12:33

The page label reference value can be extracted with \crtrefpage from the crossreftoolspackage as well, which is an expandable command. There's no need to use expl3 here.

\documentclass{article}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage{hyperref}

\usepackage{crossreftools}

\makeatletter

\newcounter{mycounter}

\newcommand{\mycommand}{%
\refstepcounter{mycounter}%
\label{myitems@\themycounter}
\typeout{Processing Item No \themycounter}
\typeout{Label Info From Aux File Is
\csname r@myitems@\themycounter\endcsname}
\typeout{Pageref From Aux File Is \crtrefpage{myitems@\themycounter}}
}

\makeatother

\begin{document}

\begin{multicols}{2}[\section{Section}]
\mycommand
\lipsum[1-10]
\mycommand
\lipsum[1-5]
\mycommand
\end{multicols}

\end{document}

• I removed the latex3 commands from my MEW (it was a relict from a more complicated source). However, I found that just using \getpagerefnumber instead of \pageref solves my problem (see my own answer). Is there any reason to prefer your solution (which involves an extra package after all)? – LaTechneuse Mar 22 '18 at 12:02
• @LaTechneuse: No, there's no advantage, it's just another solution – user31729 Mar 22 '18 at 12:06

All right, I see that there is a simple solution by using \getpagerefnumber{myitems@\themycounter}} instead of \pageref{myitems@\themycounter}}.