fontspec offers the commands \setmainfont, \setsansfont and \setmonofont to load a font and set it as the default roman, sans serif or monospaced font, respectively. It also offers the command \newfontfamily, which loads a font and defines a command that activates it:

% loads the font family 'Latin Modern Roman' and defines the command \latinmodernroman,
% which activates it
\newfontfamily\latinmodernroman{Latin Modern Roman}
\latinmodernroman% activates the Latin Modern Roman font family

% loads the font family 'Latin Modern Roman' and *also* sets it as the roman family default
\setmainfont{Latin Modern Roman}


How would one go about decoupling loading and setting, i.e., first loading a font with \newfontfamily and then setting it as family default?

\newfontfamily\latinmodernroman{Latin Modern Roman}

\def\rmdefault{  what?  }
% My understanding is that it's 'lmr' for 'Latin Modern Roman'
% But how about arbitrary fonts? How to know their "LaTeX identifiers" when using fontspec?


I've been trying to understand fontspec.dtx, but I'm not familiar with the "expl" syntax.

• @cfr I had written this question a few days ago but didn't publish it. Then, I found the (kind of) solution myself. Now, I wanted to ask another question, and found this one saved as a draft, so I thought I could publish it and answer myself, in hopes of it being useful to somebody. I am sorry for not making this clear and wasting your time. I will nevertheless consider any answer and not just accept mine right away. – Kalrish Jan 19 '16 at 22:35
• Oh, that's fair enough. It was just that as far as I could tell you'd immediately answered, which suggested you asked in order to answer but didn't bother saying so. I can see if you'd saved it as a draft, this would be easy to do. (In effect, you didn't intend to answer when you wrote the question.) I didn't know it was possible to save drafts, actually. Useful. – cfr Jan 19 '16 at 22:39
• Can you say why and how you're using this? A complete minimal example would be good. I don't think your solution is a good one because you really shouldn't be using that for this kind of case. But I'm not clear what exactly you are trying to do, so maybe that is the best solution and I just don't understand the desiderata. – cfr Jan 19 '16 at 22:40
• @cfr To be honest, it was just my fixation with decoupling things that are logically or naturally decoupled: it bothered me that \setmainfont not only loaded a font, but also set it as the default and that there were no means for carrying out these steps separately, i.e., no way of doing so with a font loaded with \newfontfamily. I also dislike globals (I feel likewise about polyglossia's \setdefaultlanguage, for instance). So… there's actually no use case, aside from my 'idealism'. I feel a bit embarrassed, I hope this doesn't anger anybody. – Kalrish Jan 19 '16 at 22:48
• The thing is, you have a default serif, default sans and default typewriter whether you set them or not. If you don't, you are in big trouble. \setmainfont is actually changing the default serif, which has already been changed at least once when fontspec was loaded. – cfr Jan 19 '16 at 23:42

What you are looking for is called "NFSS family name" (where "NFSS" stands for New Font Selection Scheme).

Section 5.2 of the fontspec manual:

In fontspec, the family names are auto-generated based on the fontname of the font; for example, writing \fontspec{Times New Roman} for the first time would generate an internal font family name of ‘TimesNewRoman(1)’.

The solution is provided in the same section: use the NFSSFamily feature when loading the font:

\newfontfamily\verbatimfont[NFSSFamily=myverbatimfont]{Inconsolata}

% Now, you can do:
\def\rmdefault{myverbatimfont}
\fontfamily{myverbatimfont}\selectfont


Beware, however:

Only use this feature when necessary; the in-built font switching commands that fontspec generates (such as \verbatimfont in the example above) are recommended in all other cases.

The fontspec manual (again, in the same section) mentions a mechanism that's part of the package's programming interface and which allows to know the NFSS family name that's auto-generated. However, it makes use of "expl" syntax, which I'm not familiar with.

• I did think this must be obviously a bad idea because... well, because the manual says so. But it doesn't really say why this isn't recommended. I've been trying to generate failures and every failure fails in just the same way with the standard commands, too. I hope one of the fontspec gurus answers this and explains it all. – cfr Jan 19 '16 at 23:39