574

The standard font packages make global changes to the fonts in my document. What if I want to use a particular font for just a portion of my text?

  1. How do I find the right name of the font?
  2. How do I select the font?
  3. How do I restrict the scope of the font change?

There are separate answers for different flavors of TeX (quick links):

5
  • 2
    For short inline sections of font change, also discussed here: tex.stackexchange.com/questions/100575/… Commented Mar 2, 2013 at 16:44
  • @Alan I think we can remove the “small section” part of this question since it covers all application scopes?
    – doncherry
    Commented Apr 21, 2013 at 17:00
  • 2
    @doncherry I think the intended scope of the question is about non-global font changes.
    – Alan Munn
    Commented Apr 21, 2013 at 17:01
  • 1
    @AlanMunn I agree, but the scope of this question could easily be widened to include local and global changes, making it more canonical? Having a separate question for global changes seems like a waste of resources. The XeLaTeX answer is ready already, the pdfLaTeX and ConTeXt answers might need a bit of tweaking.
    – doncherry
    Commented Apr 21, 2013 at 17:07
  • 5
    @doncherry: The global change is trivial when it just involves including a package. Making that change only for a portion of the document is, however, unexpectedly challenging (and I fear that's one more reason for non-LaTeX-people to refuse LaTeX), and I suspect a significant portion of the 200+ upvoters so far came here specifically looking for a way to not make a global font change, as I just did. Widening the scope of the question to include global changes could make it less obvious that the question actually explains how to locally apply a font. Commented Oct 1, 2015 at 9:03

5 Answers 5

478

Regular LaTeX

Finding the font name

This is actually the hardest part. When you load a regular font package like helvet (which sets the default sans serif font to a Helvetica clone) it issues commands to set up the font using an internal name (Berry name), which is hidden to regular users. These names traditionally use a system of three or four letter lower case names for each font family. Usually these names are documented in documentation associated with the font.

Here is a list of the most common fonts, and their codes:

Family                 Font Name
pag                    Avant Garde
fvs                    Bitstream Vera Sans
pbk                    Bookman
bch                    Charter
ccr                    Computer Concrete
cmr                    Computer Modern
pcr                    Courier
ugm                    Garamond
phv                    Helvetica
fi4 (zi4)              Inconsolata
lmr                    Latin Modern
lmss                   Latin Modern Sans
lmtt                   Latin Modern Typewriter
LinuxBiolinumT-OsF     Linux Biolinum (formerly 'fxb' in older package versions)
LinuxLibertineT-OsF    Linux Libertine (formerly 'fxl' in older package versions)
pnc                    New Century Schoolbook
ppl                    Palatino
qag                    TeX Gyre Adventor 
qbk                    TeX Gyre Bonum 
qzc                    TeX Gyre Chorus
qcr                    TeX Gyre Cursor
qhv                    TeX Gyre Heros
qpl                    TeX Gyre Pagella 
qcs                    TeX Gyre Schola
qtm                    TeX Gyre Termes
ptm                    Times
uncl                   Uncial
put                    Utopia
pzc                    Zapf Chancery

For some Greek fonts the codes are:

Artemisia              artemisia 
Baskerville            gfsbaskerville 
Bodoni                 bodoni 
Complutum              complutum 
Didot                  udidot 
Neohellenic            neohellenic 
Porson                 porson 
Solomos                solomos 
Greek Times            txr 
Kerkis                 mak 
LX                     llcmss 
Default                lmr

For others you can look at the fontname.pdf file here on page 4. See also the questions Berry naming scheme list of LaTeX font families and Quickest way to search for valid \fontfamily arguments? on this topic.

If you don't find any documentation for the font, as a last resort (or a first resort once you know what you are doing) you can open the .sty file that actually loads the font and see for yourself what the internal font family name is (or you could search inside it with grep). Here are two examples:

From helvet.sty: at the end of the package is the line:

\renewcommand{\sfdefault}{phv}

This sets the default sans font (\sfdefault) to the phv family, so phv is the internal name of the font.

From PTSansCaption.sty, you will find the following line:

\renewcommand{\sfdefault}{PTSansCaption-TLF}

Here, the internal name is closer to its file name: PTSansCaption-TLF.

Both of these examples have shown the code for changing the default sans serif font. If the font package changes the roman or mono font you would look for the following commands respectively

\renewcommand{\rmdefault}{...}
\renewcommand{\ttdefault}{...}

Selecting the font

To select a font, you use the following commands:

\fontfamily{<familyname>}\selectfont

It's often useful to wrap this in a macro:

\newcommand*{\myfont}{\fontfamily{<familyname>}\selectfont}

Restricting the scope of the selection

You can always restrict the scope of font changing commands by enclosing the text in braces:

{\fontfamily{<familyname>}\selectfont ...}

or if using a command

{\myfont ...}

or, to make the scope of the command more visible in your file if you don't have a brace-matching editor

\begingroup
  \myfont ...
\endgroup

If this is something you will be doing a lot, it would make more sense to turn it into a proper environment:

\newenvironment{myfont}{\fontfamily{<familyname>}\selectfont}{\par}

Then you use it like any other environment:

\begin{myfont}
  Some text in the new font.
\end{myfont}

You can also define a command corresponding to the standard font changing commands such as \textrm or \textsf, but using your particular font:

\DeclareTextFontCommand{\textmyfont}{\myfont}

Use this like the standard commands:

Text in the default font. \textmyfont{Text in the new font.} Again text in the default font.

An advantage of this command over the simpler version described above is automatic italic correction, cf. Why use \DeclareTextFontCommand vs. just \newcommand?.


Common fonts rendered

(except ccr Compute Concrete).

Common fonts

9
  • 30
    Wow, huge thanks to whoever took my meagre beginning of an answer and made it 100 times better.
    – frabjous
    Commented Aug 8, 2011 at 22:52
  • What's the recommended way of limiting the scope of the font to just the block? The calligra font (tug.dk/FontCatalogue/calligra) seems to have a \calligra command that applies only to the current block, however for some packages (like tug.dk/FontCatalogue/tgchorus) set the default font for the entire document. Must one reset this (using \renewcommand) right after the \usepackage command and then use the \myfont macro you describe?
    – ffledgling
    Commented Oct 24, 2015 at 13:40
  • visual reference: The LaTeX Font Catalogue, for example here the Typewriter Fonts
    – nilon
    Commented Sep 22, 2016 at 13:34
  • @Alan Munn When using \newcommand*{\myfont}{\fontfamily{<familyname>}\selectfont} or \newenvironment{myfont}{\fontfamily{<familyname>}\selectfont}{\par} and then in the text begin/end, the text does not get justified and it exceeds the set width of the text. Is it possible to fix this?
    – Valerio
    Commented May 17, 2017 at 14:52
  • @Valerio Make a minimal example document that shows the problem and then ask a new question.
    – Alan Munn
    Commented May 17, 2017 at 15:03
268
+250

XeLaTeX and LuaLaTeX

The appearance of XeLaTeX and LuaLaTeX has simplified font selection greatly for LaTeX users, since any font installed on your system can be used with these engines using the fontspec package to load the font.

Both XeLaTeX and LuaLaTeX assume by default UTF-8 input, so you should not load the inputenc package when using them, and you should save your source files as UTF-8.

A basic document will therefore look like the following:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{<any font on your system>}
\begin{document}
...
\end{document}

Selecting fonts globally

For a whole document, you can set the roman font, the sans serif font and the monospace font with the following commands:

\setmainfont[<font features>]{<name of font>} % sets the roman font
\setsansfont[<font features>]{<name of font>} % sets the sans font
\setmonofont[<font features>]{<name of font>} % sets the monospace font

Selecting fonts locally

If you want to use a font for a small section of your document it's best to define a new font family.

\newfontfamily\myfont[<font features>]{<name of font>}

This sets up a switch called \myfont which changes the font to that font. (It is also possible to select a font directly using the \fontspec command, but this is generally to be avoided, since the \newfontfamily method is much more efficient.) This switch behaves exactly like \rmfamily or sffamily (except that it is called \myfont and switches to the font assigned to it).

Font Features

Since fontspec package provides access to OpenType fonts, it is able to provide access to many of the special features that come with these fonts. These features can be selected using the optional argument of any font selection command. See the fontspec documentation for more details. I'll outline a couple of commonly used features here.

  • [Ligatures=TeX] This feature allows you to use regular TeX ligatures (which are not turned on by default in fontspec. Especially if you are used to e.g. typing LaTeX style quotation marks or -- and --- instead of typing the actual characters directly, you should always turn on this option.
  • [Numbers=OldStyle] This feature turns on lower case numbers
  • [Scale=MatchLowercase] This feature is used to scale e.g. the sans or mono font to match (in this example) the lower case characters of the roman font. Another option is MatchUppercase; alternatively a numeric scaling value can be given.

See the fontspec documentation for a full description of the wealth of features that can be specified.

If you are setting separate roman, mono, and sans fonts or creating new font families, you often want to have the same font features specified for all. You can do this by using the \defaultfontfeatures command:

\defaultfontfeatures{Ligatures=TeX} % makes this a feature for all selected fonts

Finding the name of the font

If you are using system fonts, you can use the name of the font as it appears in any GUI application on your system. (On a Mac, these are usually the fonts in /Library/Fonts; on Windows they are usually in \Windows\Fonts; on Linux the usual place is /usr/local/share/fonts. For example:

\setmainfont{Linux Libertine O}
\newfontfamily\myfont{Linux Libertine O}

There's really no sense in listing font names here, but I'll add some examples of what the names can look like:

  • Arial Rounded MT Bold
  • DejaVu Sans Mono
  • Florencesans SC Black

These names are not the filenames of the fonts. If you have some sort of font manager GUI like Font Book on the Mac, the names are displayed there. On Windows you can find the names in the Fonts preview section of the control panel.

Notice that spaces that normally appear in the name of the font must also be present when you choose the font using fontspec.

If you want to list the fonts which support a certain language (since fonts usually support only part of the Unicode character), you can use the fc-list command. For example, to find fonts which support Chinese, first find the language code of Chinese, i.e., zh, then we use the following command

fc-list :lang=zh

to show the list of fonts supporting Chinese. Output will be something like:

/usr/share/fonts/custom/STLITI.TTF: STLiti,华文隶书:style=Regular
/usr/share/fonts/custom/SourceHanSerifSC-Regular.otf: Source Han Serif SC,思源宋体:style=Regular
/usr/share/fonts/wqy-microhei/wqy-microhei.ttc: WenQuanYi Micro Hei Mono,文泉驛等寬微米黑,文泉驿等宽微米黑:style=Regular
/usr/share/fonts/custom/simhei.ttf: SimHei,黑体:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/custom/SIMLI.TTF: LiSu,隶书:style=Regular
/usr/share/fonts/custom/STXINWEI.TTF: STXinwei,华文新魏:style=Regular
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: WenQuanYi Zen Hei Sharp,文泉驛點陣正黑,文泉驿点阵正黑:style=Regular
/usr/share/fonts/custom/simfang.ttf: FangSong,仿宋:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/wqy-microhei/wqy-microhei.ttc: WenQuanYi Micro Hei,文泉驛微米黑,文泉驿微米黑:style=Regular
/usr/share/fonts/custom/FZYTK.TTF: FZYaoTi,方正姚体:style=Regular

The font name is after the font location, for example, STLiti.

Restricting the scope of the selection

You can always restrict the scope of font changing commands by enclosing the text in braces:

{\myfont ...}

or, to make the scope of the command more visible in your file if you don't have a brace-matching editor

\begingroup
  \myfont ...
\endgroup

If this is something you will be doing a lot, it would make more sense to turn it into a proper environment:

\newenvironment{myfont}{\myfont}{\par}

Then you use it like any other environment:

\begin{myfont}
  Some text in the new font.
\end{myfont}

You can also define a command corresponding to the standard font changing commands such as \textrm or \textsf, but using your particular font:

\DeclareTextFontCommand{\textmyfont}{\myfont}

Use this like the standard commands:

Text in the default font. \textmyfont{Text in the new font.} Again text in the default font.

An advantage of this command over the simpler version described above is automatic italic correction, cf. Why use \DeclareTextFontCommand vs. just \newcommand?.

13
  • 5
    More font features: [Color=000099] (dark blue) Commented Apr 13, 2012 at 6:40
  • 1
    In the final section Restricting the scope of the selection I don't understand what 'myfont' is supposed to stand for. The font's name?? So if the font's name is, say, 'Linux Libertine O', is this how one switches to this font locally: {\Linux Libertine O ...}?
    – Evan Aad
    Commented Jun 23, 2017 at 6:29
  • 1
    Another point of confusion for me is the following. The section Finding the name of the font begins with the following instruction 'If you are using system fonts, you can use the name of the font as it appears in any application on your system. (On a Mac, these are usually the fonts in /Library/Fonts[...]'. The filenames appearing in /Library/Fonts are not necessarily the names of the fonts, as there need be no relation between the name of a font's file and the font's name. So which name are you referring to? File or font? In the latter case, how can it be found on Mac, Windows and Linux?
    – Evan Aad
    Commented Jun 23, 2017 at 9:28
  • @EvanAad \myfont is the literally the macro that is created when you use \newfontfamily\myfont{Name of Font}. It is not the name of the font, but whatever name you choose to use as the macro to switch to that font. As for finding the names of fonts, on a Mac it is the name displayed in Font Book (or in the Font menu of any Mac application.) Similarly on other OSes, it is the name that is used in GUI applications, not the font filename.
    – Alan Munn
    Commented Jun 23, 2017 at 14:02
  • 1
    @Philipp But you need the \par in the environment, because it's only at the ends of paragraphs that TeX applies the changes.
    – Alan Munn
    Commented Aug 21, 2018 at 15:31
57
+250

ConTeXt MkIV

In ConTeXt MkIV it is easy to use a font for a small section. For larger parts of text I would recommend to use the Simplefonts module or to write typescripts, because larger parts probably contain font switches that change the style.

How do I find the right name of the font?

First you have to install the font, if not already done. On Unix you can use the directory $HOME/.fonts for a per user or e.g. /usr/local/share/fonts for a system wide installation. You have to point the environment variable OSFONTDIR to the directory where the fonts can be found:

export OSFONTDIR="/usr/local/share/fonts;$HOME/.fonts"

Then run

mtxrun --script fonts --reload

to regenerate the font database. Now you can query the database. This gives you a list of all available fonts:

mtxrun --script fonts --list --all

Here are the first ten lines of this list:

ams                   wncysc10              wncysc10.afm
amsbold               wncyb10               wncyb10.afm
amsmedium             wncysc10              wncysc10.afm
antpoltbold           antpoltbold           antpolt-bold.otf
antpoltbolditalic     antpoltbolditalic     antpolt-bolditalic.otf
antpoltcondbold       antpoltcondbold       antpoltcond-bold.otf
antpoltcondbolditalic antpoltcondbolditalic antpoltcond-bolditalic.otf
antpoltconditalic     antpoltconditalic     antpoltcond-italic.otf
antpoltcondregular    antpoltcondregular    antpoltcond-regular.otf
antpoltexpdbold       antpoltexpdbold       antpoltexpd-bold.otf

You get three columns the second being the normalised (all lower-case, no special characters, no spaces) name and the third being the file name (in lower-case).

You can run the list through grep or use the built-in mechanism to search for particular fonts:

mtxrun --script fonts --list --pattern=deli* --all

gives

delicious             deliciousbold         Delicious-Bold.otf
deliciousbold         deliciousbold         Delicious-Bold.otf
deliciousbolditalic   deliciousbolditalic   Delicious-BoldItalic.otf
deliciousheavy        deliciousheavy        Delicious-Heavy.otf
deliciousitalic       deliciousitalic       Delicious-Italic.otf
deliciousmedium       deliciousitalic       Delicious-Italic.otf
deliciousnormal       deliciousroman        Delicious-Roman.otf
deliciousregular      deliciousheavy        Delicious-Heavy.otf
deliciousroman        deliciousroman        Delicious-Roman.otf
delicioussmallcaps    delicioussmallcaps    Delicious-SmallCaps.otf

How do I select the font?

Since we deal just with one font without bold or italic font switches (however, the selected font can of course be bold or italic) we can simply use \definefont:

\definefont [delicious] [name:deliciousroman]

This defines a macro \delicious which switches to the given font. The name: prefix is used to refer to the normalised name in the font list (second column). One can also use the file: prefix and use the file name (third column) without the path.

How do I restrict the scope of the font change?

You can use braces, \start \stop, \begingroup \endgroup or any other grouping mechanism.

Here is a complete example which uses the font Delicious-Roman (an OpenType font).

\definefont [delicious] [name:deliciousroman]

\starttext

\start
    \delicious
    \input knuth
\stop

\input knuth

\stoptext

The output:

result

The right box indicates that indeed the requested font is being used. Further information can be found one the ConTeXt wiki - Fonts in LuaTeX and the Chapter on fonts.

2
  • If it's not in the list, it is not installed (at least not for ConTeXt). Are you sure you are using LuaTeX (MkIV) and you didn't forget to set OSFONTDIR or reloading the font database? For clarification of this very problem please contact the mailing list
    – Marco
    Commented Jul 25, 2012 at 5:26
  • 2
    simplefonts was superseded by \definefontfamily. There is also a new manual on Fonts in MKIV. Commented Sep 23, 2016 at 9:20
10

As far as delimiting small sections, you can use text property declarations. Fore example, to change the font to teletype and then back to default, follow this example:

\ttfamily http://www.ctan.org\normalfont

Or, use braces to restrict the scope of the change:

{\ttfamily http://www.ctan.org}

You could also create an environment with a declaration, for example when writing multiple lines of code:

\begin{ttfamily}
statement 1;
statement 2;
...
statement n;
\end{ttfamily}
0

This is an answer with a small working code:

\documentclass{article}
\usepackage{fontspec}
\newfontfamily\myFont{Courier_New.ttf}
\usepackage[T1]{fontenc}
\begin{document}
Default \TeX\ font. {\myFont A unicode font.} Again default \TeX\ font.
\end{document}

Here, I have used the Courier new font. To check the available fonts, go to the directory: /usr/share/fonts/. Issueing the following command from here will give you a list of fonts available: find . -type f -name "*.ttf. You any of the font files here in place of Courier_New.ttf in line number 3.

Following is the output: Output of the sample code

1
  • 1
    If you're using fontspec you shouldn't be using \usepackage[T1]{fontenc}. Don't load that package at all. And this answer doesn't really cover anything that the existing answers don't already cover.
    – Alan Munn
    Commented Feb 13, 2022 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .