The fontspec package
Loads OpenType and TrueType fonts by name in LuaLaTeX and XeLaTeX: \setmainfont, \setsansfont, \setmonofont and \newfontfamily.
This package needs LuaLaTeX or XeLaTeX. The editor compiles with pdfLaTeX, so the package does not work there.
Loading the package
\usepackage{fontspec}pdfLaTeX can only use fonts that were prepared for TeX and come with a LaTeX package. With fontspec you name any OpenType or TrueType font that is installed on the system or in the TeX distribution, or you give its file name.
The package needs LuaLaTeX or XeLaTeX. Under pdfLaTeX it stops with the fatal error The fontspec package requires either XeTeX or LuaTeX.
Font features are given in a second, optional argument, for example Numbers=OldStyle for old-style figures or Scale=MatchLowercase to match the x-height of the main font. The math font does not change; that is the job of unicode-math.
Main commands
\setmainfont{Latin Modern Roman} | Font of the running text (\rmfamily). |
\setsansfont{Latin Modern Sans} | Sans-serif font (\sffamily, \textsf). |
\setmonofont{Latin Modern Mono}[Scale=MatchLowercase] | Monospaced font (\ttfamily, \texttt), scaled to the x-height of the main font. |
\newfontfamily\headingfont{Latin Modern Sans Demi Cond} | Defines a command that switches to one more font. |
\setmainfont{Name}[Numbers=OldStyle] | Switches on an OpenType feature, here old-style figures. |
\addfontfeatures{Numbers=Lining} | Changes a feature of the current font inside a group. |
\setmainfont{MyFont}[Path=fonts/, Extension=.otf, UprightFont=*-Regular, BoldFont=*-Bold] | Loads font files from a folder of the project; * stands for the name in the first argument. |
Complete example
\documentclass{article}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}[Numbers=OldStyle]
\setsansfont{Latin Modern Sans}
\setmonofont{Latin Modern Mono}[Scale=MatchLowercase]
\newfontfamily\headingfont{Latin Modern Sans Demi Cond}
\begin{document}
{\headingfont\Large Fonts by name}
Größe, naïve and façade are typed directly. The figures 1234567890
are old-style figures. \textsf{Sans serif}, \texttt{monospaced},
\textbf{bold} and \textit{italic} use the fonts set in the preamble.
{\addfontfeatures{Numbers=Lining}1234567890 are lining figures.}
\end{document}Compiled with LuaLaTeX from TeX Live 2023.
Pitfalls
- Do not load
inputencorfontenctogether withfontspec. The source file has to be UTF-8. - A name that the engine cannot resolve stops with
Package fontspec Error: The font "..." cannot be found. The font has to be installed on the machine that compiles the document; a font that exists only on your own computer is missing elsewhere. - The first LuaLaTeX run after a new font was installed takes longer, because the engine rebuilds its list of font names.
- A character that the chosen font does not contain is left out of the PDF. The log then has a line Missing character: There is no L in font nullfont!.
Related pages
- How-to Change the font in LaTeX
- Package
unicode-math - Package
fontenc - Error Missing character: There is no L in font nullfont!