Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
Generally, you can make a package by taking the code from the preamble and inserting into a .sty file. The Creating Packages page at Wikibooks is one reference, I'm sure we have others here. –  Mike Renfro Mar 16 at 14:38
    
@MikeRenfro That's right, hence the "preamble or .sty file" line. They're equivalent options. –  Sverre Mar 16 at 14:48
    
You're right, I should have read more closely. The weird thing is the two runs of \onemoreside, one in the \maketitle definition, and another after \maketitle. This resembles something I had to do with my thesis class, and I know I didn't have to include odd commands like this in it. Will see what I can find. –  Mike Renfro Mar 16 at 14:54

4 Answers 4

up vote 10 down vote accepted

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.

share|improve this answer
    
Follow-up at tex.stackexchange.com/questions/165914/… –  Sverre Mar 16 at 17:50

If I understand you correctly, you want to remove the page number on the first page, but preserve the header. This can be done by defining a custom fancyhdr style that's identical to the default fancy but with no pagenumbering and use that for the first page only. Full MWE:

\documentclass{article}
\usepackage{lipsum,fancyhdr}
    \pagestyle{fancy}
    \fancyhead[C]{Don't ever remove this header}
\fancypagestyle{mystyle}{
    \fancyhf{}
    \fancyhead[C]{Don't ever remove this header}
    \fancyfoot[C]{}
}
\begin{document}
\thispagestyle{mystyle}
\lipsum[1] % automatically remove page number in a document with this line
\pagestyle{fancy}
\lipsum[1-6] % automatically keep page numbers in a document with this line
\end{document}

Victory! Got the automation-part working using lastpage and ifthen. In short, it checks whether or not the document lenght (as determined by lastpage) is more than 1 page. If the document is 1 page, the style mystyle is used where there is page numbering. However, if the document is longer than 1 page the first page is using mystyle and the following pages will be using the fancystyle.

\documentclass{article}
\usepackage{lipsum,fancyhdr,ifthen,lastpage}
\pagestyle{fancy}
\fancyhead[C]{Don't ever remove this header}
\fancypagestyle{mystyle}{
    \fancyhf{}
    \fancyhead[C]{Don't ever remove this header}
    \fancyfoot[C]{}
}
\begin{document}
\ifthenelse{\pageref{LastPage}=1}
{
    \pagestyle{mystyle}
}
{
    \thispagestyle{mystyle}
}
\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}

Only thing now is that it can't be used in a preamble...

share|improve this answer
    
In this case, however, I need to manually keep track of how long the document is, in order to know whether or not to include \thispagestyle. I'm looking for a way in which LaTeX automatically keeps track for me and removes the page number if the document only has one page. –  Sverre Mar 16 at 16:06
    
So some kind of if-statement to check whether or not the document is longer than 1 page? And thanks for the edit, I finally figured out how to properly include code. –  Argo Mar 16 at 16:10
    
That's right. xxxx –  Sverre Mar 16 at 16:11
    
Well, luckily latex LaTeX is turring complete, so it should be possible. Brb. –  Argo Mar 16 at 16:13

The example with lastpage package:

\documentclass{article}
\usepackage{lastpage}
\usepackage{refcount}
\usepackage{etoolbox}
\usepackage{lipsum,fancyhdr}
\pagestyle{fancy}
\fancyhead[C]{Don't ever remove this header}
\begin{document}
\ifnumcomp{\getpagerefnumber{LastPage}}{>}{2}{}{\fancyfoot{}} 
 % If document is longer than 2 pages there will be page numeration, if not there will     not be.
\lipsum[1] % automatically remove page number in a document with this line
\end{document}
share|improve this answer

Good evening, we can test the page number at the end of the document. If there is only one page, it will clear the footer in the center, otherwise it moves on without changing default values and the page number stays untouched.

\documentclass{article}
\usepackage{lipsum,fancyhdr}
\pagestyle{fancy}
\fancyhead[C]{Don't ever remove this header}
\begin{document}
\lipsum[1]
%\lipsum[1-6] % Turn on/off this line...
\ifnum\thepage=1\fancyfoot[C]{}\fi
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.