You need to be cautious when working with page-related references. \pageref*
will not return the last page number necessarily. Moreover, a check with \pageref
might not be expandable, and therefore be useless within the comparison with \ifthenelse
.
The following implements an automated \label
with a checking mechanism using refcount
's expandable \getpagerefnumber
:


\documentclass{article}
\usepackage{hyperref,lastpage,refcount}
\usepackage{lipsum}% Just for this example
\newcounter{lastpagecheck}% Counter associated with each \checkiflastpage call
\newcommand{\checkiflastpage}{%
\stepcounter{lastpagecheck}% Increment lastpagecheck counter
\label{lpc-\thelastpagecheck}% Mark unique \label
% Perform expandable check if you're on the LastPage
\ifnum\getpagerefnumber{lpc-\thelastpagecheck}=\getpagerefnumber{LastPage}
Last page%
\else
Not last page (this page = \pageref{lpc-\thelastpagecheck}; last page = \pageref{LastPage})%
\fi
}
\begin{document}
\sloppy% Just for this example
\checkiflastpage
\lipsum[1-20]
\checkiflastpage
\end{document}
The principle behind this approach, as already mentioned, is to mark the location where the check is performed with a unique \label
(called lpc-\thelastpagecheck
, with the aid of a unique counter lastpagecheck
that is incremented with every call to \checkiflastpage
).
Since the \label
-\ref
system is used with this implementation, you'll have to compile at least twice on the first go to make sure the references settle.
You may have to consider using atveryend
to get a proper gauge on the last page in your document, but this really depends on the document construction.