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?
lastpage
uses the new hook management and therefore is the better choice compared topageslts
, because the last one has not yet been updated.lastpage
still doesn't deal with different numbering schemes. It both recommendspageslts
as a solution and says it is broken in some cases. It also doesn't work withcref
's page referencing mechanism .... My hack works better, though I'm sure you're right thatlastpage
is technically superior.