Understanding Font Systems in LaTeX
By default, LaTeX uses a font family called Computer Modern. While iconic in scientific publishing, you will often want to use different fonts to give your document a specific personality or to comply with branding guidelines.
Historically, changing fonts in LaTeX was a complex process involving font metrics and specialized files. However, with modern LaTeX "engines" like XeLaTeX and LuaLaTeX, you can now use any font installed on your operating system (OpenType or TrueType) with ease.
In this guide, we will focus primarily on the modern approach using the fontspec package, while briefly touching upon the traditional methods for those using the standard pdfLaTeX engine.
The Modern Approach: Using fontspec
The easiest way to manage fonts today is by using the fontspec package. This package acts as an interface to the fonts already installed on your computer.
Important: To use the
fontspecpackage, you must compile your document using XeLaTeX or LuaLaTeX. Standard pdfLaTeX will not work with this method.
Setting Up Your Document
To begin, you need to load the package and then define which fonts you want to use for the three main categories of text:
- Main Font (Serif): Used for the bulk of your body text.
- Sans Font (Sans-Serif): Often used for headings or UI elements.
- Mono Font (Monospace): Used for code snippets or URLs.
Basic Example
Here is how you would set up a document using common system fonts:
\documentclass{article}
% Load the fontspec package
\usepackage{fontspec}
% Set the main body font
\setmainfont{Arial}
% Set the sans-serif font
\setsansfont{Verdana}
% Set the monospace (code) font
\setmonofont{Courier New}
\begin{document}
This is the main body text in Arial.
\textsf{This text appears in Verdana (Sans-Serif).}
\texttt{This text appears in Courier New (Monospace).}
\end{document}Advanced Customization with fontspec
One of the most powerful features of fontspec is the ability to fine-tune how fonts behave. You can specify different files for bold or italic versions if the automatic detection fails, or enable specific OpenType features.
Selecting Specific Font Styles
Sometimes a font family has many variations (Light, Extra Bold, Condensed). You can specify exactly which ones to use:
\setmainfont{Helvetica Neue}[
UprightFont = {* Light},
BoldFont = {* Bold},
ItalicFont = {* Light Italic},
BoldItalicFont = {* Bold Italic}
]The asterisk (*) is a wildcard that represents the font name "Helvetica Neue."
OpenType Features
Modern fonts often include hidden features like "OldStyle" numbers (where numbers have varying heights) or "Ligatures" (where characters like 'fi' merge beautifully).
\setmainfont{Palatino}[
Numbers = OldStyle,
Ligatures = TeX
]Tip: If you aren't sure what the exact name of a font is on your system, you can find it in your "Font Book" (macOS) or "Fonts Settings" (Windows). The name you type in LaTeX must match the system name exactly.
The Traditional Approach: pdfLaTeX
If you are required to use the pdfLaTeX engine (which is common for some journal submissions), you cannot simply call any font from your computer. Instead, you must use "font packages" that have been specifically prepared for LaTeX.
Using Pre-packaged Fonts
To change fonts in pdfLaTeX, you load a package that replaces the defaults. Common choices include:
lmodern: Latin Modern (an improved version of the default)helvet: Helvetica (Sans-serif)charter: Bitstream Charter (Serif)fourier: Utopia
Example with pdfLaTeX
\documentclass{article}
\usepackage[T1]{fontenc} % Recommended for better character support
\usepackage[utf8]{inputenc} % Standard encoding
% Load font packages
\usepackage{charter} % Sets the main serif font to Charter
\usepackage[scaled]{helvet} % Sets sans-serif to Helvetica
\begin{document}
This body text is now in Charter.
\textsf{This text is in Helvetica.}
\end{document}Changing Fonts for Specific Parts of Text
You don't always want to change the font for the entire document. Sometimes you just need a different font for a specific word or section.
Defining Font Families
You can define a new font "family" and call it whenever you like:
\usepackage{fontspec}
\newfontfamily\headerfont{Georgia}
\newfontfamily\codefont{Consolas}
\begin{document}
Normal body text.
{\headerfont This specific line is in Georgia.}
Back to normal text.
\end{document}Best Practices and Common Pitfalls
Choosing and implementing fonts can significantly impact the readability and professional look of your document. Here are some guidelines to keep in mind:
1. Match Your Engine to Your Method
The most common error for beginners is trying to use \setmainfont while compiling with pdfLaTeX. If you get an error saying fontspec.sty not found or Fatal error: Check your engine, double-check that you are using XeLaTeX or LuaLaTeX.
2. Stick to Two (or Three) Fonts
Using too many different fonts makes a document look cluttered and unprofessional. A classic "safe" pairing is a Serif font for body text and a Sans-Serif font for headings.
3. Check for Math Support
Not all fonts come with mathematical symbols. If your document is math-heavy, consider using the unicode-math package (for XeLaTeX/LuaLaTeX) or specialized math font packages like newtxmath.
4. Provide Font Files if Sharing
If you send your .tex file to a collaborator, they must have the same fonts installed on their computer to compile it.
Note: If you are using an online editor like Typevia, you may need to upload the
.ttfor.otffont files to your project folder and reference them by filename (e.g.,\setmainfont{myfont.ttf}) to ensure the document compiles correctly in the cloud.
5. Font Encoding
When using pdfLaTeX, always include \usepackage[T1]{fontenc}. This ensures that accented characters are handled correctly and that you can copy-paste text containing such characters from the resulting PDF.
