Fonts in LaTeX: Families, Sans Serif, and Changing the Font

Quick answer

  • Sans serif: \textsf{text} for a few words, \sffamily for the rest of a group. Back to serif: \rmfamily; monospace: \ttfamily or \texttt{...}.
  • Whole document sans serif: \renewcommand{\familydefault}{\sfdefault}.
  • Another font (pdfLaTeX): load a package, e.g. lmodern, helvet, newtxtext (Times), newpxtext (Palatino) or libertine.
  • Any installed font (XeLaTeX / LuaLaTeX): \usepackage{fontspec} and \setmainfont{Font Name}.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\begin{document}
Roman (serif) text, \textsf{sans serif text} and \texttt{monospace text}.

{\sffamily This whole paragraph is sans serif,
until the group ends.}
\end{document}

Fonts in LaTeX are organized in three families: roman (serif), sans serif and typewriter (monospace). Commands switch between the families, and a font package or fontspec decides which typeface each family uses. This guide covers the family commands, making text or a whole document sans serif, the font packages worth knowing for pdfLaTeX, system fonts with XeLaTeX and LuaLaTeX, font encodings, and the font errors that come up most. Every snippet compiles as shown.

Font Families in LaTeX

Every font in LaTeX has a family, a series (weight), a shape and a size, and each can be changed on its own. The family commands come in two forms: a command with an argument for a few words, and a declaration that applies until the end of the current group or environment:

FamilyCommandDeclarationDefault (Computer Modern)
Roman / serif\textrm{...}\rmfamilyCM Roman
Sans serif\textsf{...}\sffamilyCM Sans Serif
Typewriter / monospace\texttt{...}\ttfamilyCM Typewriter
\documentclass{article}
\begin{document}
\textrm{Roman}, \textsf{sans serif} and \texttt{typewriter} in one line.

{\sffamily A sans serif paragraph with \textbf{bold} and \textit{italic},
which keep the sans serif family.}

\begin{center}
  \ttfamily Everything in this environment is monospace.
\end{center}
\end{document}

Family, series and shape combine: \textsf{\textbf{...}} is bold sans serif. Weight and shape are covered in bold, italics and underline, and sizes in LaTeX font size.

Sans Serif in LaTeX

To set a heading, a table or a single word in sans serif, use \textsf or \sffamily. To make sans serif the main font of the document, redefine \familydefault, which tells LaTeX which family the body text uses:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.92]{helvet}               % Helvetica clone as the sans serif
\renewcommand{\familydefault}{\sfdefault}      % body text in sans serif

\begin{document}
\section{A sans serif document}
All body text is now sans serif. \textrm{\textbackslash textrm} still gives serif.

Math is unchanged: $f(x) = x^2 + \sin x$.
\end{document}

Without a package, \sfdefault is Computer Modern Sans; with helvet it is a Helvetica clone (the scaled option matches its height to the serif font). Other good sans serif packages are \usepackage[default]{sourcesanspro} and \usepackage[sfdefault]{FiraSans}, whose default / sfdefault option sets \familydefault for you.

Math stays serif, because math fonts are chosen separately. Use \mathsf{x} for single sans serif symbols, or the sansmath package for whole formulas:

\documentclass{article}
\usepackage{sansmath}
\renewcommand{\familydefault}{\sfdefault}

\begin{document}
Serif math: $f(x) = \alpha x^2$.

\sansmath
Sans serif math from here on: $f(x) = \alpha x^2$.
\end{document}

Changing the Font with a Package (pdfLaTeX)

With pdfLaTeX, the usual engine, you change fonts by loading a package in the preamble. Most packages replace the roman family, some also the sans serif, typewriter and math fonts. Load \usepackage[T1]{fontenc} with them (see font encoding below):

TypefacePackageReplacesNotes
Latin Modern\usepackage{lmodern}All familiesThe default look, scalable; use with T1
Times\usepackage{newtxtext,newtxmath}Text and mathThe modern choice for “Times New Roman”; mathptmx is the older one
Palatino\usepackage{newpxtext,newpxmath}Text and mathSuccessor of mathpazo
Helvetica\usepackage[scaled]{helvet}Sans serifAdd \familydefault to use it for the body
Linux Libertine / Biolinum\usepackage{libertine}Roman, sans, monoPair with libertinust1math for math
Charter\usepackage{XCharter}RomanSturdy, very readable on screen
TeX Gyre Pagella / Termes / Herostgpagella, tgtermes, tgherosOne family eachPalatino, Times and Helvetica clones
EB Garamond\usepackage{ebgaramond}RomanClassic book typeface
Source Sans Pro\usepackage[default]{sourcesanspro}Sans serif, as bodyClean sans for reports and slides
Fira Sans\usepackage[sfdefault]{FiraSans}Sans serif, as bodyPopular with the metropolis Beamer theme
Inconsolata\usepackage[varqu]{zi4}TypewriterReadable monospace for code listings
Courier\usepackage{courier}TypewriterThe classic typewriter look

A complete preamble with Times text, matching math and a Helvetica sans serif:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{newtxtext,newtxmath}   % Times for text and math
\usepackage[scaled=0.92]{helvet}   % Helvetica for \textsf
\usepackage[varqu]{zi4}            % Inconsolata for \texttt

\begin{document}
\section{Times, Helvetica and Inconsolata}
Body text in Times, with math to match: $\int_0^1 x^2\,dx = \tfrac{1}{3}$.

\textsf{A sans serif label} and \texttt{monospace code}.
\end{document}

To use a different font for just one passage, select a family by its internal code: {\fontfamily{ppl}\selectfont ...}. Common codes are ptm (Times), ppl (Palatino), phv (Helvetica), pcr (Courier), pbk (Bookman), pag (Avant Garde) and lmss (Latin Modern Sans). The blog post on LaTeX font packages compares the main typefaces side by side.

\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
Default font.
{\fontfamily{ppl}\selectfont This sentence is in Palatino.}
{\fontfamily{pbk}\selectfont This one is in Bookman.}
Back to the default font.
\end{document}

Try a font without installing anything

Paste a preamble into Octree, switch between newtx, libertine and helvet, and compare the PDF in seconds. Sign up free; the full TeX Live font collection is already there.

Open Octree →

Fonts in XeLaTeX and LuaLaTeX: fontspec and \setmainfont

XeLaTeX and LuaLaTeX can use any OpenType (.otf) or TrueType (.ttf) font. The fontspec package sets one font per family: \setmainfont (roman), \setsansfont and \setmonofont. Loading a font by its file name works on any installation:

% Compile with XeLaTeX or LuaLaTeX
\documentclass{article}
\usepackage{fontspec}
\setmainfont{texgyrepagella}[
  Extension      = .otf,
  UprightFont    = *-regular,
  BoldFont       = *-bold,
  ItalicFont     = *-italic,
  BoldItalicFont = *-bolditalic]
\setsansfont{texgyreheros}[
  Extension      = .otf,
  UprightFont    = *-regular,
  BoldFont       = *-bold,
  ItalicFont     = *-italic,
  BoldItalicFont = *-bolditalic]

\begin{document}
Body text in TeX Gyre Pagella, with \textbf{bold} and \textit{italic}.
\textsf{Sans serif in TeX Gyre Heros.}
\end{document}

The TeX Gyre files ship with TeX Live, so this example compiles anywhere. For fonts installed in your operating system, the family name is shorter: \setmainfont{Times New Roman}, \setsansfont{Arial} or \setmainfont{Libertinus Serif}. For a font file uploaded with the project, point Path at its folder: \setmainfont{MyFont}[Path=./fonts/, Extension=.ttf, UprightFont=*-Regular, BoldFont=*-Bold].

For math in the same style, load unicode-math and pick a math font with \setmathfont, for example \setmathfont{texgyrepagella-math.otf}. Do not combine fontspec with fontenc or with pdfLaTeX font packages such as newtxtext.

Font Encoding: OT1 vs T1

pdfLaTeX starts in the old OT1 encoding, whose fonts have only 128 characters. Accented letters are then built from a letter and an accent, which breaks hyphenation of words such as “Universität”, makes them impossible to search or copy from the PDF, and turns < and > in text into ¡ and ¿. The T1 encoding fixes all of that:

\documentclass{article}
\usepackage[T1]{fontenc}   % 256-glyph fonts with real accented letters
\usepackage{lmodern}       % scalable fonts in T1 (avoids bitmap fonts)

\begin{document}
Universität, café, naïve and résumé hyphenate and copy correctly.
Comparisons in text: a < b and c > d.
\end{document}

lmodern matters here: without it, T1 text in Computer Modern may be embedded as blurry bitmap (Type 3) fonts on installations without cm-super. XeLaTeX and LuaLaTeX use Unicode fonts and need neither line. Special characters that are not on the keyboard are listed in special characters in LaTeX.

Common Errors and Fixes

Message or symptomCauseFix
Package fontspec Error: The font “Times New Roman” cannot be foundThe font is not installed on the machine that compiles, or the name is misspelledInstall or upload the font and load it by file name with Path, or use a TeX Live font such as texgyretermes
Fatal Package fontspec Error: The fontspec package requires either XeTeX or LuaTeXCompiling with pdfLaTeXSwitch the compiler to XeLaTeX or LuaLaTeX, or use a pdfLaTeX font package instead
LaTeX Font Warning: Font shape `T1/xyz/m/n' undefined, using `T1/cmr/m/n' insteadUnknown family code in \fontfamily, or a font package not installedCheck the code (ppl, ptm, phv…) or load the package that provides it
LaTeX Error: Command `\Bbbk' already definedamssymb loaded after newtxmath (or newpxmath), which already define its symbolsLoad amssymb before the math font package, or drop it
Accented words do not hyphenate, copy as two characters, or < prints as ¡The default OT1 encoding\usepackage[T1]{fontenc} with lmodern
Text looks blurry or pixelated in the PDFBitmap (Type 3) fonts from T1 Computer Modern without cm-super\usepackage{lmodern}
Bold or small caps silently missingThe font has no such shape and LaTeX substituted another (check the log for Font shape … undefined)Choose a font that has the shape, e.g. newtx or libertine for bold small caps

Frequently Asked Questions

What is the default font in LaTeX?

Computer Modern, designed by Donald Knuth for TeX. It has a serif (roman) family, a sans serif family and a typewriter family, plus matching math fonts. Latin Modern, loaded with \usepackage{lmodern}, is the same design in a modern scalable format and is the default in XeLaTeX and LuaLaTeX with fontspec.

How do I make text sans serif in LaTeX?

For a few words use \textsf{text}. For a longer passage, switch with \sffamily inside a group: {\sffamily text}. For the whole document add \renewcommand{\familydefault}{\sfdefault} to the preamble, optionally after a sans serif package such as \usepackage[scaled]{helvet}.

How do I use Times New Roman in LaTeX?

With pdfLaTeX, load \usepackage{newtxtext,newtxmath}, which gives a Times clone for text and matching math. With XeLaTeX or LuaLaTeX, load fontspec and write \setmainfont{Times New Roman} if the font is installed on your system, or use the TeX Gyre Termes files that ship with TeX Live.

How do I change the font of the whole document?

Load a font package in the preamble, for example \usepackage{newtxtext,newtxmath} for Times, \usepackage{newpxtext,newpxmath} for Palatino or \usepackage{libertine} for Linux Libertine. With XeLaTeX or LuaLaTeX you can use any OpenType or TrueType font with \usepackage{fontspec} and \setmainfont{Font Name}.

What does \usepackage[T1]{fontenc} do?

It switches pdfLaTeX from the old OT1 encoding to T1, whose fonts contain accented letters as single glyphs. Words with accents then hyphenate correctly, copy and paste from the PDF works, and characters such as < and > print correctly in text. Load it together with lmodern or another font package. XeLaTeX and LuaLaTeX do not need it.

Why does fontspec say the font cannot be found?

fontspec looks the name up among the fonts installed on the system, and the name must match exactly. Check the spelling, install the font, or load it by file name instead: \setmainfont{texgyretermes-regular.otf} or \setmainfont{MyFont}[Path=./fonts/, Extension=.ttf] for a font file uploaded with the project. fontspec also needs XeLaTeX or LuaLaTeX; it fails under pdfLaTeX.

Next Steps

Free LaTeX Tools

Get the font right with AI

Tell Octree's AI which typeface you want and it picks the package, sets up the preamble and explains any font warnings, with the PDF compiled next to your code.

Try Octree Free →