# 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; http://tex.stackexchange.com/a/4258/9632 ; I was able to defining this macro:

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

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

\title{Test}
\author{Test Testson}

\begin{document}
\maketitle
\oneormoreside
\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 ended up with. I patch the \maketile 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} > 1%
\immediate\write\@auxout{\string\xdef\string\@multipage\string{\string}}%
\fi%
}
\newcommand{\oneormoreside}{\ifdefined\@multipage\else\thispagestyle{empty}\fi}
\patchcmd{\maketitle}{\thispagestyle{title}}{\oneormoreside}{}{}
%% Change title to plain if you are using a standard class, not memoir
%% or make a conditional or put a new patchcmd in the false part
\makeatother

\title{Test}
\author{Test Testson}

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


Thanks a lot for all help!

-
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
If I redefine \maketitle, there is a risk that other packages will break, isn’t it? I will try to avoid this. – Sveinung May 21 '12 at 21:43
You could get rid of the ifthen too package by using \ifnum instead of \ifthen – Andreas Wallner May 22 '12 at 8:33
@AndreasWallner I was to late to edit my previous comment. What will be the exact syntax? I changed \ifthenelse with \ifnum but then the macro failed. If you change your comment as a answer, I can upvote it. – Sveinung May 30 '12 at 15:48

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}

-
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? – Daniel E. Shub 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. – Daniel E. Shub 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