Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
(+1) Thanks @Werner for your nice answer. It works like a charm except the hyperlink is broken. Any thoughts. –  MYaseen208 Aug 7 at 19:47
    
@MYaseen208 There can be no link if there is no anchor. –  egreg Aug 7 at 20:10
    
@MYaseen208: I've updated my answer. For future reference, please include a minimal example that shows you're hyperref. –  Werner Aug 8 at 2:16

The pageslts package puts a last page label for each numbering scheme, provided you use a pagenumbering{…} at the beginning of the document, and at every scheme change. So, for your cover page, you can use any scheme not used beforehand, setting \pagestyle{empty} immediately after:

\documentclass{article}
\usepackage{pageslts,refcount}
\usepackage{lipsum}
\usepackage[ plainpages=false, pdfpagelabels=true]{hyperref}

\begin{document}

\pagenumbering{arabic}
Last page: \pageref{pagesLTS.arabic} \par
Very last page:  \lastpageref{LastPages}

\lipsum[1-50]
\clearpage
\pagenumbering{roman}
\pagestyle{empty}
section{New page numbering}
\lipsum[51-60]

\end{document} 

enter image description here Note that the options to hyperrefare set to prevent any problems if two pages have the same number (even in different numbering schemes), according to the documentation (§ 3.1), but aren't really necessary for the example given here. See the details of constraints in the doc.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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