Understanding Modern Font Engines: Beyond pdfLaTeX
For decades, LaTeX users were largely limited to the built-in "Computer Modern" font or a specific set of PostScript fonts. Using a custom font (like one you downloaded from the internet) required a complex process of converting files into formats that the traditional pdfLaTeX engine could understand.
With the advent of XeLaTeX and LuaLaTeX, this has changed. These modern engines allow you to use any font installed on your operating system (OpenType and TrueType) directly in your documents.
Why Use Modern Engines?
- Access to System Fonts: Use fonts like Arial, Helvetica, Georgia, or professional foundry fonts without complex installation.
- OpenType Features: Easily toggle advanced typography features like ligatures, old-style figures, and small caps.
- Native UTF-8 Support: These engines handle international characters and symbols (Unicode) natively, making multilingual typesetting much easier.
- Simplified Code: You no longer need packages like
inputencorfontenc.
Note: To follow this guide, ensure your LaTeX editor (like TeXstudio, Typevia, or VS Code) is set to compile using the XeLaTeX or LuaLaTeX engine instead of the default pdfLaTeX.
The Core Package: fontspec
The key to unlocking modern fonts is the fontspec package. This package provides the interface for selecting fonts and configuring their features. It is designed specifically for XeLaTeX and LuaLaTeX.
Basic Setup
In your document preamble, you simply load the package and use the \setmainfont command.
\documentclass{article}
\usepackage{fontspec}
% Replace "Arial" with any font installed on your computer
\setmainfont{Arial}
\begin{document}
This text is rendered in Arial. Modern LaTeX makes font selection as easy as a word processor!
\end{document}By default, fontspec will look for the font name in your system’s font library. If you have "Times New Roman" or "Helvetica" installed, you can call them by their exact names.
Setting Up Document Fonts
A professional document usually consists of three font families: Serif (the main body text), Sans-Serif (often for headings), and Monospaced (for code or technical data).
Here is how you define all three using fontspec:
\documentclass{article}
\usepackage{fontspec}
% Main serif font for body text
\setmainfont{EB Garamond}
% Sans-serif font for \sffamily
\setsansfont{Libertinus Sans}
% Monospaced font for \texttt
\setmonofont{Fira Code}[Scale=0.9]
\begin{document}
This is the main body text in EB Garamond.
{\sffamily This is a sans-serif heading or note.}
\texttt{print("This is monospaced code")}
\end{document}Loading Fonts by Filename
Sometimes, a font isn't "installed" on your system, but the .otf or .ttf files are located in the same folder as your LaTeX project (common in Typevia or portable projects). You can load these by filename:
\setmainfont{MyCustomFont.otf}[
Path = ./fonts/,
Extension = .otf,
UprightFont = *-Regular,
BoldFont = *-Bold,
ItalicFont = *-Italic,
BoldItalicFont = *-BoldItalic
]Leveraging OpenType Features
One of the greatest advantages of modern engines is the ability to use "OpenType features." Many professional fonts include multiple styles for numbers or special character combinations (ligatures).
Common Font Options
When you load a font, you can pass options in square brackets:
- Numbers: Use
Numbers=OldStylefor numbers that blend into text, orNumbers=Liningfor modern, uniform-height numbers. - Ligatures: Use
Ligatures=TeXto ensure standard LaTeX shortcuts (like--for an en-dash) still work. - Small Caps: Most modern fonts handle
\textsc{}automatically, but you can force specific behaviors.
\documentclass{article}
\usepackage{fontspec}
% Using Adobe Caslon Pro with specific features
\setmainfont{Libertinus Serif}[
Numbers = {OldStyle, Proportional},
Ligatures = TeX
]
\begin{document}
In 2023, we found that 123456789 looks better in OldStyle numbers.
Compare this to standard lining numbers: {\addfontfeature{Numbers=Lining} 123456789}.
\end{document}XeLaTeX vs. LuaLaTeX: Which to Choose?
While both engines work with fontspec, they have slight differences:
- XeLaTeX: Generally faster at finding and loading system fonts. It is very stable and has been the "standard" for modern fonts for a long time.
- LuaLaTeX: The newer successor. It is slightly slower but much more powerful because it allows you to use the Lua programming language to extend LaTeX's capabilities. It handles complex layouts and large font collections more robustly.
Best Practice: For most beginners, either will work. If you are doing complex mathematical typesetting alongside custom fonts, LuaLaTeX is often recommended by the modern LaTeX community.
Common Pitfalls and Troubleshooting
1. "Font not found" Error
This is the most common issue.
- Check the Spelling: The name must exactly match the name shown in your "Font Book" (macOS) or "Fonts" settings (Windows).
- Check the Engine: Ensure you aren't accidentally running
pdfLaTeX. - Refresh Cache: On Linux or Windows, you may need to run
fc-cachein your terminal to update the system's font list.
2. Missing Bold or Italics
Not all fonts come with bold or italic variants. If you choose a font that only has a "Regular" weight, LaTeX will try to "fake" it or simply throw a warning.
Tip: Always check if your chosen font family includes
-Bold,-Italic, and-BoldItalicfiles.
3. Math Fonts
fontspec only changes the text fonts. Your math equations will still appear in the default Computer Modern style. To change math fonts to match your text, you should use the unicode-math package.
\usepackage{unicode-math}
\setmainfont{Latin Modern Roman}
\setmathfont{Latin Modern Math}Summary Checklist
To use modern fonts in your next project:
- Switch your compiler to XeLaTeX or LuaLaTeX.
- Include
\usepackage{fontspec}in your preamble. - Use
\setmainfont{Font Name}to select your primary typeface. - Remove legacy packages like
inputencandfontenc. - Enjoy beautiful, modern typography!
