Simplifying Font Selection In Latex Documents

The Font Selection Problem

Selecting appropriate fonts for a LaTeX document can be a challenging task for new and experienced users alike. With hundreds of fonts available across various packages, understanding how to properly load, set up and apply fonts can involve a steep learning curve. Common issues faced include fonts not displaying correctly, font warnings cluttering the compile log, incompatible fonts used in text and math modes causing formatting issues, and multilingual documents requiring specialized font selections per language.

This article aims to simplify working with fonts in LaTeX by explaining the core packages and commands used for font selection, demonstrating best practices for picking compatible text and math fonts, providing troubleshooting tips for font issues, and giving guidance on font choices for multilingual documents.

Common Font Packages and Their Usage

There are a few core LaTeX packages used to load external fonts in LaTeX documents:

  • fontspec – Used to load OpenType and TrueType fonts, provides advanced font selection controls
  • mathfont – Used to set math fonts and math symbols independently of text fonts
  • unicode-math – Used to load OpenType math fonts, designed for Unicode engines like XeLaTeX and LuaLaTeX

Here is an example of loading the popular Open Sans font using the fontspec package under XeLaTeX:

\usepackage{fontspec} 
\setmainfont{Open Sans}

And here is an example of loading the Latin Modern math font using the unicode-math package:

\usepackage{unicode-math}
\setmathfont{Latin Modern Math}

As seen above, the fontspec package provides the \setmainfont command to define the default text font for a document, while unicode-math provides \setmathfont to set a separate math font.

Font Attributes and LaTeX Commands

LaTeX provides font attribute commands for easily switching font styles within the document text:

  • \emph{text} – Sets text in italic style
  • \textbf{text} – Sets text in bold style
  • \textsc{text} – Sets text in small caps style

In addition, the fontspec package introduces supplementary font commands:

  • \fontfamily{family name} – Changes the font family
  • \fontseries{series name} – Changes the font weight/series
  • \fontshape{shape name} – Changes the font shape (italic/oblique)

For example, to change to medium weight italics within a paragraph:

Open Sans supports a range of \fontseries{medium}\fontshape{it} font styles\fontseries{regular}\fontshape{upright}.

Specifying Fonts for Text and Math

When selecting fonts for LaTeX documents, it is important to choose compatible text and math font combinations that align well together. Best practices include:

  • Using OpenType math fonts like Latin Modern, Cambria Math, Asana Math or TeX Gyre with Unicode engines
  • Pairing text fonts and math fonts with similar x-heights for even baselines
  • Pairing humanist text fonts with humanist math fonts like Minion Math and Chivo Math

For example, to use Latin Modern for both text and math:

\usepackage{fontspec}
\setmainfont{Latin Modern Roman}

\usepackage{unicode-math}  
\setmathfont{Latin Modern Math}

And to demonstrate setting Chivo text and math fonts, with some font attribute commands in text mode:

\usepackage{fontspec}
\setmainfont{Chivo}  

\usepackage{unicode-math}
\setmathfont{Chivo Math}

{Some \textbf{bold} text and $x = \mathbf{y}$ math in \emph{italics}.} 

Fixing Font Warnings and Errors

When working with fonts in LaTeX, users may encounter common font warnings in the log file like:

  • "Font shape FT/foo/m/n' undefined"
  • "Font caps="foo" not loadable"

As well as fatal font errors causing compile failures, such as:

  • "Can not load font metrics"
  • "No glyph for U+xxxx in font"

Typical troubleshooting steps include:

  • Verifying font names are spelled correctly
  • Checking appropriate font attributes are set
  • Confirming using supported font file types like OTF and TTF
  • Disabling unused font features to resolve warnings

Special attention should be paid to multilingual documents mixing Latin and non-Latin scripts to ensure character sets and font encodings are properly handled.

Best Practices for Multi-lingual Font Selection

Working with multiple languages in LaTeX adds extra complexity for font selection. Best practices include:

  • Using fontspec or polyglossia to load multi-script font families like Charis SIL
  • Manually tuning fallback fonts on a per-script basis
  • Setting non-Latin main fonts and customizing Latin defaults
  • Overriding inherited font attributes to harmonize styles across languages

For example, to set up text in English and Greek:

\usepackage{fontspec}
\setmainfont[Ligatures=TeX]{Linux Libertine O}
\newfontfamily{\greekfont}[Script=Greek]{Linux Libertine O}

English text {\greekfont καὶ ἑλληνικό κείμενο}.

Here Linux Libertine supports both Latin and Greek scripts, so is defined as the base for both languages using fontspec's script selection model.

Leave a Reply

Your email address will not be published. Required fields are marked *