7

I have imported a font from files and set it as the main font like this:

\setmainfont{EBGaramond}[
Path = fonts/eb-garamond/,
Extension = .ttf,
UprightFont = *12-Regular,
BoldFont = *08-Regular,
ItalicFont = *12-Italic,
BoldItalicFont  = *08-Italic]

I now wish to assign an alias to this, so that I can change the main font to something else and then come back to this without repeating this whole block again. I cannot seem to find a way to do it.

4
  • 1
    Why don't you use \newfontfamily\ebgaramondfamily... rather than \setmainfont, if you don't want it as the main document font?
    – cfr
    Aug 30, 2015 at 19:30
  • I basically wish for the option to swap main fonts later by using aliases. If I replace \setmainfont with \newfontfamily\ebgaramondfamily, and then do \setmainfont{\ebgaramondfamily}, the compilation freezes and never ends. Aug 30, 2015 at 19:45
  • See my (again) edited answer. This doesn't work for XeLaTeX because I can't get EB Garamond to work with XeLaTeX at all - even if I just do \setfontfamily... and nothing else - not even using the font - XeLaTeX goes into infinite-spinning mode. However, the method works with XeLaTeX for a font which works for that engine.
    – cfr
    Aug 30, 2015 at 20:32
  • 1
    You can put your settings in a file myalias.fontspec. See section 6.2. of the fontspec documentation. Aug 30, 2015 at 21:46

3 Answers 3

3

There are many ways to switch fonts using NFSS and fontspec commands. The other answers on this page are good and I'd suggest that @cfr's is probably the simplest and best; I just wanted to summarise everything that I could think of below.


Hooking into \rmdefault is probably a good idea, so I wouldn't recommend

\newfontfamily\mainone{...}[...]
\newfontfamily\maintwo{...}[...]

and switching between them with \mainone and \maintwo half-way through your text, since any internal command that requests \rmfamily will switch the font from what you've selected.


The next most simple method would be

\newcommand\switchone{%
  \setmainfont{...}[...]%
}
\newcommand\switchtwo{%
  \setmainfont{...}[...]%
}

and in the document you switch with \switchone and \switchtwo. Slightly inefficient to process the options every time, but at least it's simple. (Contrary to popular belief, re-running these commands shouldn't go about re-parsing and selecting the fonts from scratch, so it shouldn't be really that slow.)


Other answers have suggested variations on setting families and extracting the names used, either by writing

\setmainfont ...
\let\myfamone\rmfamily
...
\let\rmfamily\myfamone\normalfont

or extracting the family name from \l_fontspec_family_tl.

Here's another example along the same lines:

\newfontfamily\mainone{...}[NFSSFamily=rmone,...]
\newfontfamily\maintwo{...}[NFSSFamily=rmtwo,...]
\newcommand\switchmain[1]{\renewcommand\rmdefault{#1}\normalfont}

To switch between "default main fonts", you then need \switchmain{rmone} and \switchmain{rmtwo}. The benefit of this method is maximum flexibility.


Lastly, as Ulrike reminded me in the comments above, you can also use .fontspec files for this purpose. Normally .fontspec files are "supposed" to mirror the names of the fonts they're loading, but they can be completely abstract/semantic if you like. And in fact their contents don't need to be written in a separate file at all!

So you can switch main fonts using completely sensible markup without relying on defining additional commands using something like this:

\defaultfontfeatures[myfont]{
  Extension   = .otf ,
  UprightFont = texgyrepagella-regular ,
  BoldFont    = texgyrepagella-bold    ,
  % etc
}
...
\setmainfont{myfont}
\section{hello}
hello

In some ways I think this is the cleanest solution. I'll have to put my thinking cap to decide whether this interface is really ideal.

3
  • Thank you! Your answer is the most readable one in my opinion. Hence marked as the correct answer. I never knew about the NFSSFamily option. That is exactly the 'alias' I was referring to :) Aug 31, 2015 at 13:06
  • When I try to use multiple \defaultfontfeatures commands, I am getting an error: "kernel/key-unknown"! The key 'fontspec/Path' is unknown and is being ignored. Oct 4, 2015 at 19:57
  • 1
    Sorry, this could be a bug — I'm away and can't check straight away. I've added it to the issue tracker: github.com/wspr/fontspec/issues/216 Oct 5, 2015 at 9:30
7

You can simply use \newfontfamily rather than \setmainfont if you do not want to use the font as the default serif for the document. For example (set up for EB Garamond is different because your settings are not suitable for my system):

\documentclass{article}
\usepackage{fontspec}
\newfontfamily\ebgaramondfamily{EB Garamond}
\usepackage{kantlipsum}
\begin{document}
  \kant[1]
  {\ebgaramondfamily
    \kant[2]
    \itshape\kant[3]
  }
  \kant[4]
\end{document}

Obviously, this would be a rather nasty mix, typographically, but for illustrative purposes the switch between Latin Modern and EBGaramond demonstrates the mechanism involved:

font-switching

EDIT

If you want an easy way to switch the main document font, you can create a couple of convenience commands to change the default serif family although I'm not sure at all this is a good method:

\documentclass{article}
\usepackage{fontspec}
\newfontfamily\ebgaramondfamily{EB Garamond}
\setmainfont{TeX Gyre Pagella}
\let\tgpdefaultfamily\rmfamily
\newcommand*\activateebg{%
  \let\rmfamily\ebgaramondfamily
  \rmfamily}
\newcommand*\activatetgp{%
  \let\rmfamily\tgpdefaultfamily
  \rmfamily}
\usepackage{kantlipsum}
\begin{document}
  \kant[1]
  \activateebg
  \kant[2]
  \itshape\kant[3]
  \activatetgp
  \kant[4]
  \upshape
  \kant[5]
\end{document}

further font-switching

Edit: XeLaTeX now works thanks to Thérèse's comment.

10
  • That’s odd. I have no trouble with EB Garamond in either luatex or xetex (up-to-date “vanilla” TeX Live 2015 on Debian). Are you using the fonts from bitbucket.org/georgd/eb-garamond/downloads (which I use), or those in TeX Live? The latter don’t have the same file size as the former, and there’s a + added to the version number, so something was done to them that may be causing trouble.
    – Thérèse
    Aug 30, 2015 at 21:42
  • @Thérèse So my example works for you with XeTeX? As I say, I can't figure a way to use EB Garamond at all in that case. I'm not sure which fonts I'm using though - hang on, I'll do some digging.
    – cfr
    Aug 30, 2015 at 21:56
  • In your example, xetex seems to insist on a space between “EB” and “Garamond” (which I’ve always used anyway); luatex seems happy either way.
    – Thérèse
    Aug 30, 2015 at 22:02
  • @Thérèse LuaTeX is using the TL 2015 versions. I checked and the versions I have in /usr/share/fonts are identical to the TL ones.
    – cfr
    Aug 30, 2015 at 22:02
  • @Thérèse That's it! If I add the space, it works with XeTeX for me, too. But I do not understand why. If I run otfinfo -i, the only name matching EB Garamond is Preferred family. I earlier tried, for example EB Garamond 12 (which is the Family) and EB Garamond 12 Regular (which is the Full name) but it did not occur to me to try the Preferred one. What is that? Most opentype fonts I've looked at don't even have an entry for that, so far as I remember.
    – cfr
    Aug 30, 2015 at 22:06
5

Just after a declaration such as \setmainfont or \newfontfamily is executed, the allocated family name is available in

\l_fontspec_family_tl

so you can save it under a different name for later usage.

\documentclass{article}
\usepackage{fontspec}

\newfontfamily\ebgaramond{EB Garamond}
\ExplSyntaxOn
\tl_gset_eq:NN \ebgaramondfamily \l_fontspec_family_tl
\ExplSyntaxOff

\setmainfont{Futura} % just a very different font

\begin{document}

Some text in the main font

\begingroup               
\renewcommand{\rmdefault}{\ebgaramondfamily}\normalfont

Some text in EB Garamond \textit{also in italics}

\endgroup

Some text in the main font

\end{document}

enter image description here

You may want also to save the family name assigned to the main font:

\setmainfont{Futura} % just a very different font
\ExplSyntaxOn
\tl_gset_eq:NN \defaultrmfamily \l_fontspec_family_tl
\ExplSyntaxOff

so you can use \defaultrmfamily in the same way as \ebgaramondfamily.

1
  • 1
    Although \l_fontspec_family_tl is a defined interface you're probably slightly better off using \fontspec_set_family:Nnn for this. Aug 31, 2015 at 3:11

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .