Spring til indhold

Introduction

This article shows how to install custom fonts for typesetting your document’s text—it does not address fonts for typesetting mathematics. By “custom font”, we mean one unavailable on Overleaf’s servers, including commercial fonts that require a license or free-to-use fonts not distributed with TeX Live or included in our Linux installation.

  • Note: This article addresses mainstream font technologies but does not discuss specialist TeX-based solutions such as Metafont and virtual fonts.

Where do I start?

LaTeX-based typesetting solutions operate in the context of legacy. Over four decades, the development of LaTeX compilers and font formats has produced an ecosystem comprising a blend of older and new technologies. As a result, the actual process of installing and configuring custom fonts for use with LaTeX depends on these two factors:

  1. The type (format) of any font(s) you want to install:
  2. Which LaTeX compiler is used to compile your Overleaf project(s):
    • pdfLaTeX, or
    • XeLaTeX or LuaLaTeX

Jump to the section covering your preferred compiler:

Using custom fonts with pdfLaTeX

Here is a summary of what you’ll need to do—depending on the complexity of your custom font setup:

  1. For each font file (.ttf, .otf or .pfb), you will need to have (or generate!) a suitable TeX font metric (.tfm) and, perhaps, an encoding file (.enc).
  2. pdfLaTeX must know how to embed document fonts into the final PDF file. To do that, it reads a font map file (pdftex.map), which you’ll need to update with details of your custom font. On Overleaf, you do that using one of these commands:
    • \pdfmapfile{...}
    • \pdfmapline{...}
  3. You will need a font description (.fd) file which tells LaTeX how to use the new font files.

These are the main file types you will require:

  • .tfm (TeX font metric): these compact binary files do not contain any data describing the visual appearance of character shapes, only character dimensions and font “metadata” that TeX engines can use to perform their core typesetting tasks. TeX font metrics for math fonts contain additional data not required by text-only fonts; in particular, they have several “spacing” parameters used for typesetting maths.
  • .fd (font description): these files contain LaTeX commands required to configure and prepare your font for use with LaTeX. Commands permitted within .fd files, and the .fd file-naming convention, are described in the document LaTeX2ε font selection.
  • .map (font map): map files link the name of TeX font metrics files with the physical font file containing data required to draw character shapes (glyphs). Each font known to pdfTeX is described on a single line of the map file, using a well-defined syntax. For full details, see Section 5 of The pdfTeX user manual.
  • .enc (encoding): if required, this file type defines the mapping between character codes within your LaTeX text and the corresponding character shapes (glyphs) contained within the font file.

Custom TrueType font example

The Overleaf Galley contains a project that demonstrates installing a custom TrueType font—a PostScript Type 1 font would use a similar setup. It compiles with pdfLaTeX, XeLaTeX and LuaLaTeX and produces the following output:

Image showing Overleaf typesetting using a custom font

Notes on the example project

  • It uses the following \pdfmapline command:
  • \pdfmapline{+delphine < Delphine.ttf <T1-WGL4.enc}
    

    which links a TeX font metric file called delphine to the physical font file called Delphine.ttf.

    • The first < character instructs pdfTeX to subset the TrueType font; i.e., partially embed this font into the final PDF file. Subsetting a font produces smaller PDF files because they only contain the character-shape data required to display characters present in the document.
    • The second < character precedes the name of an encoding vector file, T1-WGL4.enc, used to map from the character codes in your text to character-shape data (glyphs) within the font file—character-shape data is required to render (draw) the character.
  • LuaLaTeX requires an extra configuration step because \pdfmapline generates an Undefined control sequence error due to changes in LuaTeX’s handling of certain backend commands—Section 3 of the LuaTeX Reference Manual. The \ifLuaTeX command, provided by the iftex package, is used to test for LuaTeX; if detected, \pdfmapline is defined:
  • \ifLuaTeX
    \protected\def\pdfmapline {\pdfextension mapline }
    \fi
    

Using multiple fonts or commercial fonts

To manage files provided by commercial font vendors and collections of free-to-use fonts, you can create folders in your Overleaf project to organize font files according to their type (.tfm, .pfb, .ttf etc.). A good model for naming and organizing your folders is the TeX Directory Structure (TDS), which was designed as a best practice “blueprint” for managing a collection of TeX-related files.

With the project folders in place, you next need to create a so-called latexmcrc file containing instructions telling the LaTeX compiler to search for files in those folders. By default, the compilers do not know those folders exist and will not find the files they contain.

Overleaf project example

The Overleaf Gallery example Using Folders to Manage Custom Font Files uses folders and a latexmkrc file to install a custom TrueType font. It contains a folder structure shown in this image:

Organizing project folders on Overleaf

and a latexmkrc file containing the following lines, which set runtime environment variables telling the LaTeX compiler to look for files in our project folders:

$ENV{'TEXINPUTS'}='myfonts/tex//:' . $ENV{'TEXINPUTS'};
$ENV{'T1FONTS'}='myfonts/fonts/type1//:' . $ENV{'T1FONTS'};
$ENV{'AFMFONTS'}='myfonts/fonts/afm//:' . $ENV{'AFMFONTS'};
$ENV{'TEXFONTMAPS'}='myfonts/fonts/map//:' . $ENV{'TEXFONTMAPS'};
$ENV{'TFMFONTS'}='myfonts/fonts/tfm//:' . $ENV{'TFMFONTS'};
$ENV{'TTFONTS'}='myfonts/fonts/ttf//:' . $ENV{'TTFONTS'}; 
$ENV{'VFFONTS'}='myfonts/fonts/vf//:' . $ENV{'VFFONTS'};
$ENV{'ENCFONTS'}='myfonts/fonts/enc//:' . $ENV{'ENCFONTS'};

If you’re wondering which files typically go into which folder, have a look at this article.

Using custom fonts with XeLaTeX and LuaLaTeX

Using PostScript Type 1 fonts

To use legacy PostScript Type 1 fonts with XeLaTeX and LuaLaTeX, you can follow the guidelines for pdfLaTeX, so we won’t repeat those details here.

Using OpenType fonts

XeLaTeX and LuaLaTeX provide extensive support for advanced typesetting using OpenType fonts—TrueType and PostScript varieties—because their underlying TeX engines, XeTeX and LuaHBTeX, have native (built-in) capabilities to process OpenType font technologies, which pdfTeX does not.

  • Note: LuaLaTeX, via its underlying TeX engine (LuaHBTeX), has the most sophisticated font-handling capabilities of all TeX engines; however, discussing these is beyond the scope of this article—for more information, see sections 6 and 12 of the LuaTeX Reference Manual.

To use OpenType fonts with XeLaTeX and LuaLaTeX, you need to load fontspec, a powerful and feature-rich package which provides an interface to the advanced typesetting capabilities of OpenType fonts.

  • Note: You do not need to use \usepackage[T1]{fontenc} (or \usepackage[utf8]{inputenc}) when using OpenType fonts with XeLaTeX or LuaLaTeX.

Example: introducing fontspec

The process is straightforward because no font installation is required—no need for .tfm, enc or .map files and writing font definitions (.fd) is consigned to history! It is as simple as this:

  1. Add the following line to your document preamble:
  2. \usepackage{fontspec}
    

  3. Select the file upload icon (UploadIcon.png), located above the file list.
  4. Drag and drop the font(s) onto the upload modal, or select them from your computer, to add the font(s) to your Overleaf project.
  5. Use one of the fontspec package \setxxxfont commands to configure and activate the font(s).

The excellent fontspec package documentation provides a wealth of examples you can explore, so here, we'll keep it straightforward to help you get started. For example, suppose you upload a font file called CrimsonText-Regular.ttf; after loading fontspec you can set the main document text font by writing

\setmainfont{CrimsonText-Regular.ttf}

You can also write syntax such as

\setmainfont{CrimsonText}[ 
Extension = .ttf,
UprightFont = *-Regular,
...]

You can also declare a new font family to use in arbitrary situations:

\newfontfamily{\crimson}{CrimsonText}
[Extension = .ttf, UprightFont = *-Regular, ...]
{\crimson This text uses the CrimsonText font}

Resources

  • The fontspec package documentation is a must-read—it not only provides numerous helpful examples but also contains a lot of useful background information on OpenType fonts.
  • The Overleaf help page on XeLaTeX contains a detailed example showing how to download OpenType fonts from Google and configure them with fontspec.
Sample projects

The Overleaf Galley has example projects that demonstrate using OpenType fonts with XeLaTeX or LuaLaTeX—you can open these and switch the compiler between LuaLaTeX and XeLaTeX:

The next graphic combines the output produced by these projects:

Sample Overleaf projects using Opentype fonts

Font and LaTeX compiler basics

Font file extensions

The table below lists file extensions used to indicate different types (formats) of font files:

Font type Extention
PostScript Type 1\(\displaystyle{}^{\mathbf*}\) .pfb
TrueType .ttf
TrueType Collections .ttc
OpenType .otf or .ttf

\(\displaystyle{}^{\mathbf*}\) In January 2023, all of Adobe’s authoring applications ceased support of the legacy Type 1 font format.

OpenType fonts exist in two “varieties”, often called “flavours”: PostScript-flavour and TrueType-flavour, names which reflect the data and mechanism(s) used to represent the character shapes (glyphs) they contain. That is why OpenType fonts have two file extensions: .otf or .ttf which, by convention, imply:

  • filename.otf is a PostScript-flavour OpenType font
  • filename.ttf is a TrueType-flavour OpenType font

For example, pdfTeX assumes the extension .otf indicates a PostScript-flavour OpenType font.

  • Note: the .ttc extension is used only for TrueType Font Collections.

About the .ttf file extension

Given a font file with a .ttf extension, it could be either:

  • an older (non-OpenType) TrueType font, or
  • an OpenType font containing TrueType data plus the extra features provided by OpenType.

Determining whether a given .ttf font file is OpenType or plain TrueType can be tricky, requiring applications to probe/investigate the font’s internal data structures. For further details, see this discussion (thread) or this Microsoft article.

Font formats and LaTeX compilers

The following table summarizes the (mainstream) font formats supported by three LaTeX compilers (strictly speaking, supported by the underlying TeX engines):

Compiler PostScript (Type 1) TrueType (plain\(\displaystyle{}^{\mathbf†}\)) OpenType (TrueType)\(\displaystyle{}^{\mathbf*}\) OpenType (PostScript)
pdfLaTeX Yes Yes Limited\(\displaystyle{}^{\mathbf{**}}\) Limited\(\displaystyle{}^{\mathbf{**}}\)
XeLaTeX Yes Yes Yes Yes
LuaLaTeX Yes Yes Yes Yes

\(\displaystyle{}^{\mathbf†}\)By “plain”, we mean a TrueType font that does not contain the additional data (tables) defined by the OpenType specification, thus lacks the more advanced typesetting features of TrueType-flavour OpenType fonts.

\(\displaystyle{}^{\mathbf*}\)pdfTeX provides limited support for TrueType Collections (extension .ttc)—it will only read data for the first font contained in the collection.

\(\displaystyle{}^{\mathbf{**}}\)pdfTeX’s “Limited” OpenType support means:

  • it does not support the advanced typesetting features contained in OpenType fonts;
  • it will subset TrueType-flavour OpenType fonts but not PostScript-flavour OpenType fonts which must be fully embedded. The absence of font subsetting produces larger PDF files and raises licensing problems with commercial fonts, which usually forbid full embedding;
  • it cannot utilize more than 256 characters per font, whereas individual OpenType fonts support tens of thousands;
  • it requires the production of TeX font metrics and other ancillary files (required by LaTeX)—processes that are outside the scope of this article (see Further reading for links to descriptions of those processes).

Further reading

A note on the “font wars”...

The 1980s and 1990s saw the so-called “font wars” as two competing font formats, PostScript Type 1 (from Adobe) and TrueType (created by Apple, licensed to Microsoft), battled for supremacy—to win the hearts and minds of all users, especially designers and typographers. Eventually, the OpenType font format emerged (in 1997) as a hybrid solution, encompassing and extending Adobe and Apple’s font technologies. OpenType builds on the original TrueType file format to enable advanced typesetting capabilities and better Unicode support through fonts containing over 65,000 character shapes (glyphs).

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX