[Tex/LaTex] Automatically remove page number in one-page documents

automationpage-numbering

I find LaTeX's choice of putting a page number in one-page documents stylistically odd, so I would like to suppress that behavior. I've previously done this manually, but it would be better to not need to keep track of how long the document is myself.

The question has been asked before, but the preferred solution by the OP involves playing with \maketitle, which has no relevance to me here. The accepted answer creates a macro that needs to go after \begin{doucument}, but it would be better to have something that I can put in my preamble or .sty file, so that I don't need to remember to always put some custom macro in every document I write.

MWE:

\documentclass{article}
\usepackage{lipsum,fancyhdr}
    \pagestyle{fancy}
    \fancyhead[C]{Don't ever remove this header}
\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}

Best Answer

There are some packages that can more or less count the number of pages (totpages, count1to, lastpage, ...). The example uses package zref-totpages:

\documentclass{article}

\usepackage{zref-totpages}

\usepackage{lipsum,fancyhdr}
\pagestyle{fancy}
\fancyhead[C]{Don't ever remove this header}
\fancyfoot[C]{%
  \ifnum\ztotpages=1 \else\thepage\fi
}

\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}

Two LaTeX runs are needed, because at the time, when the first page is shipped out, it is not known in general, how many pages will follow.

\ztotpages is zero in the first LaTeX run, when the zref label LastPage is not yes recorded in the .aux file.