Change the font in LaTeX (sans serif, typewriter, other typefaces)
Every LaTeX document has three font families: serif, sans serif and typewriter. \textsf{...} and \texttt{...} switch for a piece of text, \renewcommand{\familydefault}{\sfdefault} makes the whole document sans serif, and a font package replaces the typeface.
Serif, sans serif and typewriter
\textrm, \textsf and \texttt change the text in their braces. The switches \rmfamily, \sffamily and \ttfamily change all text up to the end of the group.
Serif text, \textsf{sans serif text} and \texttt{typewriter text}.
{\sffamily This sentence is sans serif up to the closing brace.}Sans serif for the whole document
This line in the preamble makes the sans serif family the default for text, headings and captions. Formulas keep their own fonts.
This goes into the preamble.
\renewcommand{\familydefault}{\sfdefault}Another typeface with pdfLaTeX
With pdfLaTeX a typeface comes as a package. libertinus loads Libertinus Serif and Sans; newtxtext with newtxmath loads a Times-like text and math font, newpxtext a Palatino-like one, and lmodern Latin Modern, which looks like the default but scales to every size. Load fontenc with T1 as well, so accented letters are single characters of the font.
Complete document:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{libertinus}
\begin{document}
Text in Libertinus Serif and \textsf{text in Libertinus Sans}.
\end{document}Any OpenType font with LuaLaTeX
The fontspec package loads an OpenType or TrueType font by its name, from the TeX installation or from the fonts installed on the computer. \setmainfont sets the serif family, \setsansfont the sans serif family and \setmonofont the typewriter family. The document has to be compiled with LuaLaTeX or XeLaTeX.
Complete document:
\documentclass{article}
\usepackage{fontspec}
\setmainfont{Libertinus Serif}
\setsansfont{Libertinus Sans}
\setmonofont{Latin Modern Mono}
\begin{document}
Serif text, \textsf{sans serif text} and \texttt{typewriter text}.
\end{document}Pitfalls
- pdfLaTeX cannot load fonts such as Arial or Times New Roman from the operating system.
fontspecwith pdfLaTeX stops with "The fontspec package requires either XeTeX or LuaTeX". Use a font package, or switch the compiler. - A combination the font does not have, for example bold small capitals in Computer Modern, is replaced by another shape; the log shows the warning Font shape `OT1/cmr/bx/sc' undefined.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
fontspec - Package
fontenc - Package
lmodern - How-to Change the font size in LaTeX
- Error Font shape `OT1/cmr/bx/sc' undefined