LaTeX/Customizing Page Headers and Footers

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:

\pagestyle{myheadings}
\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:

\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}

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:

\usepackage{fancyhdr}
\setlength{\headheight}{15.2pt}
\pagestyle{fancy}

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 (this page style is deprecated, for new documents a combination of fancy and \fancypagestyle should be used instead, see section 26 of the manual).

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 (these commands are deprecated, use the new ones defined below).
\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

\fancyhf{}

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:

\pagestyle{fancyplain}
\fancyhf{}
\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:

\pagestyle{fancy}

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

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

Examples

edit

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}
\lhead[]{\today}
\rhead[\today]{}
\lfoot[\thepage]{}
\rfoot[]{\thepage}

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}
\fancyhf[HRE,HLO]{\today}
\fancyhf[FLE,FRO]{\thepage}

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

\usepackage{fancyhdr}
\setlength{\headheight}{15pt}

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

\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\textit{ \nouppercase{\leftmark}} }
\fancyhead[LO]{\textit{ \nouppercase{\rightmark}} }

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

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:

\fancypagestyle{fancybook}{%
    \fancyhf{}%
    % 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
    \fancyhfoffset[RO]{6mm}%
    % Put some space and a vertical bar between the folio and the rest of the header
    \fancyhead[LE]{\thepage\hskip3mm\vrule\hskip3mm\leftmark}%
    \fancyhead[RO]{\rightmark\hskip3mm\vrule\hskip3mm\thepage}%
}

Customizing with scrlayer-scrpage

edit

Package scrlayer-scrpage is part of the KOMA-script bundle. Using this package to customize the headers and footers is recommended with every KOMA-script class. The package can be used with the standard classes as well.

\documentclass{book}
\usepackage{scrlayer-scrpage}
\usepackage{mwe}
\begin{document}
\chapter{Manta Ray}
\blindtext
\section{Taxonomy}
\blindtext[10]
\chapter{Mobula}
\blindtext
\section{Life style}
\blindtext[10]
\end{document}

Just loading the package doesn't change the default behavior. Chapter titles are on left hand pages, the section titles on right hand pages. The page number appears in the outer head.

Now, what can we do to customize the headers and footers?

We can use the commands that are available with the package, they are described in more detail in the package documentation.

Following a few examples that may be needed in real documents.

edit
\documentclass{book}
\usepackage{mwe}
\usepackage[markcase=noupper% remove the uppercasing
]{scrlayer-scrpage}
\ohead{}% clear the outer head
\cfoot*{\pagemark}% the pagenumber in the center of the foot, also on plain pages
\begin{document}
\chapter{Manta Ray}
\blindtext
\section{Taxonomy}
\blindtext[10]
\chapter{Mobula}
\blindtext
\section{Life style}
\blindtext[10]
\end{document}

Both, header and footer are separated in an inner, center, and outer part, which can be set independently. The asterisk defines the same content to be on plain pages as well. Usually, pages on which a chapter starts, use the plain style.

How can I have my name and title of my thesis in the inner foot?

edit
\documentclass{book}
\usepackage{mwe}
\usepackage[markcase=noupper% remove the uppercasing
]{scrlayer-scrpage}
\ohead{}% clear the outer head
\ofoot*{\pagemark}% the pagenumber in the outer part of the foot, also on plain pages
\ifoot*{Walter Wombat\\ Is life on Mars possible?}% Name and title beneath each other in the inner part of the foot
\setlength{\footheight}{24.0pt}
\begin{document}
\chapter{Manta Ray}
\blindtext
\section{Taxonomy}
\blindtext[10]
\chapter{Mobula}
\blindtext
\section{Life style}
\blindtext[10]
\end{document}


How to change to font style in headers and footers?

edit

The package provides an interface similar to the KOMA-script classes. You can add font attribute to the header and footer. The page number can be set independently.


\documentclass{book}
\usepackage{mwe}
\usepackage{xcolor}
\usepackage[markcase=noupper% remove the uppercasing
]{scrlayer-scrpage}
\ohead{}% clear the outer head
\addtokomafont{pagehead}{\sffamily}
\addtokomafont{pagefoot}{\tiny}% Making the foot extra tiny to demonstrate
\addtokomafont{pagenumber}{\large\bfseries\color{red!80!black}</!---->}% that the page number can be controlled on its own. 
\ofoot*{\pagemark}% the pagenumber in the outer part of the foot, also on plain pages
\ifoot*{Walter Wombat\\ Is life on Mars possible?}% Name and title beneath each other in the inner part of the foot
\setlength{\footheight}{24.0pt}
\begin{document}
\chapter{Manta Ray}
\blindtext
\section{Taxonomy}
\blindtext[10]
\chapter{Mobula}
\blindtext
\section{Life style}
\blindtext[10]
\end{document}

 

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:

\usepackage{lastpage}
...
\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.

Customizing with titleps

edit

titleps takes a one-stage approach, without having to use \leftmark or \rightmark.

For example:

\newpagestyle{main}{
   \sethead[\thepage][\chaptertitle][(\thesection]  % even
           {\thesection)}{\sectiontitle}{\thepage}} % odd
\pagestyle{main}

defines a pagestyle named main with the page at the left on even pages and at the right on odd pages, the chapter title at the center of even pages, and so on.

With titleps all the format is done in the page style, and not partly when the mark is emitted and partly when the mark is retrieved. The following example is similar to the previous one, but elements are coloured:

\usepackage[dvipsnames,usenames]{color}
  \usepackage{titleps}
  \newpagestyle{main}[\small]{
    \setheadrule{.55pt}%
    \sethead[\colorbox{black}{\color{white}{\thepage}}]%  even-left
            [\chaptertitle]%                              even-center
            [\colorbox{CornflowerBlue}{\thesection}]%     even-right
            {\colorbox{CornflowerBlue}{\thesection}}%     odd-left
            {\sectiontitle}%                              odd-center
            {\colorbox{black}{\color{white}{\thepage}}}%  odd-right
  }