Page LayoutPrint page number if #pages > 1

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Print page number if #pages > 1

Post by svend_tveskaeg »

Hi all.

In a arbitrarily LaTeX document, I would like to have the page number printed only if the number of pages is strictly greater than 1. (In a single-paged document, the page number should not be printed.)

I would like the number to be printed at the bottom of the page, centered (i.e., inside \cfoot{} form the fancyhdr package).

How do I do this?

I am thinking of a construction like

Code: Select all

\if\count{\pagenumber}>1
  \cfoot{\thepage}
 \else
  \cfoot{}
\fi
 
but it is just a thought.

Thank you in advance!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Print page number if #pages > 1

Post by localghost »

Off the top of my head I would suggest something like this.

Code: Select all

\cfoot{\ifnum\value{page}>1\thepage\else\fi}

Thorsten
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Print page number if #pages > 1

Post by svend_tveskaeg »

Consider the following MWE:

Code: Select all

\documentclass{article}

\usepackage{fancyhdr}
\pagestyle{fancy}
\cfoot{\ifnum\value{page}>1\thepage\else\fi}

\begin{document}

Test
\newpage

Test

\end{document}
Here, nothing is printed in the centered footer.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Print page number if #pages > 1

Post by localghost »

Sorry. A very small detail can make the difference. I have forgotten a simple blank space.

Code: Select all

\cfoot{\ifnum\value{page}>1 \thepage\else\fi}
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Print page number if #pages > 1

Post by Stefan Kottwitz »

Wow, a simple space! I tested it before, thanks to the MWE, and wondered about it. Seems that control sequences have to be separated here. {} works as well, as of course a line break would do.

Stefan
LaTeX.org admin
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Print page number if #pages > 1

Post by svend_tveskaeg »

Hmm. I have not expressed myself correct. Sorry! :oops:

If the document is more than one page long, I would like to have a page number on every page, including the first. (In the MWE above with the correction, i.e. with the extra space in the command, there is no page number on the first page, but there are on the rest af them.)

If there is a single page, however, there should be no page number.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Print page number if #pages > 1

Post by localghost »

Obviously a small misunderstanding. You can achieve what you want easily by the totcount package.

Code: Select all

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{totcount}

\regtotcounter{page}

\cfoot{\ifnum\totvalue{page}>1 \thepage\else\fi}
\pagestyle{fancy}

\begin{document}
  Test
  \newpage

  Test
\end{document}
It needs at least two compiler runs to get everything right.
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Print page number if #pages > 1

Post by svend_tveskaeg »

Spot on!

Thank you very much.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Print page number if #pages > 1

Post by cgnieder »

localghost wrote:Sorry. A very small detail can make the difference. I have forgotten a simple blank space.

Code: Select all

\cfoot{\ifnum\value{page}>1 \thepage\else\fi}
Stefan_K wrote:Wow, a simple space! I tested it before, thanks to the MWE, and wondered about it. Seems that control sequences have to be separated here.
That's because \ifnum scans ahead (and expands in the process) after the 1 until it finds a space or another non-number. It cannot know that 1 isn't the beginning of 17, say. So even if the actual value of the page counter was zero always the “false” branch was executed:

Code: Select all

\ifnum\value{page}>1\thepage\else\fi
would have been equivalent to

Code: Select all

\ifnum0>10\else\fi
That's why it's good practice to put a \relax after the test:

Code: Select all

\ifnum\value{page}>1\relax\thepage\else\fi
Best
site moderator & package author
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: Print page number if #pages > 1

Post by localghost »

Clemens,

many thanks for this clarification. I see that there's lot to learn for me.
Post Reply