General LaTeX tips and tricks → Custom headers

## 18. How can you get custom page headers and footers?

Use the fancyhdr package by Piet van Oostrum.

This is another powerful package, which rewards a good browse through its documentation. It allows complete control over headers and footers on even and odd pages, although it can be a bit of a puzzle to get things right (and I am not sure if my way of using it is entirely correct — but in the end, it does what I want it to do).

As a kind of hidden tribute I wanted the headers in my thesis to be a close approximation of those in Donald Knuth's The Art of Computer Programming. That style is in fact not so different from what LaTeX already does by default, but nevertheless some tweaking was necessary.

### Example usage

In the preamble:


%% We will completely define our own header strings, so switch the fancy
%% headers on, but nuke all the default values. (Note that this package
%% *has* to load after the geometry package!)

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}

%% Now begin customising things. See the fancyhdr docs for more info.

\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\MakeUppercase{#1}}{}}



In the document:


\include{dedication}

\frontmatter{}

%% Configuration of the header strings for the frontmatter pages.
\setcounter{tocdepth}{2}

...

\mainmatter{}

%% Configuration of the header strings for the main manuscript pages.

\include{1-introduction}

...

\include{8-conclusion}

%% Configuration of the header strings for the Appendices
\renewcommand{\chaptermark}[1]{%
\markboth{\MakeUppercase{\appendixname\ \thechapter}}%
{\MakeUppercase{#1}}
}

\appendix

\include{A-rule-language-grammar}

...

\include{A-examples}

\backmatter{}

%% Configuration of the header strings for the backmatter pages.
\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\MakeUppercase{#1}}{}}

\include{bibliography}



### Results, before

Even-numbered page:

Odd-numbered page:

### Results, after

Even-numbered page:

Odd-numbered page:

### Things to watch out for

This package must load after the geometry package.

Leo Breebaart (leo@lspace.org)
Last updated: 27 June 2016