9

My goal is to achieve n of m type page numbering if the document has more than 1 page and no page numbers otherwise. I have no problem putting n of m page numbers thanks to How can I add "page # of ##" on my document?. My plan was to use the totcount package to test the total number of pages. The problem is that the lastpage package screws up the total count. Specifically, the resulting 1 page document of the following MWE:

\documentclass{article}
\usepackage{lastpage}
\usepackage{totcount}
\regtotcounter{page}
\begin{document}
totcount: \total{page}

lastpage: \pageref{LastPage}
\end{document}

has \total{page} equal to 2 while \pageref{LastPage} is equal to 1.

Any ideas what is going on?

5

Perhaps this doesn't answer the question completely, but it provides a work-around (circumventing totcount):

enter image description here

\documentclass{article}
\usepackage[paper=a6paper]{geometry}% http://ctan.org/pkg/geometry
\usepackage{fancyhdr,refcount}% http://ctan.org/pkg/{fancyhdr,refcount}
\usepackage{lastpage}% http://ctan.org/pkg/lastpage
\pagestyle{fancy}
\fancyhf{}% Clear header/footer
\renewcommand{\headrulewidth}{0pt}% No header rule
\fancyfoot[C]{%
  \ifnum\getpagerefnumber{LastPage}>1
    \thepage~of~\getpagerefnumber{LastPage}%
  \fi%
}
\begin{document}

First page.

\clearpage

Last page.
\end{document}

If you're running into problems with unprocessed floats at the end of the document, one might have to consider using atveryend.

  • I like the refcount package. – StrongBad Nov 4 '13 at 8:33
5

The problem is that the page counter is incremented at every call of the output routine and there is at least a call of it when TeX decides the document has ended. So totcount is unreliable with page.

You can use zref-lastpage for this purpose, because it provides expandable access to the referenced value.

\documentclass{article}
\usepackage{zref-lastpage}
\usepackage{fancyhdr}

\makeatletter
\fancypagestyle{plain}{%
  \fancyhf{}%
  \fancyfoot[C]{%
    \ifnum\zref@extractdefault{LastPage}{page}{0}>1
      \thepage\ of\ \zref@extract{LastPage}{page}%
    \fi
  }
  \renewcommand{\headrulewidth}{0pt}%
}
\makeatother
\pagestyle{plain}

\begin{document}
x

% uncommenting the following lines will show page numbers as “1 of 2” and “2 of 2”
% \clearpage
% y

\end{document}
  • I feel silly now. Of course you cannot use totcount with page, otherwise there would be no need for packages like lastpage. Thank you for seeing around my question and realizing that my problem was accessing the reference value. – StrongBad Nov 4 '13 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.