Home General LaTeX tips and tricks → Custom headers [Previous Page] [Question index] [Next Page]

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


Answer

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}}{}}
\renewcommand{\headrulewidth}{0pt}

In the document:


\include{dedication}

\frontmatter{}

%% Configuration of the header strings for the frontmatter pages.
\fancyhead[RO]{{\footnotesize\rightmark}\hspace{2em}\thepage}
\setcounter{tocdepth}{2}
\fancyhead[LE]{\thepage\hspace{2em}\footnotesize{\leftmark}}
\fancyhead[RE,LO]{}
\fancyhead[RO]{{\footnotesize\rightmark}\hspace{2em}\thepage}

...

\mainmatter{}

%% Configuration of the header strings for the main manuscript pages.
\fancyhead[RE,LO]{\thesection}

\include{1-introduction}

...

\include{8-conclusion}

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

\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}}{}}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[LE]{\thepage\hspace{2em}\footnotesize{\leftmark}}
\fancyhead[RE,LO]{}
\fancyhead[RO]{{\footnotesize\leftmark}\hspace{2em}\thepage}

\include{bibliography}


Results, before

Table of contents page:

'Before' screenshot

Even-numbered page:

'Before' screenshot

Odd-numbered page:

'Before' screenshot

Results, after

Table of contents page:

'Before' screenshot

Even-numbered page:

'Before' screenshot

Odd-numbered page:

'Before' screenshot

Things to watch out for

This package must load after the geometry package.


[Previous page] [Question index] [Next page]

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