[Tex/LaTex] Set font family default after loading it

fontsfontspec

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.

Best Answer

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.