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.)
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 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}
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
Notice that spaces that normally appear in the name of the font must also be present when you choose the font using fontspec.
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}