Headers, footers & page numbers

The header at the top of a page and the number at the bottom exist to answer a single question: where in the book am I? The device is older than printing — medieval scribes ruled an extra line in the upper margin of a manuscript and wrote a running title there, so that a reader turning leaves could tell one part of a codex from another. LaTeX inherits the job and hands it to a mechanism called the page style: four built-in styles chosen with \pagestyle, and, once those run out, the standard package fancyhdr. This page follows the same path — why the running head vanishes on the first page of a chapter, how \leftmark and \rightmark pull chapter and section titles into the header, and how \pagenumbering numbers the front matter in roman and the body in arabic.

The four page styles \pagestyle chooses between

In plain LaTeX you do not configure headers and footers slot by slot; you switch a whole named set — a page style — at once. \pagestyle{headings} in the preamble applies from that point to the end of the document, while \thispagestyle{empty} in the body overrides the setting for the current page only. Exactly four styles come as standard.

StyleWhat appears
emptyHeader and footer both empty; no page number at all
plainEmpty header, page number centred in the footer. The default for article / report
headingsEmpty footer; the header carries chapter/section titles and the page number. The default for book
myheadingsThe same layout as headings, but automatic updating stops: you supply the text with \markboth / \markright

Which one is already in force depends on the document class. article and report start in plain — nothing but a centred page number at the foot. book starts in headings, with chapter and section titles running across the top. The split is not arbitrary: a ten-page article has no need to announce your position on every page, whereas in a four-hundred-page book the running head is exactly what lets a reader jump from the index back into the text.

What headings actually puts in the running head is also decided by the class. In a two-sided book, even (left-hand) pages carry the chapter title and odd (right-hand) pages carry the section title, with the page number always on the outer, fore-edge side. Choosing myheadings switches the automatic updating off and hands the strings to you instead, through \markboth{left page}{right page} in two-sided documents or \markright{right page}. That is as far as the built-ins bend, though: a layout as ordinary as “document title on the left, page number on the right” is already out of reach. From there on the job belongs to fancyhdr.

Why the header disappears on the first page of a chapter

The answer is written into \chapter itself. In book.cls, the second line of \chapter, right after the page break, is \thispagestyle{plain}. A chapter-opening page therefore switches to plain — no header, just a centred page number at the foot — no matter what you set. \part and \maketitle do exactly the same thing. This is why a document declared \pagestyle{empty} still shows a page number on its title page and at the head of every chapter.

Which fix you want depends on the goal. To get rid of the number as well, write \thispagestyle{empty} immediately after \chapter{…}; putting it in the preamble does nothing, because \chapter overwrites the setting at the moment it runs. If instead you want the chapter opening to match the rest of the book, rebuild the plain style itself. That is what fancyhdr’s \fancypagestyle{plain}{…} is for: define it once, and every \chapter that reaches for plain gets your design rather than the class’s.

latex
% Fix 1: drop the page number too, on this chapter page only.
% This must come AFTER \chapter, never in the preamble.
\chapter{Introduction}
\thispagestyle{empty}

% Fix 2: redefine plain, so every chapter opening follows your design.
\fancypagestyle{plain}{%
  \fancyhf{}%
  \fancyfoot[C]{\thepage}%
  \renewcommand{\headrulewidth}{0pt}%
}

\pagestyle{fancy}: the six slots fancyhdr opens up

Load fancyhdr, declare \pagestyle{fancy}, and six slots open up: left (L), centre (C) and right (R) in the header, and the same three again in the footer, each ready to be filled. The author is Pieter van Oostrum, and the copyright line inside the package runs from 1994. The clipped name has a history. The first version was called fancyheadings.sty, but MS-DOS — still very much alive at the time — kept only eight characters of a filename, so on disk it arrived as fancyhea.sty. People started writing \usepackage{fancyhea}, and those documents stopped compiling on Unix. Van Oostrum found this annoying enough to rename the package to something that fits in eight characters: fancyhdr.

The working idiom is to empty all six slots with \fancyhf{} first, then fill only the ones you want. Skip that cleanup and the class’s own running heads can survive underneath, so the same text appears twice. To fill them, use \fancyhead[L]{…} / [C] / [R] and \fancyfoot[L]{…} / [C] / [R]; the position letters can be combined, as in \fancyhead[LO,RE]{…}, to set several slots at once.

latex
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}                       % clear all six slots first
\fancyhead[L]{Document title}    % header, left
\fancyhead[R]{\thepage}          % header, right: the page number
\fancyfoot[C]{\today}            % footer, centre

In two-sided (twoside) documents the positions combine with even (E) and odd (O) pages. [LE,RO] means “left on even pages, right on odd pages” — the outer, fore-edge corner of a spread, and the traditional home of the page number. A specifier with neither E nor O, such as [L], puts the same content on both. The rules below the header and above the footer are \headrulewidth and \footrulewidth, 0.4pt and 0pt (no rule) by default. Note that these two are macros, not lengths: they are changed with \renewcommand, not \setlength — a detail that catches nearly everyone once.

latex
\renewcommand{\headrulewidth}{0.4pt}  % header rule; this is the default
\renewcommand{\footrulewidth}{0pt}    % no footer rule; also the default

Putting the chapter title in the header: \leftmark and \rightmark

Write \fancyhead[L]{\leftmark} and that page’s chapter title lands in the header. In book and report, \leftmark holds the current chapter and \rightmark the current section, and their contents are replaced every time a \chapter or \section is processed. article has no chapters, so everything shifts down a level: two-sided, \leftmark carries the section and \rightmark the subsection; in the one-sided default, \rightmark carries the section.

Underneath all this sits TeX’s \mark primitive. Each time TeX assembles a page it keeps two of the marks that appeared on it separately: the first one (\firstmark) and the last one (\botmark). The LaTeX kernel then defines \leftmark as the left half of \botmark and \rightmark as the right half of \firstmark. The last/first asymmetry makes sense as soon as you picture a dictionary: the guide words at the top of a page are only useful if they name the first and last entries actually printed on it. The design shows up in practice, too. When a section begins halfway down a page, \rightmark picks up the first mark on that page, so the running head names the section that has only just started lower down rather than the one filling the top half. That is the specified behaviour, not a bug.

The other standard stumble is capitalisation. The standard classes pass headings through \MakeUppercase in \chaptermark and \sectionmark before handing them to the mark, so \leftmark already contains something like “CHAPTER 1. INTRODUCTION”. To get the original capitalisation back, wrap it in fancyhdr’s \nouppercase, which typesets its argument in an environment where \MakeUppercase and \uppercase do nothing. The cancellation applies to everything inside, though, so deliberate capitals in the same slot are undone as well. When you want the running-head text entirely under your own control, call \markboth / \markright yourself, or redefine \chaptermark.

latex
% Chapter title on the left, page number on the right.
% \nouppercase undoes the \MakeUppercase applied by the class.
\fancyhead[L]{\nouppercase{\leftmark}}
\fancyhead[R]{\thepage}

% Or set the running-head text by hand, with no automatic updating.
\pagestyle{myheadings}
\markboth{Left-hand page text}{Right-hand page text}

Fixing Package fancyhdr Warning: \headheight is too small

The warning says the box that holds the header is shorter than what you put in it. Enlarging \headheight to the value the message names makes it go away. The standard classes set \headheight to 12pt, and a 0.4pt rule plus the leading above it is enough to overflow that — which is why the warning so often appears on the very first compile after fancyhdr is loaded. The message states the height it needs, so the number following Make it at least … can be used verbatim.

There are two ways to enlarge it: \setlength{\headheight}{15pt} directly, or the headheight= option of geometry. Either silences the warning, but if geometry is already in the preamble it is safer to keep the value there, because \headheight sits in the same arithmetic as the text height and \topmargin, and changing just one of them afterwards can shift the body text down the page. Without geometry, the classic move is to follow \setlength with a line such as \addtolength{\topmargin}{-3pt} so the totals still add up — and that is exactly the adjustment the warning message suggests.

latex
% Either one of these is enough.
\setlength{\headheight}{15pt}   % enlarge the header box directly
\addtolength{\topmargin}{-3pt} % ... and keep the vertical totals unchanged

% Or let geometry do the arithmetic for the whole page.
\usepackage[headheight=15pt]{geometry}

Switching page numbering from i, ii, iii to 1, 2, 3 (\pagenumbering)

\pagenumbering{roman} makes the following pages i, ii, iii…, and \pagenumbering{arabic} brings back 1, 2, 3…. The essential point is that the command does not only change the format: it resets the page counter to 1. LaTeX’s definition literally begins with \global\c@page \@ne. That is why the standard book arrangement — front matter in roman starting at i, body in arabic starting at 1 — costs two lines of source.

ArgumentOutput
arabicArabic numerals: 1, 2, 3 … (the default)
romanLowercase roman numerals: i, ii, iii … (the usual front matter)
RomanUppercase roman numerals: I, II, III …
alphLowercase letters: a, b, c … Page 27 stops with ! LaTeX Error: Counter too large.
AlphUppercase letters: A, B, C … Also runs out after 26 pages

If you are using the book class, those two lines are already written for you: \frontmatter is defined to include \pagenumbering{roman} and \mainmatter to include \pagenumbering{arabic}, so adding them yourself just performs the same switch twice — harmless, but redundant. article and report have no \frontmatter, so there you call \pagenumbering by hand. To begin from an arbitrary number, use \setcounter{page}{7}; to read the current number, use \thepage. One trap is worth knowing about: alph and Alph only have letters for 26 pages, so reaching page 27 halts the run with ! LaTeX Error: Counter too large.

latex
% book: \frontmatter and \mainmatter already switch the numbering.
\frontmatter    % includes \pagenumbering{roman}
\tableofcontents
\mainmatter     % includes \pagenumbering{arabic}

% article / report: no \frontmatter, so do it by hand.
\pagenumbering{roman}    % i, ii, iii ... and the counter is reset to 1
\tableofcontents
\clearpage
\pagenumbering{arabic}   % 1, 2, 3 ... counting starts over
\setcounter{page}{7}     % or force any starting number

A complete two-sided book header, start to finish

Put the pieces together into one preamble and you have the traditional book arrangement: the page number on the outer edge of the spread, the chapter title on the inner edge of even pages, the section title on the inner edge of odd pages, and a thin rule under the header. The chapter-opening page is the one place where \chapter insists on plain, so \fancypagestyle{plain} is redefined to pull it into the same design.

document.tex
\documentclass[twoside]{book}
\usepackage[headheight=15pt]{geometry}
\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}                               % clear all six slots first
\fancyhead[LE,RO]{\thepage}              % page number on the outer edge
\fancyhead[RE]{\nouppercase{\leftmark}}  % even page, inner edge: chapter
\fancyhead[LO]{\nouppercase{\rightmark}} % odd page, inner edge: section
\renewcommand{\headrulewidth}{0.4pt}     % thin rule under the header
\renewcommand{\footrulewidth}{0pt}       % no rule above the footer

\fancypagestyle{plain}{%                 % chapter openings, same design
  \fancyhf{}%
  \fancyfoot[C]{\thepage}%
  \renewcommand{\headrulewidth}{0pt}%
}

\begin{document}
\chapter{Introduction}
\section{Background}
Body text goes here. From the next page on, the running head shows
the chapter, the section and the page number.
\end{document}

A closing rule of thumb. For a short one-sided document where a number at the foot is enough, leave \pagestyle{plain} alone and add nothing. If all you want is chapter and section titles in the head, \pagestyle{headings} already does the job. Move to fancyhdr at the moment you want to decide positions and fonts yourself. Adopting each layer only when it is actually needed produces the preamble that survives longest. And one principle that is easy to forget: a running head exists so that readers can tell where they are, which is why the chapter or section they are currently in usually works harder up there than the title of the document.