1

A question asked here How to get second last page as a number without hyperref? had an answer, which was accepted and must have worked at the time:

\documentclass[a5paper]{article}
\usepackage{lastpage}
\usepackage{hyperref}
\usepackage{refcount}
\usepackage{lipsum}

\newcommand*{\thesecondlastpage}{%
  \the\numexpr(\getrefbykeydefault{LastPage}{page}{0})-1\relax
}
\AtBeginDocument{\refused{LastPage}}

\begin{document}
The second last page is \thesecondlastpage.

\lipsum
\end{document}

However, it no longer works - after the first typeset, the first line of the PDF is "The second last page is -1." But after the second typeset, it has an error "Missing number, treated as zero. \begingroup

I assume this is either because of the update to the LastPage package on 2023-07-25, or the update to the hyperref package on 2023-07-09.

Can anyone suggest what changes might be needed to this MWE to get it functional again?

2
  • The error is due to a change in lastpage. If hyperref is used it writes out the lastpage label into the aux-file in a way that refcount can't use. Ask the author why this is done so. Commented Oct 4, 2023 at 7:14
  • Short answer: Update to version 2023-10-06 v2.0d of the lastpage package. Long answer: see answer below.
    – Stephen
    Commented Oct 7, 2023 at 20:04

3 Answers 3

1

The issue only arises if using version 2023-07-24 v2.0c of the lastpage package. Update to version 2023-10-06 v2.0d of the lastpage package and everything should just work.


In addition to the answers of David Carlisle (just use that for a nice, simple document with plain arabic numbers) and Ulrike Fischer:

If you are using different page numbering schemes and some "fancy" ones (instead of plain arabic numbers 1, 2, 3,...), you can use:

\documentclass[a5paper]{article}
\pagenumbering{Roman}% Roman page numbering, for example
\usepackage{lastpage}
\usepackage{hyperref}
\usepackage{refcount}
\usepackage{lipsum}

\makeatletter
\def\thesecondlastpage{??}

\AddToHook{cmd/lastpage@putl@bel/after}{%
    \addtocounter{page}{-2}%
      \protected@iwrite\@auxout{}{\string\gdef\string\thesecondlastpage{%
        \string\begingroup\string\pdfstringdefDisableCommands{%
        \string\let\string\TextOrMath\string\@firstoftwo}%
        \thepage\string\endgroup}}%
      \addtocounter{page}{+2}%
}

\makeatother

\begin{document}
The second last page is \thesecondlastpage.

\lipsum

\pagenumbering{fnsymbol}% foot-note-symbol-page-numbering, for example
\lipsum
\end{document}

This only works with recent TeX format and recent lastpage. I am too tired to also include tests for recentness.

If you use different page numbering schemes for the last page and the second last page, then just put a label before that \pagebreak\pagenumbering{ something for lastpage } and refer to that label (or ask a new question here, of course).

1
  • Yes, the recent update to the lastpage package restores this functionality
    – James
    Commented Oct 10, 2023 at 16:56
4

It takes two runs but gives 2 on the second run:

\documentclass[a5paper]{article}

\usepackage{lipsum}

\begin{document}

The second last page is \the\numexpr\PreviousTotalPages -1\relax

\lipsum
\end{document}

2

The error is due to a change in lastpage. It writes out the lastpage label like this into the aux-file if hyperref is used:

\newlabel{LastPage}{{}{\begingroup\pdfstringdefDisableCommands{\let\TextOrMath\@firstoftwo}1\endgroup}{}{page.1}{}}

refcount has no chance to extract the number from this. As an alternative you can use zref:

\documentclass[a5paper]{article}
\usepackage{zref-lastpage}
\usepackage{hyperref}
\usepackage{lipsum}

\makeatletter
\newcommand*{\thesecondlastpage}{%
  \inteval{\zref@extractdefault{LastPage}{page}{0}-1}}
\AtBeginDocument{\zref@refused{LastPage}}
\makeatother

\begin{document}
The second last page is \thesecondlastpage.

\lipsum
\end{document}

Be aware that this (like your old version) assumes that the page number is an arabic number. If you switch e.g. to roman it will error too, for such a case you would have to define a new property.

1
  • Yes, has been fixed in version 2023-10-06 v2.0d of the lastpage package.
    – Stephen
    Commented Oct 7, 2023 at 20:03

You must log in to answer this question.

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