5

I met this problem when I put \label{LastPage} after \include{chapter}. I found that the \ref{LastPage} turned to ?? when I used latexmk -pdf to compile it. I tested the reason and knew that \label after \clearpage would turn \ref to ??. Here is my code

% label after \clearpage
\documentclass{article}
% \usepackage{lastpage}
\begin{document}
    this is main file. \pageref{LastPage}
    \clearpage
    \label{LastPage}
\end{document}

I got the output image.png

I want to know what led to this issue?

2 Answers 2

8

The last page of your document is effectively the one containing this is main file. \pageref{LastPage} as it is shipped out with a call to \clearpage. Since \label doesn't cause any printable content to be set, there are no other pages to be shipped out. And, since \labels are only "processed" during page shipout (in order to obtain the appropriate values of the page for \pageref - an asynchronous process to the content), your final \label is never processed, leaving \pageref to search for an unknown (page) reference LastPage.

The lastpage package does some work to circumvent a similar setup to yours, allow the following minimal example to work as expected, even though it matches your setup:

\documentclass{article}

\usepackage{lastpage}

\begin{document}
this is main file. \pageref{LastPage}
\clearpage

\end{document}

Other options include the use of atveryend for placing \labels at the end of a document.

6

I think this is probably because that no new page was actually created after the last \clearpage, since there's no content. Adding a \null or \hbox{} resolves this.

\documentclass{article}
% \usepackage{lastpage}
\begin{document}
    this is main file. \pageref{LastPage}
    \clearpage
    \null
    \label{LastPage}
\end{document}
1
  • 1
    But this creates a spurious page.
    – egreg
    Feb 8, 2021 at 18:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .