\setmainfont vs \fontspec
My impression is that the usage of \fontspec
has been misunderstood from the beginning. Will Robertson acknowledged it was not the best choice, when I discussed the topic with him.
According to the guidelines of LaTeX3 programming, internal functions can have user level interfaces. All the main commands of fontspec
are defined in terms of shared internal functions:
\setmainfont
\setsansfont
\setmonofont
\newfontfamily
\newfontface
\fontspec
The first five do more than the last one, because they set up fonts for general usage in the document, whereas \fontspec
directly accesses the lower level functions.
You could use \fontspec
in the preamble, but it would do nothing, because \begin{document}
executes \normalfont
. So with
\documentclass{article}
\usepackage{fontspec}
\fontspec{EB Garamond}
\begin{document}
Abc def ghi \textit{abc def ghi}
\end{document}
you'd only get the default Latin Modern font. It would be different with
\documentclass{article}
\usepackage{fontspec}
\begin{document}
\fontspec{EB Garamond}
Abc def ghi \textit{abc def ghi}
\end{document}
but a subsequent \normalfont
declaration would return to Latin Modern. Most importantly, this would go against the “abstraction paradigm”: in the document you should never (well, hardly ever) choose a specific font, but rather issue high level instructions, such as \textit
or \emph
or \sffamily
. I'd be even more restrictive and say that \textit
or \sffamily
should very rarely be found in a well written typescript.
Abstraction is good, because it puts the details in specific places (the preamble), so the document is as independent as possible from font choices.
The usual example, though not strictly connected with this problem, is the following. Suppose you are writing a thesis in Computer Science and you have several multiletter identifiers for functions. You choose to set them in sans serif and happily type $\mathsf{foo}(x)=2$
. Now you show a preliminary version to your supervisor who raises her eyebrows and tells you “What's this way of typesetting function names? Everybody knows they should be in Fraktur type!” Frantic search and replace follows: every \mathsf
command changed into \mathfrak
. “Did I tell you that at a recent conference a colleague convinced me that function names should be boldface Roman type?” says your supervisor a few weeks later.
But wait! If you had started with \newcommand{\func}[1]{\mathsf{#1}}
in the preamble and used $\func{foo}(x)$
in the document, the change to Fraktur or boldface would have required just one change in the document.
Now, \setmainfont{EB Garamond}
not only issues the equivalent of \fontspec{EB Garamond}
at begin document as part of \normalfont
, but also sets up all the font related infrastructure.
\documentclass{article}
\usepackage{fontspec}
\setmainfont{EB Garamond}
\begin{document}
Abc def ghi \textit{abc def ghi}
\end{document}
A document might use sans serif fonts for certain parts (for instance, titles), so it's much better to use \setsansfont
for deciding what font to use as sans serif. Similarly for the monospaced font, if at all used in the document. You don't want to do
{\fontspec{Futura}This is sans serif}
every time you want to print something in a sans serif font, do you? Instead you do \setsansfont{Futura}
in your preamble and use \textsf{This is sans serif}
(with the above caveat about abstraction, of course).
If you need a secondary font, for instance a Greek font for those few Greek words when your preferred text font (Comic Sans, of course) hasn't support for Greek, you do
\newfontfamily{\greekfont}{GFS Artemisia}[Scale=MatchUppercase]
and use {\greekfont ελληνική λέξη}
in the document.
Using {\fontspec{GFS Artemisia}ελληνική λέξη}
would do the same, at the cost of recomputing all fonts for this family every time the command is issued (well, not really, but \fontspec
is much less efficient than \newfontfamily
in the preamble).
\setmainfont
sets up the main document font and common variants such as bold and italic if they are available, so it is usualy used in the preamble. \fontspec
actually selects a one-off font at the point of use.
\documentclass{article}
\usepackage{fontspec}
\setmainfont{Tex Gyre Termes}
\begin{document}
one two \textbf{three}
four {\fontspec{Arial Bold} six} seven
\end{document}