I write a lot of documents (agreements) based on memoir
with the article
option. Normal pagestyle
is 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}}
\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}
Is there better and simpler solutions? For example one that does not involve additional packages?
To include the macro \oneormorepages
in 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-totpages
and 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}
\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}
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
\let\@oddhead\@empty\let\@oddfoot\@empty
\let\@evenhead\@empty\let\@evenfoot\@empty{}%
\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.
Best Answer
This doesn't require any extra packages or an extra
\label
at the end, but it does still write to the aux file.Since the information is written to the aux file you need two passes for it to work correctly.