This article demonstrates how to modify document headers and footers using the fancyhdr package. Another option for customizing headers and footers, addressed in the companion article, How does LaTeX typeset headers and footers?, is to dig deeper into LaTeX code to define new page styles or redefine appropriate low-level commands.
Here, we start with an overview of some basic LaTeX features which define default headers and footers used within a document:
If you wish to skip the introductory material, jump to the section on the fancyhdr package. As noted, much more technical detail is contained in the companion article How does LaTeX typeset headers and footers? which explores and explains the low-level, mark-based, mechanisms used by TeX engines to create headers and footers—and why the \mark command is needed.
Different types of LaTeX document are created using document classes; for example, LaTeX provides a number of standard classes:
which are used via the \documentclass command:
\documentclass[⟨options⟩]⟨class⟩>
If a LaTeX document class supports different layouts for left-facing pages (even page numbers) and right-facing pages (odd page numbers) the document’s headers and footers are usually different on left- and right-facing pages. Defaults for the standard document classes are:
\documentclass[twoside]article>
\documentclass[twoside]report>
LaTeX applies a set of default ⟨options⟩ to each of the standard document classes:
Document classes also provide commands which enable users to structure their documents into logical divisions such as parts, chapters, sections, subsections and so forth. As you might expect, the book class has a rich set of document structuring commands.
In LaTeX, the default format of a document’s headers and footers are determined by the page style being used. Several predefined page styles are provided by LaTeX:
Unless the user specifies otherwise, the standard document classes apply a default page style:
Core LaTeX provides two commands to change (set) the page style:
The style of page numbers is set with the command \pagenumbering ; for example, \pagenumbering typesets page numbers in lowercase Roman numerals: i, ii, iii.
Because headers and footers form part of the overall page layout/design it can be helpful to understand a little more about LaTeX's model of page layout; the following Overleaf articles might be of interest:
As noted, LaTeX provides several built-in page styles used as defaults for the standard document classes. The following short examples demonstrate the default headers and footers produced by standard document classes using LaTeX's default ⟨options⟩ for each class.
\documentclassarticle> % Choose a conveniently small page size \usepackage[paperheight=16cm,paperwidth=12cm,textwidth=10cm]geometry> \usepackagelipsum>% for some dummy text \titleAn article class example> \authorOverleaf> \begindocument> \maketitle \sectionIn the beginning. > \lipsum[2] \sectionAnother section> \lipsum[1] \sectionYet another> \lipsum[1]
\documentclassreport> % Choose a conveniently small page size \usepackage[paperheight=16cm,paperwidth=12cm,textwidth=10cm]geometry> \usepackagelipsum>% for some dummy text \titleA report class example> \authorOverleaf> \begindocument> \maketitle \chapterOne> \sectionIn the beginning. > \lipsum[2] \sectionAnother section> \lipsum[1] \sectionYet another> \lipsum[1]
\documentclassbook> % Choose a conveniently small page size \usepackage[paperheight=16cm,paperwidth=12cm,textwidth=10cm]geometry> \usepackagelipsum>% for some dummy text \titleMemoirs of a \TeX<> user> \authorOverleaf> \begindocument> \frontmatter \maketitle This is frontmatter which uses Roman numerals. \mainmatter \chapterWhere do I start> \lipsum[4] \sectionIn the beginning. > \lipsum[1] \sectionYet another section> \lipsum[1] \chapterThings I remember> \sectionOh and a further section> \lipsum[1]
The packages/commands required to change headers and footers can vary according to the document class being used; for example, the TeX FAQ notes use of the scrlayer-scrpage package, rather than fancyhdr, for improved integration with the KOMA-script document classes. Additionally, the memoir document class contains its own features (user commands) to change headers and footers.
To load the fancyhdr package add the following line to your document preamble:
\usepackage[⟨options⟩]fancyhdr>
where ⟨options⟩ are explained in the package documentation.
The following diagram summarises the general structure of headers and footers defined by fancyhdr for a two-sided document:
In the L zone the content is typeset flush left; in the C zone it is centred and in the R zone it is typeset flush right.
Commands provided by fancyhdr use single-letter abbreviations, called “coordinates”, to identify specific header and footer zones and page locations:
This section discusses several fancyhdr commands used to define the content of headers and footers:
Each of these commands uses “coordinates” to define header and footer locations where the content should appear. Depending on which command you use, (up to) 2 or (up to) 3 coordinates may be required to fully specify the desired locations .
The following commands require up to 2 coordinates to specify particular locations in a header or footer:
where locations is an optional comma-separated list of positions, specified by 1 or 2 coordinates, in which to place content .
The following example is taken from the fancyhdr documentation:
\documentclassbook> \usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper includehead, nomarginpar,% We don't want any margin paragraphs textwidth=10cm,% Set \textwidth to 10cm headheight=10mm,% Set \headheight to 10mm ]geometry> \usepackagefancyhdr> \begindocument> % Set the page style to "fancy". \pagestylefancy> %. then configure it. \fancyhead<> % clear all header fields \fancyhead[RO,LE]\textbfThe performance of new graduates>> \fancyfoot<> % clear all footer fields \fancyfoot[LE,RO]\thepage> \fancyfoot[LO,CE]From: K. Grant> \fancyfoot[CO,RE]To: Dean A. Smith> % Some content: This is page 1.\newpage This is page 2. \enddocument>
The following graphic shows the output produced by this example:
The diagram above shows the structure of headers and footers for a two-sided document, such as the book class, or article / report classes loaded using the option twoside .
For single-sided documents based on the article or report classes, all pages are, in effect, right-hand pages so the “coordinates” for E ven pages do not apply. We also don’t need to specify the O coordinate and can use a single L , C or R value:
\documentclassarticle> \usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper includehead, nomarginpar,% We don't want any margin paragraphs textwidth=10cm,% Set \textwidth to 10cm headheight=10mm,% Set \headheight to 10mm ]geometry> \usepackagefancyhdr> \begindocument> % Set the page style to "fancy". \pagestylefancy> \titleSingle-sided document> \authorOverleaf> \dateAugust 2022> \fancyhf<> % clear existing header/footer entries % We don't need to specify the O coordinate \fancyhead[R]Hello> \fancyfoot[L]\thepage> \maketitle \sectionIntroduction> Some content. \newpage \sectionContinued. > \enddocument>
The fancyhdr package issues a warning if you try to specify headers or footers using the E coordinate within single-sided documents:
\documentclassarticle> \usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper includehead, nomarginpar,% We don't want any margin paragraphs textwidth=10cm,% Set \textwidth to 10cm headheight=10mm,% Set \headheight to 10mm ]geometry> \usepackagefancyhdr> \begindocument> % Set the page style to "fancy". \pagestylefancy> \titleWarning without twoside> \authorOverleaf> \dateJuly 2022> \fancyhead[E]Hello>% This triggers a warning! \fancyfoot[E]\thepage>% This triggers a warning! \maketitle \sectionIntroduction> Some content. \newpage \sectionContinued. > \enddocument>
As can be seen in this annotated screengrab:
the example above produces the warnings:
Package fancyhdr Warning: \fancyhead's `E' option without twoside option is useless on input line 23. Package fancyhdr Warning: \fancyfoot's `E' option without twoside option is useless on input line 24.
If you use \fancyhead or \fancyfoot in a two-sided document but omit the E and O from a location and use only 1 coordinate, L , C or R , the content will be placed in that location on even and odd pages.
The following example demonstrates omitting E and O from locations specified in \fancyhead and \ fancyfoot . It also uses the following commands:
\begindocument> % Set the page style to "fancy". \pagestylefancy> %. then configure it. % Clear all headers and footers (see also \fancyhf<>) \fancyhead<>\fancyfoot<> % Set the Centre header location but do not specify O or E \fancyhead[C]In the centre of the header on all pages: \thepage> % Set the Left footer location but do not specify O or E \fancyfoot[L]On the left of the footer on all pages: \thepage> % Some content: This is page 1.\newpage This is page 2.
The following graphic is an annotated version of the output produced by this code:
As can be seen from this graphic, the following code
\fancyhead[C]In the centre of the header on all pages: \thepage>
sets the C entre header location but does not specify if it should appear on O dd or E ven pages. The omission of O and E coordinates results in the C entre zone header content appearing on odd and even pages.
In addition, this code fragment
\fancyfoot[L]On the left of the footer on all pages: \thepage>
sets the L eft footer location but does not specify if it should appear on odd or even pages. The omission of O and E coordinates results in the L eft zone content appearing in the left zone of the footer appearing on odd and even pages.
If you omit L , C and R from a location in a two-sided document, and use only 1 coordinate, E or O , the content will be placed in all three zones on the even or odd page pages specified in the location.
The following example sets the header and footer for odd and even pages but none of the locations in the \fancyhead and \fancyfoot commands specify the “zone”, L , C or R , in which the content should be placed. Note how the content is reproduced in all three zones.
\begindocument> % Set the page style to "fancy". \pagestylefancy> %. then configure it. % Clear all headers and footers (see also \fancyhf<>) \fancyhead<>\fancyfoot<> % Set the header and footer for Even % pages but omit the zone (L, C or R) \fancyhead[E]Header: even page \thepage> \fancyfoot[E]Footer: even page \thepage> % Set the header and footer for Odd % pages but omit the zone (L, C or R) \fancyhead[O]Header: odd page \thepage> \fancyfoot[O]Footer: odd page \thepage> % Some content: This is page 1.\newpage This is page 2.
The following graphic is an annotated version of the output produced by this code:
The following code sets the header and footer for E ven pages but omits a zone coordinate ( L , C or R ):
\fancyhead[E]Header: even page \thepage> \fancyfoot[E]Footer: even page \thepage>
This results in:
The following code sets the header and footer for O dd pages but also omits a zone coordinate ( L , C or R ):
\fancyhead[O]Header: odd page \thepage> \fancyfoot[O]Footer: odd page \thepage>
This produces results similar to those for even pages:
\fancyhf is a more general command which can be used to combine specifications for headers and footers. To do this it supports the coordinates H (for H eader) and F (for F ooter). The general form of the command is
where locations is a optional comma-separated list of locations, specified by 2 or 3 coordinates, to place content .
Because this command can specify the content of headers and footers you need the third coordinate, H or F to fully specify the locations . For example, to use (refer to) the centre zone of footers on the left-hand (even) pages you would specify this as the three coordinates EFC :
Note that the order of the coordinates is not important: EFC produces the same result as FEC and so forth.
The following example uses \fancyhf to place content in the C entre zones of all headers and footers:
\begindocument> \pagestylefancy> \fancyhf<> \fancyhf[EHC]Even+Header+Centre> \fancyhf[EFC]Even+Footer+Centre> \fancyhf[OFC]Odd+Footer+Centre> \fancyhf[OHC]Odd+Header+Centre> \lipsum[1]\newpage\lipsum[1]
The following graphic is an annotated version of the output produced by the previous example:
The following rule-related commands are provided by version 4.x of fancyhdr :
NOTES:
\renewcommand\headrulewidth>2pt> \renewcommand\footrulewidth>4pt> \renewcommand\headruleskip>10mm> \renewcommand\footruleskip>10pt>
that you must use \renewcommand to change values stored by these commands. The following code will not work:
\setlength\headrulewidth>2pt>% does not work \setlength\headruleskip>10mm>% also will not work
The next example changes rule widths and shows how to modify the distance between rules (lines) and header and footer text—using \headruleskip and \footruleskip :
\begindocument> % Set the page style to "fancy". \pagestylefancy> %. then configure it. \renewcommand\headruleskip>10mm> \renewcommand\footruleskip>10pt> \renewcommand\headrulewidth>2pt> \renewcommand\footrulewidth>4pt> % Some content: \chapterWrangling settings> \sectionPage 1> This is page 1. \newpage \sectionPage 2> This is page 2. \enddocument>
The following example is derived from one contained in the fancyhdr package documentation which uses the fourier-orns package.
\documentclassbook> \usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper includehead, nomarginpar,% We don't want any margin paragraphs textwidth=10cm,% Set \textwidth to 10cm headheight=14pt,% Set \headheight to 14pt to accommodate our fancy rule! ]geometry> \usepackagefourier-orns> \usepackagefancyhdr> \renewcommand\headrule>% \vspace-8pt>\hrulefill \raisebox-2.1pt>\quad\decofourleft\decotwo\decofourright\quad>\hrulefill> \titleDecorative line> \authorOverleaf> \dateAugust 2022> \begindocument> \pagestylefancy> \fancyhf<> \fancyhead[LE]\nouppercase\rightmark\hfill\leftmark>> \fancyhead[RO]\nouppercase\leftmark\hfill\rightmark>> \fancyfoot[LE,RO]\hfill\thepage\hfill> \chapterFancy rules!> \sectionIntroduction> This example is taken from the \textttfancyhdr> documentation. \newpage \sectionSee the header> \newpage \sectionYes, very neat!> \enddocument>
This example produces the following output:
If you receive the fancyhdr warning that \headheight is too small you can reset it to some value height by writing
\setlength\headheight>height>
but the recommended solution is to use the geometry package and apply its headheight=height option to set the value of \headheight to height .
The following example revisits our earlier book class example and resets \headheight to 2cm via headheight=2cm :
\documentclassbook> \usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper includehead,% See below for an explanation nomarginpar,% We don't want any margin paragraphs textwidth=10cm,% Set \textwidth to 10cm headheight=2cm,% Set \headheight to 2cm showframe % Show the page layout ]geometry> \titleMemoirs of a \TeX<> user> \authorOverleaf> \begindocument> \frontmatter \maketitle This is frontmatter which uses Roman numerals. \mainmatter \chapterWhere do I start> Chapter 1: A short chapter that ends on page 1. \chapterThings I remember> Chapter 2: Starts on page 3, so \LaTeX<> inserts a ``blank'' page 2. \enddocument>
The example above uses several options of the geometry package:
The geometry package also provides options which affect how it calculates certain values for LaTeX page layout parameters; for example:
Our example (above) used includehead to ensure that the height of the text area is directly affected by changing the value of headheight . The following diagram, copied from the geometry package documentation, demonstrates how the includehead and includefoot options affect calculation of the document’s text height:
Note that the geometry package refers to the height of the text area via the option textheight whereas LaTeX uses the similarly-named \textheight page layout parameter.
Source: edited version of code posted on tex.stackexchange: https://tex.stackexchange.com/a/282840.
This example creates a footer which displays the current page number and the total number of pages. It uses the lastpage package together with a modified version of the plain page style implemented via the \fancypagestyle command provided by fancyhdr .
\documentclass[12pt]article> \usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper includehead,% See below for an explanation nomarginpar,% We don't want any margin paragraphs textwidth=10cm,% Set \textwidth to 10cm headheight=16pt % Set \headheight to 16pt to avoid fancyhr warnings ]geometry> \usepackagelastpage> \usepackagefancyhdr> \usepackagelipsum> % just for mock text \pagestylefancy> \renewcommand\headrulewidth>0pt> \fancyfoot[C]\thepage\ of \pagerefLastPage>> \fancypagestyleplain>% \renewcommand\headrulewidth>0pt>% \fancyhf<>% \fancyfoot[C]\thepage\ of \pagerefLastPage>>> \begindocument> \titleAnother fancyhdr demo> \author\texttttex.stackexchange> \textitet al>> \maketitle \lipsum \enddocument>
As noted in the fancyhdr package documentation, if you create a document using
any part ( \part <. >) and chapter ( \chapter <. >) pages will start on odd-numbered (right-hand) pages.
Occasionally, LaTeX needs to insert a "blank" page to ensure the next chapter or part page starts on a right-hand page—but that "blank" page will still contain headers that many people consider to be unsightly.
The following book class example creates 5 pages including 2 right-hand facing chapter pages.
\begindocument> \frontmatter \maketitle This is frontmatter which uses Roman numerals. \mainmatter \chapterWhere do I start> Chapter 1: A short chapter that ends on page 1. \chapterThings I remember> Chapter 2: Starts on page 3, so \LaTeX<> inserts a ``blank'' page 2. \enddocument>
As shown in the following annotated graphic, chapter 2 starts on page 3 because LaTeX inserted page 2 which is nominally “blank” but contains an unsightly header.
A variety of mechanisms—code and packages—can be used to ensure inserted pages, such as page 2 above, are completely blank. Here are some examples:
\usepackageemptypage>
\clearpage \begingroup \pagestyleempty>\cleardoublepage \endgroup
\newpage\thispagestyleempty>
For more information on, and examples of, using truly blank pages, consult the TUG FAQ or search for questions tagged blank-page on TeX.stackechange.
Sometimes is convenient to change the style of a specific page; for example, removing the header and footer from the current chapter page:
\begindocument> \maketitle \chapterUsing different page styles> \lipsum[1] % via \usepackage \chapterSample Chapter> \thispagestyleempty> % remove headers/footers from the chapter page \lipsum[1] \clearpage \sectionNew section> \lipsum[1] \enddocument>
Headers and footers produced by fancyhdr can contain multiple lines. The following example is based on the fancyhdr documentation but expanded to include a multiline header. This example uses the datetime2 package command \DTMnow to insert the date on which the draft document was typeset. The command \\ is used to terminate the first line of the header.
\documentclassbook> \usepackage[paperheight=16cm, paperwidth=12cm,% Set the height and width of the paper includehead, nomarginpar,% We don't want any margin paragraphs textwidth=10cm,% Set \textwidth to 10cm headheight=25pt,% Set \headheight to 25pt to accommodate the multiline header ]geometry> \usepackagefancyhdr> \usepackagedatetime2> \begindocument> % Set the page style to "fancy". \pagestylefancy> %. then configure it. \fancyhead<> % clear all header fields \fancyhead[RO,LE]\textbfDRAFT> (Typeset \DTMnow)\\\textbfThe performance of new graduates>> \fancyfoot<> % clear all footer fields \fancyfoot[LE,RO]\thepage> \fancyfoot[LO,CE]From: K. Grant> \fancyfoot[CO,RE]To: Dean A. Smith> % Some content: This is page 1.\newpage This is page 2. \enddocument>
The following graphic shows the output produced by this example: