Simplifying Font Management in XeTeX with fontspec
XeTeX’s system‑font integration via fontspec lets you load OpenType/TrueType fonts directly by name, simplifying font management and improving portability. This guide walks through setting fonts, discovering available families, handling missing fonts, and understanding trade‑offs.
29 Nov 2025, 01:27 UTC

Why System Fonts Matter in XeTeX
When you compile a document with XeTeX, the engine can read OpenType and TrueType fonts straight from your operating system. This means you can reference a font by its familiar name—Times New Roman, Helvetica, Fira Code—instead of juggling TeX’s legacy .tfm and .pfb files. The fontspec package turns this capability into a developer‑friendly interface with commands like \setmainfont and \setsansfont. The result is a document that looks the same on any machine that has the same font family installed, and you can avoid shipping bulky font files with your source.
Getting Started: A Minimal Example
Below is a minimal XeLaTeX source that demonstrates how to load a system font, set its variants, and handle missing fonts gracefully. Replace Times New Roman with any font that is installed on your system.
\documentclass{article}
\usepackage{fontspec}
% Load the main font and its variants
\setmainfont[{
UprightFont = *-Regular,
BoldFont = *-Bold,
ItalicFont = *-Italic,
SmallCapsFont = *-SmallCaps
}]{Times New Roman}
% Fallbacks for missing styles
\defaultfontfeatures{Ligatures = TeX}
\begin{document}
Hello, world! Here is a sample of the main font.
\end{document}
Compile this with xelatex -file-line-error sample.tex. If the font is missing, XeTeX will emit a “font not found” error and fall back to the default font. The \defaultfontfeatures line tells XeTeX to enable TeX‑style ligatures, which is a common typographic preference.
Discovering Available System Fonts
Before you hard‑code a font name, it’s useful to list what is actually available to XeTeX. Two approaches:
- Command line:
xelatex --listfonts sample.texprints a table of font names and file paths. - In‑document:
\fontspeclistfontsprints the same list to the log. Add this line to the preamble:\usepackage{fontspec} \AtBeginDocument{\fontspeclistfonts}
Verify that the desired font appears. On Unix‑like systems, font names are case‑sensitive; Times New Roman is not the same as times new roman.
Fine‑Grained Control with Options
Sometimes you want to override the default variant mapping. fontspec lets you specify exact font files for each style:
\setmainfont[{
BoldFont = {Times New Roman Bold.otf},
ItalicFont = {Times New Roman Italic.otf}
}]{Times New Roman}
When you use full file paths, XeTeX bypasses the system’s font lookup and loads the exact file you point to. This is handy when you have a custom variant that isn’t part of the standard family.
Trade‑Offs and Limitations
- Portability: If a collaborator’s machine lacks
Times New Roman, the PDF will default to a different font, potentially changing line height and overall layout. Use\fallbackfontor\defaultfontfeatures{FakeBold = 0.5}to mitigate drastic shifts. - Licensing: Many system fonts are licensed for local use only. When you embed a font in a PDF, you must confirm that the license permits distribution.
fontspeccan embed fonts automatically, but you can disable embedding withEmbedFont = falseif required. - Performance: Loading many high‑resolution fonts can increase compilation time. Keep the number of loaded families to a minimum.
- Case sensitivity: On Linux and macOS, font names are case‑sensitive. A typo in the font name will cause a silent fallback or a compilation error.
Actionable Checklist
- List system fonts with
xelatex --listfontsand confirm the target font exists. - Use
\setmainfontwith the exact font name; addBoldFont,ItalicFont, etc., to match your design. - Add
\defaultfontfeatures{Ligatures = TeX}if you want traditional ligatures. - Run
xelatex -file-line-errorto catch missing‑font errors early. - Check the PDF visually and with
pdfinfoto ensure the correct font is embedded. - If the font is not available on all target machines, consider bundling the font files or using a web‑friendly family like
Robotothat is widely installed.
By following these steps, you can harness XeTeX’s system‑font integration to produce clean, portable documents without the overhead of managing legacy TeX font files.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.