Typevia
Text Formatting

Understanding Page Styles in LaTeX

Headers and footers are the areas at the top and bottom of a page, respectively. They are essential for providing context to the reader, such as the document title, author name, section heading, or page number.

In LaTeX, the way a page looks is governed by a "page style." By default, LaTeX provides a few built-in styles:

  • plain: The default style for most documents. It features a centered page number at the bottom (footer) and an empty header.
  • empty: Both the header and footer are completely blank.
  • headings: The header contains the current chapter/section title and the page number, while the footer is empty.

To apply a style globally, you use the \pagestyle{style_name} command in your preamble. To change the style for a single page only, you use \thispagestyle{style_name} within the document body.

\documentclass{article}

% Apply the empty style to the entire document
\pagestyle{empty}

\begin{document}
This page and all subsequent pages will have no headers or footers.
\newpage
Still empty!
\end{document}

Introducing the fancyhdr Package

While the default styles are functional, they are very rigid. For professional documents, you will almost certainly want to use the fancyhdr package. This is the industry standard for creating custom, complex headers and footers.

To get started, add the following to your preamble:

\usepackage{fancyhdr}
\pagestyle{fancy}

Once fancyhdr is activated, the page is divided into three sections for the header and three for the footer: Left, Center, and Right.


Basic Customization

The most common way to define content is by using the following six commands:

  • Header commands: \lhead{}, \chead{}, \rhead{}
  • Footer commands: \lfoot{}, \cfoot{}, \rfoot{}

Here is a practical example of a document with a custom header and footer:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{} % Clear all default header and footer fields

\lhead{Introduction to LaTeX}
\chead{}
\rhead{Tutorial Series}

\lfoot{Author: Jane Doe}
\cfoot{\thepage} % \thepage inserts the current page number
\rfoot{Draft v1.0}

\begin{document}
\section{The First Page}
Content goes here...
\end{document}

Note: Always use \fancyhf{} before defining your custom headers. This clears any default settings (like the centered page number from the plain style) and gives you a blank canvas to work on.


Modern Syntax: \fancyhead and \fancyfoot

While \lhead and \rhead are easy to remember, modern LaTeX users prefer the more powerful \fancyhead and \fancyfoot commands. These commands use "selectors" to define where the text should go.

The selectors are:

  • E for Even pages
  • O for Odd pages
  • L for Left side
  • C for Center
  • R for Right side

For a simple one-sided document (like a standard report), you just use the position selectors:

\fancyhead[L]{Left Header Content}
\fancyhead[R]{Right Header Content}
\fancyfoot[C]{\thepage}

Handling Two-Sided Documents (Books)

In a book or a double-sided report, headers usually mirror each other. For example, you might want the page number on the outer edge of every page.

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

% Page number on the Left of Even pages and Right of Odd pages (the outer edges)
\fancyhead[LE,RO]{\thepage}

% Chapter name on the Right of Even pages and Section name on Left of Odd pages
\fancyhead[RE]{\leftmark}
\fancyhead[LO]{\rightmark}

\begin{document}
\chapter{Getting Started}
Content...
\end{chapter}

In this example, \leftmark usually refers to the Chapter title, and \rightmark refers to the Section title.


Decorative Lines and Styling

By default, fancyhdr places a horizontal line (a "rule") between the header and the body text. You can customize the thickness of this line or add a line to the footer.

To change the thickness, redefine the \headrulewidth and \footrulewidth commands using \renewcommand:

\renewcommand{\headrulewidth}{1pt} % Thick header line
\renewcommand{\footrulewidth}{0.5pt} % Thin footer line

% To remove the header line entirely, set it to 0pt
% \renewcommand{\headrulewidth}{0pt}

You can also style the text within the headers using standard LaTeX formatting:

\fancyhead[L]{\textit{Confidential Document}}
\fancyhead[R]{\textbf{\today}}

Common Challenges and Best Practices

1. The "First Page" Problem

In many document classes (like report or book), the first page of a chapter automatically resets the page style to plain. This means your fancy header will disappear on chapter starts. To fix this, you can redefine the plain style or force the fancy style:

% To force fancy style on a specific page:
\thispagestyle{fancy}

% OR, better yet, redefine the 'plain' style in your preamble:
\fancypagestyle{plain}{
    \fancyhf{}
    \fancyfoot[C]{\thepage}
    \renewcommand{\headrulewidth}{0pt}
}

2. Header Height Issues

If your header contains multiple lines or an image, LaTeX might give you a warning: Package Fancyhdr Warning: \headheight is too small. To fix this, use the geometry package to increase the space allocated for the header:

\usepackage[headheight=110pt]{geometry}

3. Best Practices

  • Consistency: Don't clutter the header. Keep essential navigation (like page numbers) in a consistent spot throughout the document.
  • Margins: If you have very wide headers, ensure your page margins (set via the geometry package) are wide enough to accommodate them without looking cramped.
  • Images: You can include small logos in headers using \includegraphics. Just ensure they are scaled appropriately: \rhead{\includegraphics[width=2cm]{logo.png}}.

4. Common Mistake: Forgetting \pagestyle{fancy}

The most common mistake beginners make is loading the package but forgetting to actually tell LaTeX to use it. Without \pagestyle{fancy}, your custom \fancyhead commands will be ignored.