[Tex/LaTex] \setmainfont vs \fontspec


The documentation of fontspec package is quite confusing:

This section concerns the variety of commands that can be used to
select fonts.

\fontspec{⟨font name⟩}[⟨font features⟩]
\setmainfont{⟨font name⟩}[⟨font features⟩]
\setsansfont{⟨font name⟩}[⟨font features⟩]
\setmonofont{⟨font name⟩}[⟨font features⟩]
\newfontfamily⟨cmd⟩{⟨font name⟩}[⟨font features⟩]

These are the main font-selecting commands of this package. The
\fontspec command selects a font for one-time use; all others should
be used to define the standard fonts used in a document, as shown in
Example 1.

The mentioned Example 1 does not really clarify much. After reading a few more pages, I still could not understand the exact effect of these commands and why one would need both \setmainfont and \fontspec. Most examples were given with \setmainfont.

Why something like

\setmainfont[UprightFont={* 10 Regular},
             BoldFont={* 10 Bold},
             ItalicFont={* 10 Italic},
             BoldItalicFont={* 10 Bold Italic},
             SmallCapsFont={* Caps 10 Regular},
             SlantedFont={* Slanted 10 Regular}
            ]{Latin Modern Roman}

is not enough? When to use \setmainfont and when to use \fontspec?

Best Answer

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:



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


\fontspec{EB Garamond}


Abc def ghi \textit{abc def ghi}


you'd only get the default Latin Modern font. It would be different with


\fontspec{EB Garamond}

Abc def ghi \textit{abc def ghi}


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.


\setmainfont{EB Garamond}


Abc def ghi \textit{abc def ghi}


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