LaTeX/Customizing Page Headers and Footers

From Wikibooks, open books for an open world
Jump to: navigation, search

Page styles in Latex terms refers not to page dimensions, but to the running headers and footers of a document. These headers typically contain document titles, chapter or section numbers/names, and page numbers.

Standard page styles[edit]

The possibilities of changing the headers in plain Latex are actually quite limited. There are two commands available: \pagestyle{''style''} will apply the specified style to the current and all subsequent pages, and \thispagestyle{''style''} will only affect the current page. The possible styles are:

empty Both header and footer are cleared
plain Header is clear, but the footer contains the page number in the center.
headings Footer is blank, header displays information according to document class (e.g., section name) and page number top right.
myheadings Page number is top right, and it is possible to control the rest of the header.

The commands \markright and \markboth can be used to set the content of the headings by hand. The following commands placed at the beginning of an article document will set the header of all pages (one-sided) to contain "John Smith" top left, "On page styles" centered and the page number top right:

\markright{John Smith\hfill On page styles\hfill}

There are special commands containing details on the running page of the document.

\thepage number of the current page
\leftmark current chapter name printed like "CHAPTER 3. THIS IS THE CHAPTER TITLE"
\rightmark current section name printed like "1.6. THIS IS THE SECTION TITLE"
\chaptername the name chapter in the current language. If this is English, it will display "Chapter"
\thechapter current chapter number
\thesection current section number

Note that \leftmark and \rightmark convert the names to uppercase, whichever was the formatting of the text. If you want them to print the actual name of the chapter without converting it to uppercase use the following command:

\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }
\renewcommand{\sectionmark}[1]{ \markright{#1}{} }

Now \leftmark and \rightmark will just print the name of the chapter and section, without number and without affecting the formatting. Note that these redefinitions must be inserted after the first call of \pagestyle{fancy}. The standard book formatting of the \chaptermark is:

\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{\chaptername\ \thechapter.\ #1}}{}}

Watch out: if you provide long text in two different "parts" only in the footer or only in the header, you might see overlapping text.

Moreover, with the following commands you can define the thickness of the decorative lines on both the header and the footer:


The first line for the header, the second for the footer. Setting it to zero means that there will be no line.

Plain pages issue[edit]

An issue to look out for is that the major sectioning commands (\part, \chapter or \maketitle) specify a \thispagestyle{plain}. So, if you wish to suppress all styles by inserting a \pagestyle{empty} at the beginning of your document, then the style command at each section will override your initial rule, for those pages only. To achieve the intended result one can follow the new section commands with \thispagestyle{empty}. The \part command, however, cannot be fixed this way, because it sets the page style, but also advances to the next page, so that \thispagestyle{} cannot be applied to that page. Two solutions:

  • simply write \usepackage{nopageno} in the preamble. This package will make \pagestyle{plain} have the same effect as \pagestyle{empty}, effectively suppressing page numbering when it is used.
  • Use fancyhdr as described below.

The tricky problem when customizing headers and footers is to get things like running section and chapter names in there. Standard LaTeX accomplishes this with a two-stage approach. In the header and footer definition, you use the commands \rightmark and \leftmark to represent the current section and chapter heading, respectively. The values of these two commands are overwritten whenever a chapter or section command is processed. For ultimate flexibility, the \chapter command and its friends do not redefine \rightmark and \leftmark themselves. They call yet another command (\chaptermark, \sectionmark, or \subsectionmark) that is responsible for redefining \rightmark and \leftmark, except if they are starred -- in such a case, \markboth{Chapter/Section name}{} must be used inside the sectioning command if header and footer lines are to be updated.

Again, several packages provide a solution:

  • an alternative one-stage mechanism is provided by the package titleps);
  • fancyhdr will handle the process its own way.

Customizing with fancyhdr[edit]

To get better control over the headers, one can use the package fancyhdr written by Piet van Oostrum. It provides several commands that allow you to customize the header and footer lines of your document. For a more complete guide, the author of the package produced this documentation.

To begin, add the following lines to your preamble:


You can now observe a new style in your document.

The \headheight needs to be 13.6pt or more, otherwise you will get a warning and possibly formatting issues. Both the header and footer comprise three elements each according to its horizontal position (left, centre or right).

The styles supported by fancyhdr:

  • the four LaTeX styles;
  • fancy defines a new header for all pages but plain-style pages such as chapters and titlepage;
  • fancyplain is the same, but for absolutely all pages.

Style customization[edit]

The styles can be customized with fancyhdr specific commands. Those two styles may be configured directly, whereas for LaTeX styles you need to make a call to the \fancypagestyle command.

To set header and footer style, fancyhdr provides three interfaces. They all provide the same features, you just use them differently. Choose the one you like most.

  • You can use the following six commands.
\lhead[<even output>]{<odd output>}
\chead[<even output>]{<odd output>}
\rhead[<even output>]{<odd output>}
\lfoot[<even output>]{<odd output>}
\cfoot[<even output>]{<odd output>}
\rfoot[<even output>]{<odd output>}

Hopefully, the behaviour of the above commands is fairly intuitive: if it has head in it, it affects the head etc, and obviously, l, c and r means left, centre and right respectively.

  • You can also use the command \fancyhead for header and \fancyfoot for footer. They work in the same way, so we'll explain only the first one. The syntax is:
\fancyhead[selectors]{output you want}

You can use multiple selectors optionally separated by a comma. The selectors are the following:

E even page
O odd page
L left side
C centered
R right side

so CE,RO will refer to the center of the even pages and to the right side of the odd pages.

  • \fancyhf is a merge of \fancyhead and \fancyfoot, hence the name. There are two additional selectors H and F to specify the header or the footer, respectively. If you omit the H and the F, it will set the fields for both.

These commands will only work for fancy and fancyplain. To customize LaTeX default style you need the \fancyplainstyle command. See below for examples.

For a clean customization, we recommend you start from scratch. To do so you should erase the current pagestyle. Providing empty values will make the field blank. So


will just delete the current heading/footer configuration, so you can make your own.

Plain pages[edit]

There are two ways to change the style of plain pages like chapters and titlepage.

First you can use the fancyplain style. If you do so, you can use the command \fancyplain{...}{...} inside fancyhdr commands like \lhead{...}, etc.

When LaTeX wants to create a page with an empty style, it will insert the first argument of \fancyplain, in all the other cases it will use the second argument. For instance:

\lhead{ \fancyplain{}{Author Name} }
\rhead{ \fancyplain{}{\today} }
\rfoot{ \fancyplain{}{\thepage} }

It has the same behavior of the previous code, but you will get empty header and footer in the title and at the beginning of chapters.

Alternatively you could redefine the plain style, for example to have a really plain page when you want. The command to use is \fancypagestyle{plain}{...} and the argument can contain all the commands explained before. An example is the following:


\fancypagestyle{plain}{ %
  \fancyhf{} % remove everything
  \renewcommand{\headrulewidth}{0pt} % remove lines as well

In that case you can use any style but fancyplain because it would override your redefinition.


For two-sided, it's common to mirror the style of opposite pages, you tend to think in terms of inner and outer. So, the same example as above for two-sided is:

\lhead[Author Name]{}
\rhead[]{Author Name}

This is effectively saying author name is top outer, today's date is top inner, and current page number is bottom outer. Using \fancyhf can make it shorter:

\fancyhf[HLE,HRO]{Author's Name}

Here is the complete code of a possible style you could use for a two-sided document:


\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }
\renewcommand{\sectionmark}[1]{ \markright{#1} }

\fancyhead[RE]{\textit{ \nouppercase{\leftmark}} }
\fancyhead[LO]{\textit{ \nouppercase{\rightmark}} }

\fancypagestyle{plain}{ %
  \fancyhf{} % remove everything
  \renewcommand{\headrulewidth}{0pt} % remove lines as well

Using \fancypagestyle one can additionally define multiple styles for one's document that are easy to switch between. Here's a somewhat complicated example for a two-sided book style:

    % Note the ## here. It's required because \fancypagestyle is making a macro (\ps@fancybook).
    % If we just wrote #1, TeX would think that it's the argument to \ps@fancybook, but
    % \ps@fancybook doesn't take any arguments, so TeX would complain with an error message.
    % You are not expected to understand this.
    \renewcommand*{\sectionmark}[1]{ \markright{\thesection\ ##1} }%
    \renewcommand*{\chaptermark}[1]{ \markboth{\chaptername\ \thechapter: ##1}{} }%
    % Increase the length of the header such that the folios 
    % (typography jargon for page numbers) move into the margin
    \fancyhfoffset[LE]{6mm}% slightly less than 0.25in
    % Put some space and a vertical bar between the folio and the rest of the header

Page n of m[edit]

Some people like to put the current page number in context with the whole document. LaTeX only provides access to the current page number. However, you can use the lastpage package to find the total number of pages, like this:

\cfoot{\thepage\ of \pageref{LastPage} }

Note the capital letters. Also, add a backslash after \thepage to ensure adequate space between the page number and 'of'. And recall, when using references, that you have to run LaTeX an extra time to resolve the cross-references.

Alternative packages[edit]

Other packages for page styles are scrpage2, very similar to fancyhdr, and titleps, which takes a one-stage approach, without having to use \leftmark or \rightmark.