13

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?

  • 1
    setmainfont sets up the main document font but you may still want a different font for headings or just for effect on some words, \fontspec allows such use. – David Carlisle Feb 8 '17 at 22:42
  • What does it mean "main font"? How is it different from "secondary fonts"? How is the effect of \setmainfont different from the effect of \fontspec? (What are their effects?) – Alexey Feb 8 '17 at 22:44
  • If I only want one set of fonts in my document, is \setmainfont enough? Would there be any difference if i use \fontspec instead? – Alexey Feb 8 '17 at 22:47
  • \setmainfont is a preamble level declaration which defined \normalfont etc to some consistent font family. \fontspec is a one-off load that selects the named font at that point so you may have your declaration setting up latin modern and italic and bold variants but still at some point go {\fontspec{Arial} hello} and have a word in a clashing font. – David Carlisle Feb 8 '17 at 22:48
  • Ok, i see. I was confused because i saw some examples where both were used one after another. However, why can't i use \fontspec in the preamble instead of \setmainfont? – Alexey Feb 8 '17 at 22:49
17

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

7

\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}

enter image description here

  • But as general practice, I don't think using \fontspec this way is really correct. – Alan Munn Feb 8 '17 at 23:04
  • @AlanMunn oh I agree font changes like spacing should be avoided mid document and hidden in higher level macros, but the question was what does \fontspec do so hiding it didn't seem appropriate. – David Carlisle Feb 8 '17 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.