2

I am trying to write a code for figuring if the current page is the last page. I do run into some problems, though. In my example here, the code will not run if hyperrefis loaded. Without it, it produces the result I'm after. Also, in my real document, the code actually works with hyperref, although I do not know why. But it does write an extra "page " for some reason. I am not however asking for an explanation of this, as the document is quite complicated at the moment.

\documentclass[11pt]{article}
\usepackage{lastpage, xifthen,hyperref}
\begin{document}
\ifthenelse{1=\pageref*{LastPage}}{Not last page}{Last page}
\end{document}
2

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:

enter image description here

enter image description here

\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.

  • I saw your response in another thread, and included a working example based on it in my answer. This answer might be better, but I don't know, really. I would like to thank you for two great answers to this question, and I should give you the credit instead of providing an answer myself based on your answer to another question. However, is there anything wrong with doing it that way? I don't really need to know if it actually is the last page, just to know if the document has more than one page. – Runar Nov 10 '15 at 8:11
0

I got this working with the following code, which is based on Werners response in this thread

 \documentclass[11pt]{article}
    \usepackage{lastpage, xifthen,hyperref}
    \begin{document}
    \ifthenelse{\number\numexpr\getpagerefnumber{LastPage}=1}{Not last page}{Last page}
    \end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.