[Tex/LaTex] Working alternatives to totcount to get the total number of pages of a document

counterspackagestotcount

I have encountered a bug in totcount package: sometimes it counts pages incorrectly. For example, the following code produces for me a 2-page PDF file, but totcount thinks there is only one page:

\documentclass[letterpaper]{article}

\usepackage{lipsum}

\usepackage{totcount}
\regtotcounter{page}

\begin{document}

\lipsum[1-5]

{\LARGE Total page count according to \texttt{totcount}: \total{page}.}

\end{document}

Is anybody still maintaining this package?

What are working alternatives to totcount?

I know about lastpage but I need the total number in numerical form, not as text. With totcount I use \totvalue{page} for this.

Best Answer

You can use zref that shouldn't have problems like totcount with respect to the page number. I mean, totcount is great for saving the value of counters, but it can fail with page.

\documentclass[letterpaper]{article}

\usepackage[lastpage]{zref}

\usepackage{lipsum} % for the example

\makeatletter
\newcommand{\totalpages}{\zref@extractdefault{LastPage}{page}{0}}
\makeatother

\newcounter{test}

\begin{document}

\lipsum[1-5]

\bigskip

\LARGE

\setcounter{test}{\totalpages}\thetest

Total page count according to \texttt{zref}: \totalpages.

\end{document}

As you see, you can use \totalpages also in context where a number is expected.

enter image description here

Related Question