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
\ref{label}
to refer to this counter's value..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)\pageref
isn't expandable, that's the reason