# Automatically remove page number if document has only one page (but not if two)

I write a lot of documents (agreements) based on memoir with the article option. Normal pagestyleis plain (page number centered in the footer). Sometimes a document is only one page long, and a page number is unnecessary.

It is not much work to put a \thispagestyle{empty} after \maketitle (because \maketitle has hardcoded a \thispagestyle{plain} somewhere). But sometimes I forget this, and sometimes I add a word or two, and the document suddenly is two pages long. And it is of course much more fun to have LaTeX to do this automatically.

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}


Is there better and simpler solutions? For example one that does not involve additional packages?

To include the macro \oneormorepagesin the text is a kludge. I can redefine \maketitle, but I am reluctant to that. Is there a better solution for integrating the test in the preamble? My goal is to include such test in a separate, private style- or class-file?

EDIT: And 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 is 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}


EDIT: MWE is updated 28.09.2014 and improved based on comments from egreg and JFBU.

Thanks a lot for all help!

FINAL EDIT 28.09.2014

Based on the discussion in this question and this answer to another question, I have a working solution not requiring any additional packages, working under KOMAscript and the standard classes and surviving \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. (At least for the time being).

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 make it more useful by adding additional code to the false or 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}


And finally, one for the standard classes, conditionally redefining the plain-pagestyle. (let\ps@plain\ps@empty did not work).

\documentclass{article}
\usepackage{lipsum,zref-totpages}
\pagenumbering{Roman}
\makeatletter
\renewcommand{\ps@plain}{%
\ifnum\ztotpages=\@ne\let\@mkboth\@gobbletwo
\fi}
\makeatother

\title{Test}
\author{Test Testson}
\begin{document}
\maketitle

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


Hopefully, it is useful.

-
If your documents all have a \maketitle line, then you can change its definition to have \oneormoreside instead of \thispagestyle{plain}. If they haven't it necessarily, say also \AtBeginDocument{\oneormoreside}. –  egreg May 21 '12 at 21:24
You could get rid of the ifthen too package by using \ifnum instead of \ifthen –  Andreas Wallner May 22 '12 at 8:33

This doesn't require any extra packages or an extra \label at the end, but it does still write to the aux file.

\documentclass{article}
\usepackage{lipsum}
\title{Test}
\author{Test Testson}

\makeatletter
\AtEndDocument{%
\ifnum\value{page} > 1%
\immediate\write\@auxout{\string\xdef\string\@multipage\string{\string}}%
\fi%
}
\newcommand{\oneormoreside}{\ifdefined\@multipage\else\thispagestyle{empty}\fi}
\makeatother

\begin{document}%
\maketitle
\oneormoreside

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


Since the information is written to the aux file you need two passes for it to work correctly.

-
Why not simply \let\noexpand\@multipage\noexpand\@empty as the token list to write in the aux file? \xdef\@multipage{} is not different from \let\@multipage\@empty and more efficient (\xdef serves no real purpose and \def would be the same: the aux file is not read inside a group). However there are many things that might make the test fail. –  egreg May 22 '12 at 12:55
@egreg thanks for the helpful comments, both are improvements. When you say there are many things that might make the test fail, are you referring to the \ifnum test, the \ifdefined test or both? –  StrongBad May 22 '12 at 13:17
Something can add to the document after what you've added with \AtEndDocument. It's uncommon, but it can happen. Just be aware of that. –  egreg May 22 '12 at 13:19
@egreg I just checked and I need \xdef, well I only need \gdef, but for some reason it has to be global. –  StrongBad May 22 '12 at 13:25
So \global\let\noexpand... –  egreg May 22 '12 at 13:27

You requested a solution without "extra packages". Here is a solution without zref, but it still needs ifthen:

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

\begin{document}

\newcommand{\mypagestyle}%
{\ifthenelse{\pageref{LastPage}>1}%
{\thispagestyle{plain}}%
{\thispagestyle{empty}}%
}
\mypagestyle

\lipsum[2-10]

\label{LastPage}
\end{document}


P.S. Personally, I would prefer a solution like yours but with lastpage (http://ctan.org/tex-archive/macros/latex/contrib/lastpage) instead of zref.

-
Your solution is elegant, but breaks if \pagenumbering{Roman} (see the answer referred to in my question). In addition, you have two commands in the text. But one vote for reducing the number of packages. I was not able defined a workable macro with \lastpage. \refcount was easier. –  Sveinung May 21 '12 at 22:22