# zlabel not recorded in aux file

I want to use zref to record page numbers, but with this code structure, labels are not recorded in the aux file.

And I have no idea what I have missed.

MWE:

\documentclass{article}

\usepackage{multicol}
\usepackage{zref-user}

\AtEndDocument{\zlabel{label1}}

\begin{document}

\begin{multicols*}{2}
Some text.
\end{multicols*}

\zlabel{label2}

\end{document}


Aux file content:

\$ cat issue.aux
\relax
\providecommand\zref@newlabel[2]{}


The 2 labels are not recorded. The expected aux content should like:

\relax
\providecommand\zref@newlabel[2]{}
\zref@newlabel{label2}{\default{}\page{2}}
\zref@newlabel{label1}{\default{}\page{2}}

• Apparently, the multicol output routine kicks in. – egreg Dec 12 '17 at 11:27
• labels at the end of document can be problematic. If there is no output they can be lost. If you want a label for the last page use better the module/package zref-lastpage which takes care of this. – Ulrike Fischer Dec 12 '17 at 11:39

The issue can also be reproduce with the standard \label command:

\documentclass{article}

\usepackage{multicol}

\begin{document}

\begin{multicols*}{2}
Some text.
\end{multicols*}

\label{label}

\end{document}


Compilation will leave an .aux file containing just \relax. The label is correctly written if some text follows \end{multicols*} or the command is inside the multicols* environment.

If your aim is to set a label for the last page, use zref-lastpage that does it automatically.

• I want to use a general method to record page number at the end of a content group. In my real project, these content groups are auto-generated. zref-lastpage can only be applied when there is only 1 group. If this is not easy to solve, I guess I may use both \zlabel and zref-lastpage, and let some external logic to resolve correct page numbers. – SongHerz Dec 13 '17 at 2:20

Thanks to @egreg and @UlrikeFischer for point out label issue at end of a document.

When a label is not recorded in an aux file, the label should be at the end of a document, and the page number of that label must be the last page.

Here is my solution:

\documentclass{article}

\usepackage{multicol}
\usepackage{zref-lastpage}% for LastPage

\makeatletter
\newcommand{\labelthispage}[1]{\zref@label{#1}}
\newcommand{\getl@beledpage}[1]{\zref@extractdefault{#1}{page}{-1}}
\newcommand{\getl@stpage}{\zref@extractdefault{LastPage}{page}{0}}
\newcommand{\getlabeledpage}[1]{\ifnum\getl@beledpage{#1}=-1 \getl@stpage \else \getl@beledpage{#1} \fi}
\makeatother

\begin{document}

\begin{multicols*}{2}
Some text.
\labelthispage{LABEL1}
\end{multicols*}

\edef\tmp{################ LABELED PAGE 1: \getlabeledpage{LABEL1}}
\typeout\tmp

\begin{multicols*}{2}
Other text.
\end{multicols*}

\labelthispage{LABEL2}
\edef\tmp{################ LABELED PAGE 2: \getlabeledpage{LABEL2}}
\typeout\tmp

\end{document}


Snippet of the 2nd compilation log:

(./issue_try_2.aux) [1]
################ LABELED PAGE 1: 1
[2]
################ LABELED PAGE 2: 2
(./issue_try_2.aux) )


The aux file:

\relax
\providecommand\zref@newlabel[2]{}
\zref@newlabel{LABEL1}{\default{}\page{1}\abspage{1}}
\zref@newlabel{LastPage}{\default{}\page{2}\abspage{2}}


LABEL2 not recorded in the aux file, and LastPage value is used by \getlabeledpage{LABEL2}