2

At the beginning of my document, I need to list the total number of pages in the document. I've got a system that works most of the time (see below), but if the document only has one page, then my document reads "1 pages," which is unfortunate. Is there anyway to have it correct read "1 page" if there is one page, and "n pages" if there are n > 1 pages? My document needs to compile using pdflatex.

\documentclass{article}
\usepackage{lipsum}
\usepackage{lastpage}

\begin{document}
\pageref{LastPage} pages
\lipsum[1-2]
\end{document}
3

See also How to use \ref command in the test block of \ifnum?

\documentclass{article}
\usepackage{lipsum}
\usepackage{refcount}% must go before ]astpage
\usepackage{lastpage}

\begin{document}
\expandafter\ifnum\getpagerefnumber{LastPage}>1\relax \pageref{LastPage} pages
\else \pageref{LastPage} page
\fi

\lipsum[1-2]
\end{document}
3

With zref package

\documentclass{article}
\usepackage{lipsum}
\usepackage[totpages,user]{zref}

\newcommand{\totpages}{\ifnum\ztotpages=1 1 page \else  \ztotpages\ pages \fi}

\begin{document}
\totpages
\lipsum[1-2]
\end{document}
  • I'm going to use the solution with the lastpage package because that is what I am currently using, but this solution is good, too! – Stirling Oct 28 '15 at 21:45
0

You can do like this

\documentclass{article}
\usepackage{lipsum}

\makeatletter
\AtEndDocument{%
\label{LastPage}%
\protected@write\@auxout{}%
{\string\gdef\string\mtpages{page%
\ifx\@begindvi\@empty s\fi}}
}\makeatother
\begin{document}
\pageref{LastPage} \mtpages{}
\lipsum[1-2]
\end{document}

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.