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?.