3

I'm trying to get the second last page as a number but without having to create a hyperlink to it.

What I have now is:

\def\leavesecond#1#2#3!{#2}
\makeatletter
\AtBeginDocument{%
  \expandafter\ifx\csname r@LastPage\endcsname\relax
    \newcommand\SecondLastPage{{\bfseries??}}%
  \else
    \newcounter{butlastpage}%
    \setcounter{butlastpage}{\expandafter\leavesecond\r@LastPage!}%
    \addtocounter{butlastpage}{-1}%
    \newcommand\SecondLastPage{}%
    \edef\SecondLastPage{\arabic{butlastpage}}%
  \fi}

Which works, the problem is that I'm having some compatibility issues with fancy-preview due to a hyperlink created to the last page. Is it possible to define it without creating the hyperlink?

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
user80175
  • 33
  • 5
  • Do you want to have the logical page number or the absolute page number? –  Jun 15 '15 at 15:39

1 Answers1

4

Packages lastpage and refcount

Package refcount helps to get the page number from the reference LastPage:

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

This produces three pages, the first run reports -1 as second last page, because LastPage is not yet available. The final run reports 2, because the document has three pages.

[Caveat: Does not work like this for version 2023-07-24 v2.0c of the lastpage package. Update to version 2023-10-06 v2.0d of the lastpage package and everything works again.]

Package zref-lastpage

Modul zref-lastpage uses the zref referencing system to set a reference LastPage at the last page. This can also be used to get the page number of the last page to calculate the previous one:

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

\makeatletter \newcommand*{\thesecondlastpage}{% \the\numexpr(\zref@extractdefault{LastPage}{page}{0})-1\relax } \AtBeginDocument{\zref@refused{LastPage}} \makeatother

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

\lipsum \end{document}

Package zref-totpages

Modul zref-totpages of project zref provides \ztotpages as absolute number of pages. Depending on the document numbering, it can also be used to get the second last page:

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

\newcommand*{\thesecondlastpage}{% \the\numexpr(\ztotpages)-1\relax }

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

\lipsum \end{document}

Stephen
  • 14,890
Heiko Oberdiek
  • 271,626
  • I'm still having compatibility problems with fancy-preview. In the first (and the second) run to search the labels, it is getting a label \fancypreviewnewlabel{LastPage}{{}{25}{}{page.25}{}} that results in a error. Is there any way to do that without using the package lastpage? – user80175 Jun 15 '15 at 16:02
  • @user80175 I have no idea, what you are doing. Where does \fancypreviewnewlabel comes from? Which packages are you using? MWE? My answer variants provide \thesecondlastpage, which expands to a plain number without any links and can be used safely, even inside \csname. – Heiko Oberdiek Jun 15 '15 at 16:17
  • I'm sorry about my question being incomplete. The problem is that this funtion comes in a user-created class, which would create a huge question to a very specific problem. That's why I tried to put the problem the most general way possible to be solved. Also, the \fancypreviewnewlabelcomes from a script fancy-preview, which is very specific and the question would end up being huge.

    Anyhow, using zref-totpages works ike a charm. Again, sorry for not being more specific in my question

    – user80175 Jun 15 '15 at 16:24