Customizing headers and footers

Page headers and footers in LaTeX are defined by the \pagestyle and \pagenumbering commands.

Using the fancyhdr package, you can create customized headers and footers. I have added this to the stylesheet so it can be applied to all chapters in the project.

Defining headers and footers

To make the headers and footers more useful, use the fancyhdr package. The package fancyhdr is used with the page style fancy.

Rather than using standard header provided by default, using this package to have different headers and footers for odd and even pages, different headers and footers for chapter pages and control over font sizes, and so on.

The following commands defines the headers and footers of the document.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}

\newcommand{\helv}{\fontfamily{phv}
  \color{titlepagecolor}
  \fontseries{b}
  \fontshape{n}
  \fontsize{9}{11}\selectfont}

\renewcommand{\chaptermark}[1]{\markboth{\helv\NoUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\helv\NoUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\helv\thesection.\ #1}}
\renewcommand{\chaptermark}[1]{\markboth{\helv\thechapter.\ #1}{}}

\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\fancyfoot[LE,RO]{\helv Page \thepage}
\fancyfoot[RE,LO]{\helv Copyright \copyright 2020 Glenn J. Lea Confidential}

These commands are explained below.


Note: Information about the fancyhdr package is available here.


Font definitions

In the first command I defined the font as 9 pt Helvetica. This is defined using the short form phv. The text of the headers and footers use the titlepagecolor for the background,

1
2
3
4
5
\newcommand{\helv}{\fontfamily{phv}
\color{titlepagecolor}
\fontseries{b}
\fontshape{n}
\fontsize{9}{11}\selectfont}

The titlepagecolor which has been defined elsewhere in the stylesheet as:

1
\definecolor{titlepagecolor}{RGB}{0,38,75}

Injecting chapter and section text into the header

The text of a chapter heading is injected into the header using \chaptermark. \thechapter pulls in the chapter number. Similarly, the text of the section heading is pulled in using \sectionmark. The macro thesection pulls in the number of the section.

1
2
3
4
\renewcommand{\chaptermark}[1]{\markboth{\helv\NoUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\helv\NoUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\helv\thesection.\ #1}}
\renewcommand{\chaptermark}[1]{\markboth{\helv\thechapter.\ #1}{}}

Eliminating horizontal lines

I didn’t want to have a decorative line to separate the header from the body text so I eliminated using the 0pt value for header and footer rule width.

1
2
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

I wanted the footer to have only a page number and a copyright notice on each page.

1
2
\fancyfoot[LE,RO]{\helv Page \thepage}
\fancyfoot[RE,LO]{\helv Copyright \copyright 2017 Glenn J. Lea. Confidential}

Inserting fancyhead into the root file

After defining the headers and footers in the stylesheet, you insert the header into the root file after the \frontmatter environment command as follows.

1
2
3
4
5
6
7
\begin{document}

\frontmatter

% Configuration of the header strings for the frontmatter pages.
\setcounter{tocdepth}{3}
\fancyhead[RE,LO]{}

It is important where you place this command. The LaTeX typesetter needs to know how to build the headers and footers before any geometry is defined. After calling the \fancyhead command, you can go ahead and input your files, and so forth.

The following shows the resulting pages with headers and footers.

Sample chapter page with header and footer

Sample chapter page with header and footer.

Sample even page

Sample even page.