How to Customize \leftmark and \rightmark with fancyhdr
At a glance
- 1. Choose the mark source
In book and report layouts, chapter and section commands normally set the running marks.
- 2. Redefine mark commands
Customize \chaptermark and \sectionmark to control the text stored in those marks.
- 3. Place marks in header
Use fancyhdr fields to show \leftmark and \rightmark on the desired pages.
- 4. Check the page style
Compile a two-sided example and verify the even- and odd-page headers.
The usual way to customize running headings with fancyhdr is to change the commands that write the marks, not the commands that read them. In the standard book and report classes, \chaptermark and \sectionmark set the text later accessed through \leftmark and \rightmark.
Here is a complete example for a two-sided book. It puts the page number at the outside edge, the chapter title on even pages, and the section title on odd pages.
\documentclass[twoside,openright]{book}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[LO]{\nouppercase{\rightmark}}
\fancyhead[RE]{\nouppercase{\leftmark}}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\chaptermark}[1]{%
\markboth{\thechapter.\ #1}{}%
}
\renewcommand{\sectionmark}[1]{%
\markright{\thesection.\ #1}%
}
\begin{document}
\chapter{Getting Started}
\section{A First Section}
Text on the first page.
\newpage
Text on the next page.
\end{document}
The example uses the fancyhdr package; fontenc is included for conventional T1 font encoding with pdfLaTeX. The heading positions are specified by LE, RO, LO, and RE: left-even, right-odd, left-odd, and right-even. With \fancyhf{}, existing fancy header and footer fields are cleared first.
What the mark commands do
LaTeX’s running marks are stored as pages are built. In the standard book class, the chapter command sets a left mark, while the section command sets a right mark. The header macros \leftmark and \rightmark retrieve the appropriate stored text for the page. This lets LaTeX handle headings that cross page boundaries.
In the example, \chaptermark writes the chapter number and title using \markboth; its second argument is empty so it does not set a competing right mark. \sectionmark writes the section number and title using \markright. The \nouppercase wrappers guard against \MakeUppercase transformations in the displayed marks. With the custom mark definitions shown, the titles retain their typed capitalization because those definitions do not use \MakeUppercase.
Why not redefine \leftmark directly?
You can redefine \leftmark, but that changes how the stored mark is interpreted rather than changing what chapter or section commands store. It can discard the normal page-mark behavior, and may make the result depend on where a page break occurs. For ordinary header customization, redefining \chaptermark and \sectionmark is the more robust approach.
If you only need to change where the headings appear, leave the mark commands alone and edit the \fancyhead assignments. For example, to put both marks in the center header, use:
\fancyhead[C]{\nouppercase{\leftmark\ ---\ \rightmark}}
Whether both marks contain useful text depends on the document class and its mark definitions.
Class differences and common mistakes
The definitions of \chaptermark and \sectionmark vary by class. The example is intended for the standard book class; report uses a similar chapter-and-section structure. The standard article class has no chapters, so its section marks are handled differently. Check the class’s mark definitions before copying chapter-specific code into another class.
Common problems include:
- The header is blank: The current page may not yet have encountered a section or chapter mark. Some page styles, especially the first page of a chapter, also use a special style such as
plain. - The title is unexpectedly uppercase: Remove uppercase formatting in the mark definition or use
\nouppercasewhere the mark is printed. - The wrong title appears: Confirm that you are using
\markbothand\markrightin the appropriate mark command and that your chosen header field displays the matching mark. - The header is too wide: Shorten the running title or adjust the header layout. A long mark can extend into the margin or overlap the page number.
If you want a different running title from the full chapter or section title, you can give the heading command an optional short title, such as \section[Short header]{A Much Longer Section Title}. Standard classes generally use that optional title for the running mark while printing the full title in the body.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.