Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 38 ยท Packages and Templates

Page Layout, Headers, Footers and Fonts

Control margins with geometry, headers and footers with fancyhdr, line spacing, colours and fonts in LaTeX, including pdfLaTeX versus XeLaTeX font choices.

Default LaTeX pages are designed for readability: wide margins, good line length, numbered pages. Often you still need to follow a template: narrower margins, running headers, double spacing, a university font. Four packages cover most of it: geometry, fancyhdr, setspace and the font packages.

Margins and paper with geometry

LATEX
\usepackage[
  a4paper,
  top=2.5cm, bottom=2.5cm,
  left=3cm, right=2cm,
  headheight=14pt,
]{geometry}

% or simply
\usepackage[a4paper, margin=2.5cm]{geometry}
\usepackage[letterpaper, landscape, margin=1in]{geometry}

Other useful options: twoside with inner=3cm, outer=2cm for books, bindingoffset=1cm for stapled pages, showframe to draw the layout boxes while you debug, and includehead / includefoot to count the header and footer inside the text area. You can change the layout mid-document with \newgeometry{...} and return with \restoregeometry.

The stand-in below calculates the resulting text area for a page, which is the arithmetic geometry does when you set margins.

JS Text area calculator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Headers and footers with fancyhdr

LATEX
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}                                  % clear all fields
\fancyhead[L]{\leftmark}                    % chapter or section name, left
\fancyhead[R]{\thepage}                     % page number, right
\fancyfoot[C]{\small My Thesis -- Draft}
\renewcommand{\headrulewidth}{0.4pt}        % line under header
\renewcommand{\footrulewidth}{0pt}

\fancypagestyle{plain}{                     % first pages of chapters
  \fancyhf{}
  \fancyfoot[C]{\thepage}
  \renewcommand{\headrulewidth}{0pt}
}

Position letters: L, C, R for left, centre, right, and E/O for even and odd pages in two-sided documents (\fancyhead[LE,RO]{\thepage}). \leftmark and \rightmark hold the current chapter and section titles; \nouppercase stops the default capitalisation. If LaTeX warns "\headheight is too small", set headheight in geometry (14pt or more).

For a title page without header use \thispagestyle{empty}. Page numbering style is changed with \pagenumbering{roman} for front matter and \pagenumbering{arabic} afterwards; \setcounter{page}{1} restarts.

Spacing

LATEX
\usepackage{setspace}
\onehalfspacing          % or \doublespacing, \singlespacing
\setstretch{1.15}        % custom

\setlength{\parindent}{0pt}
\setlength{\parskip}{0.8em}   % blank line between paragraphs instead of indent

\usepackage{parskip}     % does the same with sensible defaults

Wrap block quotes and tables in \begin{singlespace}...\end{singlespace} when a thesis template demands double spacing for the body only. \linespread{1.3} is a blunt alternative that also affects footnotes.

Colour and visual touches

LATEX
\usepackage[dvipsnames]{xcolor}
\definecolor{brand}{HTML}{1A73E8}
\textcolor{brand}{Important}
\colorbox{yellow!30}{highlighted}

Use titlesec to restyle headings:

LATEX
\usepackage{titlesec}
\titleformat{\section}{\Large\bfseries\color{brand}}{\thesection}{1em}{}
\titlespacing*{\section}{0pt}{2ex}{1ex}

Fonts

The right approach depends on the engine.

pdfLaTeX uses font packages:

LATEX
\usepackage[T1]{fontenc}
\usepackage{lmodern}            % scalable Latin Modern
\usepackage{libertinus}         % or palatino, mathpazo, newtxtext + newtxmath
\renewcommand{\familydefault}{\sfdefault}   % sans-serif body

XeLaTeX and LuaLaTeX use any installed OpenType or TrueType font through fontspec:

LATEX
\usepackage{fontspec}
\setmainfont{Libertinus Serif}
\setsansfont{Inter}
\setmonofont{JetBrains Mono}[Scale=MatchLowercase]

In math, keep text and math fonts matched: pair mathpazo with Palatino, newtxmath with Times-like text, or use unicode-math with XeLaTeX and LuaLaTeX.

Switch inline with \textbf{}, \textit{}, \textsc{}, \texttt{}, \textsf{}, or declaratively with \bfseries, \itshape, \scshape, \ttfamily. Sizes run from \tiny through \normalsize to \Huge. For arbitrary sizes use \fontsize{12}{14}\selectfont (size and baseline skip in points).

Quick fixes

  • Text too close to the edge: increase margin; use showframe to see the boxes.
  • Header overlaps the text: raise headheight and top.
  • Page numbers missing on chapter openers: redefine the plain page style.
  • Wrong font in a template: check which engine the template expects.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which geometry option draws the layout boxes for debugging?

    showframe draws the text area, header and footer boxes so you can see your margins.
  2. 2. How do you place the page number at the right of the header on every page with fancyhdr?

    \fancyhead[R] sets the right field of the header; \thepage prints the page number.
  3. 3. Which package lets XeLaTeX or LuaLaTeX use any installed system font?

    fontspec provides \setmainfont and friends for OpenType and TrueType fonts.
  4. 4. How do you get double line spacing for the body?

    setspace provides \singlespacing, \onehalfspacing and \doublespacing.

Summary

  • geometry sets paper, margins, binding offset and two-sided layouts.
  • fancyhdr defines headers and footers with L/C/R and E/O positions; set headheight to avoid warnings.
  • setspace, \parskip and \parindent adjust vertical spacing.
  • xcolor and titlesec restyle colours and headings.
  • pdfLaTeX picks fonts via packages; XeLaTeX/LuaLaTeX use fontspec.

Next: Document Templates