Site icon texblog

Header/Footer in LaTeX with Fancyhdr

Creating headers and footers in Latex can be done by using the package “fancyhdr”. This is a short introduction, showing the most important features of the package. If you know “fancyhdr” and are looking for something particular, refer to the fancyhdr-documentation.

First of all, you need to tell Latex to use the package:

\usepackage{fancyhdr}

and change the style from “plain” to “fancy”:

\pagestyle{fancy}

You will now the get the default fancy pagestyle which adds a line at the top of every page, except for some exceptions (title-page, abstract, new chapter in report).

Default fancyhdr page style:

Above the line, Latex will print headings:

Book/report

Left-hand side: section
Right-hand side: chapter
Note: if you use the optional documentclass argument “twoside”, Latex will alter the position of the section and chapter. (e.g. \documentclass[twoside]{report}, also introducing non-symmetric margins).

Article

For acticles, Latex will print the section only (chapters cannot be used with articles).
The footer only includes the page number which is centered by default.

Custom fancyhdr page style:

Even though fancyhdr has a default page style, you are free to define headers/footers yourself , which is not too difficult after all.

First you need to clear the default layout:

\fancyhead{}
\fancyfoot{}

There are seven letters you need to know before you can define your own header/footer:

E: Even page
O: Odd page
L: Left field
C: Center field
R: Right field
H: Header
F: Footer

Now it’s time to start defining your own layout. The definitions are added to the preamble:

\fancyhead[CO,CE]{---Draft---}
\fancyfoot[C]{Confidential}
\fancyfoot[RO, LE] {\thepage}

The decorative lines can be changed by increasing/decreasing their thickness (0pt means no line):

\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

Note: According to the fancyhdr-documentation, the default layout is produced by the following commands:

\fancyhead[LE,RO]{\slshape \rightmark}
\fancyhead[LO,RE]{\slshape \leftmark}
\fancyfoot[C]{\thepage}
\headrulewidth 0.4pt
\footrulewidth 0 pt
Exit mobile version