Thursday, June 28, 2012

Getting the total number of pages into a counter

This is the only way i know of, to get the total number of pages for anything other than mere display. Took me two hours to get just right and requires two packages. There is a surprising lot of packages that do similar things but in the end all just print. Weird. Enjoy

\documentclass{article}
\newcounter{c@LastPage}
\usepackage{lipsum}
\usepackage{atveryend}
\begin{document}
a %\lipsum
%\lipsum
\makeatletter
\AfterLastShipout{\immediate\write\@auxout{\string\setcounter{\string c@LastPage} {\thepage}}}
\ifnum\value{c@LastPage}>2 foo \fi
\makeatother
\end{document}

No comments:

Post a Comment