Preserve page numbering in fancyhdr

by coding_heart   Last Updated May 15, 2019 15:23 PM - source

I'm using the fancyhdr package to include a header on each page. In order to surpress the section title also being cited in the header of each page, I'm using the command \fancyhf{}, however this also erases all page numbers after the first page. Does anybody know how to get them back in?

See code below

\documentclass[10pt, letter]{article}
\usepackage{setspace}
\usepackage[margin=1.250in]{geometry}
\singlespacing
\usepackage{fancyhdr}
\fancyhf{}
\pagestyle{fancy}
\rhead{\textcolor{gray}{Mock Draft}}
\renewcommand{\headrulewidth}{0pt}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor

\begin{document}
xxxxx
\clearpage
xxxx
\clearpage
xxxx
\end{document}

Many thanks!



Answers 4


This can be done with the help of the afterpage package.

Before the page where you want the page number begins, put:

\afterpage{\cfoot{\thepage}}

So, your MWE can be rewritten as:

\documentclass[10pt]{article}
\usepackage{setspace}
\usepackage[margin=1.250in]{geometry}
\singlespacing
\usepackage{fancyhdr}
\usepackage{afterpage}
\fancyhf{}
\pagestyle{fancy}
\rhead{\textcolor{gray}{Mock Draft}}
\renewcommand{\headrulewidth}{0pt}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor

\begin{document}
xxxxx
\afterpage{\cfoot{\thepage}}
\clearpage
xxxx
\clearpage
xxxx
\end{document}

Note that I deleted letter from the options of the article class. The default is already the letter paper and anyway the right option is letterpaper.

karlkoeller
karlkoeller
July 01, 2013 17:46 PM

Using \fancyhf{} clears the header and footer of anything. However, if you wish to insert the page number, just add \thepage to the appropriate location. If you wish to do this conditionally, that is also possible through something like

\fancyhead[L]{\ifnum\value{page}<2\relax\else\thepage\fi}

which will place \thepage int the [L]eft header if its value is at least 2.

Werner
Werner
July 01, 2013 18:19 PM

There's no need of special settings, assuming you don't want the header in the first page; just define the center footer (or any other field you like) to contain the page number.

\documentclass[10pt, letter]{article}

\usepackage[margin=1.250in]{geometry}
\usepackage[table]{xcolor}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{\textcolor{gray}{Mock Draft}}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0pt}

\begin{document}
\thispagestyle{plain} % not needed if a \maketitle command is issued

xxxxx
\clearpage

xxxx
\clearpage

xxxx

\end{document}
egreg
egreg
July 01, 2013 20:47 PM

After \begin{document} (or in the document preamble), issue

\renewcommand{\thepage}{\roman{page}}% Roman page numbers

On the page where you want it to change to Arabic numbering, use

\renewcommand{\thepage}{\arabic{page}}% Arabic page numbers

The above just modifies the page number/counter printing mechanism. In contrast, using \pagenumbering{arabic} will change the numbering to Arabic but also resets the page counter.

This text is copied from: https://tex.stackexchange.com/a/56133/110064 (Werner)

JoKalliauer
JoKalliauer
May 15, 2019 14:33 PM

Related Questions



Wrap page number in colored box in footer

Updated May 25, 2015 04:10 AM

multiple headers and pagination in a book

Updated July 30, 2015 14:10 PM

Pagenumbering on chapter pages

Updated February 22, 2017 08:23 AM

How to move page number?

Updated August 12, 2017 21:23 PM