6

I am/was using pageslts, which I know is not supposed to be compatible with current LaTeX. As far as I understand, there is no alternative offering the same functionality, but I'd like to at least find a way to get a reference to the last page of the document.

I need this to work whether or not hyperref is loaded and whether fancyref or cleveref is used.

I've been playing with putting a label into either enddocument or shipout/lastpage. Of the two, shipout/lastpage seems to work a little better, but I'm not at all convinced by my approach.

There are also \g_shipout_readonly_int/\ReadOnlyShipoutCounter and \g_shipout_totalpages_int/totalpages but those don't, as far as I can tell get me a hyperlink target if hyperref is loaded.

The documentation of the new shipout routine and hooks explains how to replace various packages, but I can't find anything specifically about lastpage or pageslts.

I'm not sure I'm using the correct hook. I'm not sure I should be using a hook at all. I'm really not sure I should be using \phantomsection. I'm not at all sure using pageslts isn't better, even if it isn't really compatible. (For basic usage, it still seems to work and then it figures out where to put its labels.)

\documentclass{book}
\usepackage{kantlipsum}
\AddToHook{begindocument}{\ProvideDocumentCommand \phantomsection {}{}}
\usepackage{bookmark}
\usepackage{cleveref}
\AddToHook {enddocument} {\phantomsection\label{enddocument:lastpage}}
\AddToHook {shipout/lastpage} {\phantomsection\label{shipout-lastpage:lastpage}}
\begin{document}
\frontmatter
The last page of this document is \pageref{enddocument:lastpage}.

The last page of this document is \pageref{shipout-lastpage:lastpage}.

The last page of this document is \cpageref{endocument:lastpage,shipout-lastpage:lastpage}.

\mainmatter
\kant[1-10]

\backmatter
The last page of this document is \thepage.


\end{document}

What's the recommended way to do this now?

2
  • 2
    AFAIK from version 2 lastpage uses the new hook management and therefore is the better choice compared to pageslts, because the last one has not yet been updated.
    – cabohah
    Commented Apr 8 at 15:42
  • Thanks @cabohah Unfortunately, lastpage still doesn't deal with different numbering schemes. It both recommends pageslts as a solution and says it is broken in some cases. It also doesn't work with cref's page referencing mechanism .... My hack works better, though I'm sure you're right that lastpage is technically superior.
    – cfr
    Commented Apr 8 at 20:04

1 Answer 1

9

There is not really a need to set an anchor as hyperref adds anchors to all pages anyway:

\documentclass{book}
\usepackage{kantlipsum}
\usepackage{bookmark}
\usepackage{cleveref}
\makeatletter
\AddToHook {shipout/lastpage} {\gdef\@currentHref{\@currentHpage}\label{shipout-lastpage:lastpage}}
\makeatother
\begin{document}
\frontmatter

The last page of this document is \pageref{shipout-lastpage:lastpage}.

The last page of this document is \cpageref{shipout-lastpage:lastpage}.

\mainmatter
\kant[1-10]

\backmatter
The last page of this document is \thepage.
\end{document}

This interface requires a current LaTeX and hyperref version. Loading hyperref is not required, if it is not present \@currentHpage is simply empty. There will naturally be no anchor if you load hyperref with pageanchor=false, in this case you should add one manually, e.g. with \MakeLinkTarget, see hyperref-linktarget.pdf.

You can also label and reference the pageanchor with a property, naturally this requires new reference commands:

\documentclass{book}
\usepackage{kantlipsum}
\usepackage{bookmark}
\AddToHook {shipout/lastpage} {\RecordProperties{shipout-lastpage:lastpage}{page,pagetarget}}

\begin{document}
\frontmatter

The last page of this document is \hyperlink{\RefProperty{shipout-lastpage:lastpage}{pagetarget}}{\RefProperty{shipout-lastpage:lastpage}{page}}.


\mainmatter
\kant[1-10]

\backmatter
The last page of this document is \thepage.
\end{document}
5
  • Thanks! I knew hyperref added anchors to every page, but I couldn't figure out how to access one and I wasn't sure whether I was meant not to.
    – cfr
    Commented Apr 8 at 23:53
  • This even seems to work after I've mangled it, which is surprising because little else does. (Well, it does now, but it didn't on Sunday. Apparently trying to count beyond 2 was a bit ambitious.)
    – cfr
    Commented Apr 9 at 2:36
  • 2
    The hyperref interface is quite new. I added it when we wrote the properties module (which predefines also the pageanchor property). Commented Apr 9 at 6:07
  • Since I wrote that comment I've broken everything and I only meant to tidy up. I know I don't understand hooks. (It's not the hyperref stuff I'm breaking - or it is, partly, but only indirectly.) I'm sure rewriting \hypersetup temporarily has to be the stupidest thing I've done to get something to (finally) compile.
    – cfr
    Commented Apr 9 at 6:54
  • @cfr yes redefining \hypersetup is not really I good idea ;-). I added a few details about the interface. Commented Apr 9 at 10:43

You must log in to answer this question.

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