[Tex/LaTex] Referring to Second Last Page in pageref in lastpage package

cross-referencinghyperreflastpage

I can use \pageref{LastPage} to give the reference of last page with lastpage package. The last page of document is the back cover so I don't want to count it. I wonder how to specify the second last page with lastpage package. Thanks in advance for your help.

Best Answer

Use refcount to manipulate (page) reference:

enter image description here

\documentclass{article}
\usepackage{lastpage,refcount,atbegshi}
\usepackage{hyperref}
\usepackage{lipsum}
\AtBeginShipout{%
  \ifnum\value{page}=\number\numexpr\getpagerefnumber{LastPage}-2\relax
    \phantomsection\label{preLastPage}
  \fi}
\begin{document}

Last page: \pageref{LastPage} \par
Second to last page: \pageref{preLastPage}

\lipsum[1-50]

\end{document}

The reason for comparing \value{page} with <LastPage>-2 is because of the fact that page numbers are stepped after printing the value - different from other counters in a document. So, \AtBeginShipout, the page counter has already been stepped and we need to insert a \label at the end of page <LastPage>-2 in order for it to be set at the start of page <LastPage>-1.