Headers and footers hold running text such as a document title, a chapter name, or a page number. LaTeX sets a basic page style for you, and the fancyhdr package and KOMA-Script give you full control. This guide covers both, along with the two-sided layouts used in books and theses.
Set up a header and footer with fancyhdr
The fancyhdr package defines header and footer fields with three positions: left (L), center (C), and right (R). The \fancyhf{} command clears all of them, so you start from a blank page style:
\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear the default header and footer
\fancyhead[L]{Project report} % fixed text at the left of the header
\fancyhead[R]{\rightmark} % current section title at the right
\fancyfoot[C]{\thepage} % page number at the center of the footer
\renewcommand{\headrulewidth}{0.4pt}
\begin{document}
\section{Introduction}
Text goes here.
\end{document}
\rightmark prints the current section title, and it changes as the sections change. If you also use the geometry package, load it before fancyhdr. For more on margins, see how to change page margins in LaTeX with geometry.
Use separate entries for odd and even pages
fancyhdr uses E for even-numbered pages and O for odd-numbered pages. In a two-sided document, even pages are on the left and odd pages are on the right. A common layout puts the page number on the outer edge of each page:
\documentclass[twoside]{book}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE]{\leftmark} % left side of even pages
\fancyhead[RO]{\rightmark} % right side of odd pages
\fancyfoot[LE,RO]{\thepage} % page number on the outer edge
\renewcommand{\headrulewidth}{0.4pt}
In book and report, \leftmark usually holds the chapter title and \rightmark holds the section title. In article, only section titles are available, through \rightmark.
Show chapter titles in the header
You can control what LaTeX stores in the marks with two commands. Put these lines in the preamble, after \usepackage{fancyhdr}:
\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\MakeUppercase{#1}}}
\markboth{left}{right} sets both marks at once. \markright sets only the right mark. This pattern, which many thesis templates use, prints the titles in capitals. For chapter titles on the left of even pages, keep the \fancyhead[LE]{\leftmark} line from the example above.
Chapter opening pages use the plain style
In book and report, the first page of each chapter uses the plain page style, not the running header you set. To give those pages a simple footer and no header, redefine plain:
\fancypagestyle{plain}{%
\fancyhf{}%
\fancyfoot[C]{\thepage}%
\renewcommand{\headrulewidth}{0pt}%
}
Two-sided layouts
Several settings control how a two-sided document behaves:
- The
twosideoption turns on two-sided printing.bookuses it by default.articleandreportuseonesideby default. - In
book, chapters start on right-hand pages by default (openright). Theopenanyoption lets a chapter start on any page. - The
\cleardoublepagecommand ends the current page and adds a blank page if needed, so the next page is a right-hand one. - To keep margins mirrored across the spine, use the
innerandouteroptions ofgeometry. The margin guide shows an example.
Use KOMA-Script page styles
The KOMA-Script classes scrbook and scrreprt provide a page style called scrheadings. The scrlayer-scrpage package controls what goes in its header and footer. The example below is a common starting point, and the KOMA-Script manual lists every option:
\documentclass[twoside]{scrbook}
\usepackage{scrlayer-scrpage}
\clearpairofpagestyles % clear the built-in header and footer
\ihead{\headmark} % inner header: current chapter or section
\ohead{\pagemark} % outer header: page number
\automark[section]{chapter} % choose which titles go into the marks
\pagestyle{scrheadings}
Troubleshooting
- The even-page header is missing. The document is probably one-sided. In a one-sided document, LaTeX uses only the odd-page entries (
O), so anEentry never prints. Addtwosideto the class options if you want different pages. - A header or page number is on the same side of every page. Use
LEandROentries rather thanLandR, and check that the document is two-sided. - “\headheight is too small” warning. The header needs more room. Add
\setlength{\headheight}{14pt}to the preamble. - The chapter opening page shows the wrong header. Redefine the
plainpage style, as shown above.
For more on page numbers, see page numbering in LaTeX. For chapter styling, see chapters in LaTeX.