Headers and footers in LaTeX (fancyhdr)

Load fancyhdr, switch it on with \pagestyle{fancy} and fill the fields with \fancyhead and \fancyfoot. Each of them has a left, a centre and a right field.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{fancyhdr}

Fill the header and the footer

\fancyhf{} empties all six fields. The letter in brackets picks the field: L left, C centre, R right. \thepage prints the page number.

This goes into the preamble.

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{Project report}
\fancyhead[R]{Anna Schmidt}
\fancyfoot[C]{\thepage}

Open in the editor

Show the current section in the header

\leftmark holds the number and title of the current section in article, and of the current chapter in report and book. \rightmark holds the level below it. fancyhdr stores both in capitals; \nouppercase prints them as written.

This goes into the preamble.

\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\nouppercase{\leftmark}}
\fancyfoot[C]{\thepage}

Open in the editor

Change or remove the lines

\headrulewidth is the thickness of the line under the header (0.4 pt by default) and \footrulewidth the line above the footer (0 pt by default). Set a width to 0pt to remove the line.

This goes into the preamble.

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0.4pt}

Open in the editor

Different fields on left and right pages

In a two-sided document, add E for even (left-hand) pages and O for odd (right-hand) pages. [LE,RO] puts the page number at the outer edge of every page.

Complete document:

\documentclass[twoside]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{Project report}
\fancyhead[LO]{\nouppercase{\leftmark}}
\begin{document}
\section{Introduction}
Text on the first page.
\newpage
Text on the second page.
\end{document}
Result
Page 1 of the compiled examplePage 1
Page 2 of the compiled examplePage 2

Open in the editor

KOMA-Script classes

With scrartcl, scrreprt or scrbook, load scrlayer-scrpage instead. \ihead, \chead and \ohead fill the inner, centre and outer field of the header, \ifoot, \cfoot and \ofoot those of the footer. With the automark option, \headmark prints the current section; \pagemark prints the page number.

Complete document:

\documentclass{scrartcl}
\usepackage[automark]{scrlayer-scrpage}
\clearpairofpagestyles
\ihead{Project report}
\ohead{\headmark}
\cfoot{\pagemark}
\begin{document}
\section{Introduction}
Text.
\end{document}
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages