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}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}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}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}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}Pitfalls
\maketitleand the first page of a chapter switch to the page styleplain, so your header is missing there. Redefine that style with\fancypagestyle{plain}{\fancyhf{}\fancyfoot[C]{\thepage}}.- The warning
\headheight is too smallappears when the header is taller than the space reserved for it, for example with a 12 pt font. The warning names the height it needs; set it in the preamble, for example\setlength{\headheight}{14.5pt}. \thispagestyle{empty}removes header and footer from one page.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
fancyhdr - Package
scrlayer-scrpage - How-to Page numbers in LaTeX
- How-to Set page margins in LaTeX