4

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.

1
  • 1
    Load package lastpage as well and you get a total of 3 pages. :-)
    – Johannes_B
    Oct 22, 2016 at 12:40

3 Answers 3

6

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

2
  • Could you give some explanation, please, why the same command sequence can be used here both as text and as a number? For example, \totalvalue{pages} cannot be used this way.
    – Alexey
    Oct 22, 2016 at 20:40
  • @Alexey The package zref provides some interesting features that make possible to use reference numbers in expansion contexts like setting a counter. Of course, getting the correct reference may require one more LaTeX run, if the number of pages has changed.
    – egreg
    Oct 22, 2016 at 21:07
3

lastpage returns 2 as you want here

\documentclass[letterpaper]{article}

\usepackage{lipsum}

\usepackage{lastpage}


\begin{document}

\lipsum[1-5]

{\LARGE Total page count according to \texttt{lastpage}: \pageref{LastPage}.}

\end{document}

You can use the value of \pageref{LastPage} as a number if you are careful.

\documentclass[letterpaper]{article}

\makeatletter
\long\def\@secondoffour#1#2#3#4{#2}
\def\getlastpage{\ifx\r@LastPage\@undefined 0\else
\expandafter\@secondoffour\r@LastPage\@empty\@empty\fi}
\makeatother

\usepackage{lipsum}

\usepackage{lastpage}


\begin{document}

\lipsum[1-5]

{\LARGE Total page count according to \texttt{lastpage}: \pageref{LastPage}.}

\the\numexpr\getlastpage+5\relax

\end{document}
2
  • Can you do a numeric coparison based on the pageref value?
    – Johannes_B
    Oct 22, 2016 at 12:42
  • @Johannes_B I can, yes (as long as last page isn't numbered in roman) also it is the printed number of the last page, not the number of pages. Oct 22, 2016 at 12:49
1

You can do this "by hand" using the \AtEndDocument command from the etoolbox package. The code below increments the equation counter at the end of the document and then creates LastPage label. With this in place \pageref{LastPage} will give the number of pages in the document (of course, you need to latex twice and the number of pages needs to be stable.

enter image description here

Here is the top few lines of the output from the MWE:

Here is the code:

\documentclass{article}
\usepackage{etoolbox}% setting the LastPage label
\AtEndDocument{\refstepcounter{equation}\label{LastPage}}

\usepackage{blindtext}% for illustrative purposes only below

\begin{document}

The number of pages in this document is \pageref{LastPage}.

\Blinddocument% a fake random document

\end{document}
3
  • It's not that easy, add \newpage or \clearpage right before \end{document}. Then there isn't a page left for the \label to go and the reference remains undefined. Usually the label at the end of the document is written \immediate, see the implementation of packages lastpage or zref-lastpage. The latter uses the hook \AfterLastShipout of package atveryend to place the label after the last page. Oct 22, 2016 at 13:00
  • @HeikoOberdiek I think that these corner case scenarios are user errors:) I essentially use the code above for all of my tutorials and there are never any problems.
    – user30471
    Oct 23, 2016 at 0:54
  • No, these are not user errors nor corner cases. The last page can be shipped out before the label for many reasons, e.g., the last page just has ended and was output, an explicit or implicit \newpage, \clearpage et cetetera. Dealing with the implications of the asynchronous output routine is tricky. When LaTeX reaches \end{document}, both cases are perfectly normal and no user error, that the last page is output and is not yet output. Oct 23, 2016 at 1:16

You must log in to answer this question.

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