# How to use the total page count in a conditional expression?

I would like to have different footer or page style depending on whether the document has one or more pages. I am not very experienced with TeX programming, so the answers about "Comparing an argument to a string when argument is a result of a command with etoolbox" did not help me much and "When to use \edef, \noexpand, and \expandafter?" is a bit complicated for me for the first time.

I tried this, but it did not work:

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{lastpage}
\begin{document}

\expandafter\ifstrequal{\pageref*{LastPage}}{1}{
\pagestyle{empty}
}{
\pagestyle{plain}}

\lipsum[2-3]

\end{document}


I may keep experimenting.

Could someone show me a good way to do this, please?

• You will find a solution in this question. – Sveinung Sep 27 '14 at 8:47
• Thanks for the link, @Sveinung. An unaccepted answer there does exactly what i want, with ifthen package and \ifthenelse{\pageref{LastPage}>1}{\pagestyle{plain}}{\pagestyle{empty}} command. However, now i am trying to do the same with etoolbox, but i do not succeed. A straightforward replacement of \ifthenelse{\pageref{LastPage}>1} with \ifnumcomp{\pageref{LastPage}}{>}{1} does not work. – Alexey Sep 27 '14 at 10:14
• I was not able to get this to work, so I ended up the other solution. Cfr. my comments. Also, have look at egreg's comments and suggested improvements in the answer that I accepted. – Sveinung Sep 27 '14 at 11:32
• The only problem for me is to convert \pageref{LastPage} to a number so that \ifnumcomp from etoolbox could use it. I saw it claimed that etoolbox can do everything ifthen can, but i do not find how. – Alexey Sep 27 '14 at 11:41
• See Heiko Oberdiek's answer here, – Sveinung Sep 28 '14 at 12:34

Instead of lastpage you can use totcount package.

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{totcount}
\regtotcounter{page}

\begin{document}

\ifnumcomp{\totvalue{page}}{>}{1}{\pagestyle{plain}}{\pagestyle{empty}}

\lipsum[2]

\end{document}


This will take 2 compilation runs to settle down.

• I'm not sure but if any other style that changes the text area size instead of plain, this might not settle down at all. I can't think of any example now but I think we can cook up one. – percusse Sep 27 '14 at 12:26
• Remember that this macro has to be used after \maketitle. – Sveinung Sep 27 '14 at 12:29
• I hope, that nobody resets the number of pages somewhere right before the end of the document, such that it totcount accidentally has the value of 1 whereas there are definitely more than 1 page – user31729 Sep 27 '14 at 17:01

I have the same problem regarding page number in documents, and found several solutions.

By borrowing some code from Ulrike Fisher’s answer to this question I was able to defining this macro:

\documentclass{article}
\usepackage{lipsum,ifthen}
\usepackage[lastpage]{zref}

\makeatletter
\zref@newprop*{numpage}{\the\value{page}}
\newcommand{\oneormorepages}%
{\ifthenelse{\zref@extractdefault{LastPage}{numpage}{1}>1}%
{\thispagestyle{plain}}%
{\thispagestyle{empty}}%
}
\makeatother

\title{Test}
\author{Test Testson}

\begin{document}
\maketitle
\oneormorepages
\lipsum[1-60] %More than one page
%\lipsum[1]   % One page
\end{document}


Here is the result I have used until today. I patch \maketitle on the fly using \patchcmd from etoolbox, a package I load for other purposes in the ‘real’ document:

\documentclass{article}
\usepackage{lipsum,etoolbox}

%% No page number  if the document ai a onepager
\makeatletter
\AtEndDocument{%
\ifnum\value{page} > \@ne
\immediate\write\@auxout{\global\let\string\@multipage\relax}%
\fi
}
\newcommand*{\oneormorepages}{%
\ifdefined\@multipage
\thispagestyle{plain}%
\else
\thispagestyle{empty}%
\fi
}
\patchcmd{\maketitle}
{\thispagestyle{plain}}%
{\oneormorepages}{}{}
%% Change plain to title if you are using a memoir class
\makeatother

\title{Test}
\author{Test Testson}

\begin{document}
\maketitle
\lipsum[1-60] % More than one page
%\lipsum[1]   % One page
\end{document}


As egreg emphasises in the comment below (and in a comment to the original answer), the solution is not hundred per cent fool proof (f.ex. it does not work under scrartcl). I have now corrected the errors jfbu has pinpointed.

Today I was even able to find two additional solutions that does not need patching etc.:

Based on the discussion in this question and this answer to another question, I have tinkered together a working solution not requiring any additional packages, and working under KOMAscript and the standard classes. It survives \pagenumbering{Roman}. As egreg has pointed out, it is still not fool proof, but I have tried postponing the tests by loading the atendvi- and atveryend-packages from the oberdiek-bundle and using commands from those packages. Then the tests fail. So for the MWEs below, we have to trust \AtEndDocument.

Here are the MWEs:

\documentclass{article}
\usepackage{lipsum}
\makeatletter % You may remove this line if you change\@ne to 1
\AtEndDocument{\ifnum\value{page]=\@ne\thispagestyle{empty}{}\fi} % survives \pagenumbering{Roman}
\makeatother % You may remove this line if you change\@ne to 1
\title{Test}
\author{Test Testson}

\begin{document}
\maketitle
\lipsum[1]
\lipsum[1-6] % Turn on/off this line...
\end{document}


If you need Roman numbering, you may also load zref-totpagesand change the test to:

\AtEndDocument{\ifnum\ztotpages=\@ne\thispagestyle{empty}{}\fi}


Based on this answer, I have found a solution using scrartcl, scrpage2 and zref-totpages, which also survives \pagenumbering{Roman}. You may at additional code to the falseand true parts of the test:

\documentclass{scrartcl}
\usepackage{zref-totpages,lipsum,scrpage2}
\pagestyle{scrplain}
% You may use \@ne instead of 1 if you enclose the line in a \makeatletter\makeatother
\cfoot[\ifnum\ztotpages=1 \else\pagemark\fi]{\pagemark}

\begin{document}

\lipsum[1] % automatically remove page number in a document with this line
%\lipsum[1-6] % automatically keep page numbers in a document with this line
\end{document}


Hopefully, it is useful.

• It's rather common for Germans to translate Seite into side, but it's wrong, although the words are cognate. I don't think the second method is completely foolproof, because \AtEndDocument is not necessarily executed at the last page. – egreg Sep 27 '14 at 10:23
• Thanks for the proposed solutions, but the second one looks a bit too low-level for me (i might be not the only person to look at my document class source code). Using ifthen, i could also make it work, but now i am trying to convert it to etoolbox staying "high-level", see my comment to my question. – Alexey Sep 27 '14 at 11:37
• @Sveinung you can replace \string{\string} with {}. – user4686 Sep 27 '14 at 11:39
• you can use \immediate\write\@auxout{\global\let\string\@multipage\relax} and besides your MWE is with article but uses title and not plain thus it does not work as expected in the single page case. Also are you sure you don't want your patch something like \newcommand{\oneormoreside}{\ifdefined\@multipage\thispagestyle{title}\else\thispagestyle{empty}\fi}  else the \thispagestyle {title} disappears entirely. – user4686 Sep 27 '14 at 11:57
• @Sveinung you can drop % in situations such as ifdefined\@multipage%, or \else%, \fi%, and on the other hand it would better with > 1 rather than > 1% because the end of line space will stop TeX scan for a number, which is good practice, here it stops on encountering the \immediate\write thing, in other contexts it could lead to unexpected hard to debug problems. Or still better, replace 1 by \@ne. – user4686 Sep 27 '14 at 17:29