Introduction

Headers and footers are an important aspect of document formatting, as they provide additional information and context to the contents of a page. In LaTeX, headers and footers can be easily customized using the fancyhdr package which will be explored in this section.

Using fancyhdr package

In LaTeX, headers and footers can be created using the fancyhdr package. This package provides a set of commands for customizing the headers and footers, including the ability to specify the contents of the header and footer, the font style, and the horizontal and vertical position.

Here's an example of how to create a simple header and footer using the fancyhdr package:

\documentclass{book}
\usepackage{blindtext}
\usepackage{fancyhdr}
\pagestyle{fancy}

\fancyhead[L]{\LaTeX guides and tutorials}
\fancyhead[R]{TexReady}

\fancyfoot[L]{\today}
\fancyfoot[C]{\thepage}

\begin{document}

\blindtext[5]

\end{document}

This example generates the following output:

headers and footers example

Description:

  • \documentclass{book} declares that the document class is a book.
  • \usepackage{blindtext} imports the blindtext package, which provides dummy text to be used in the document.
  • \usepackage{fancyhdr} imports the fancyhdr package, which provides the capability to customize headers and footers.
  • \pagestyle{fancy} sets the pagestyle of the document to fancy, which enables the customization of headers and footers.
  • \fancyhead[L]{\LaTeX guides and tutorials} sets the left header to the text "LaTeX guides and tutorials".
  • \fancyhead[R]{TexReady} sets the right header to the text "TexReady".
  • \fancyfoot[L]{\today} sets the left footer to the current date using \today command.
  • \fancyfoot[C]{\thepage} sets the center footer to the current page number using \thepage command.

Style customization of headers and footers

The fancyhead and fancyfoot commands in LaTeX accept selectors as arguments to determine the position of the header or footer text on the page:

  • L → left
  • C → center
  • R → right
  • E → even page
  • O → odd page

Here's an example of how you might use each selector in the fancyhead and fancyfoot commands:

\documentclass{book}
\usepackage{blindtext}
\usepackage{fancyhdr}
\pagestyle{fancy}

\fancyhead[L]{Left header}
\fancyhead[C]{Center header}
\fancyhead[R]{Right header}

\fancyfoot[L]{Left footer}
\fancyfoot[C]{Center footer}
\fancyfoot[R]{Right footer}

\begin{document}
\blindtext[5]
\end{document}

This example generates the following output:

headers and footers example

The above example would produce a document with a left, center and right header and footer.

The E and O selectors are used to set different headers or footers for even and odd pages.

For two-sided documents, such as books or double-sided printing, you can specify different headers and footers for odd and even pages. Here's an example:

\documentclass[twoside]{book}
\usepackage{blindtext}
\usepackage{fancyhdr}
\pagestyle{fancy}

\fancyhead[LE,RO]{\leftmark}
\fancyhead[RE,LO]{\thepage}

\fancyfoot[C]{\today}

\begin{document}

\chapter{Introduction}
\section{Section 1}

\blindtext[15]

\end{document}

This example generates the following output:

twoside headers example

Description:

  • \documentclass[twoside]{book} This line specifies that the document is a book and it will be typeset with two-sided layout.

  • \usepackage{fancyhdr} This line loads the fancyhdr package, which provides the necessary tools for creating custom headers and footers.

  • \pagestyle{fancy} This line sets the page style to fancy, which means that the headers and footers will be set according to the specifications provided in the following lines.

  • \fancyhead[LE,RO]{\leftmark} This line sets the current section title to be displayed on the left-hand side of even pages and the right-hand side of odd pages.

  • \fancyhead[RE,LO]{\thepage} This line sets the page number to be displayed on the right-hand side of even pages and the left-hand side of odd pages.

  • \fancyfoot[C]{\today} This line sets the current date to be displayed at the bottom center of every page.

  • \blindtext[15] This line generates dummy text to fill the page.