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

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}}
\zref@addprop{main}{numpage}
\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}
\clearscrheadfoot
% 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.


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}

enter image description here

This will take 2 compilation runs to settle down.