[Tex/LaTex] way to use different local font files for different font size in fontspec

fontsizefontspec

I am a big lover of the EB-Garamond fonts by Georg Duffner. As of now, he has made two sets of font files, EB-Garamond-8 and EB-Garamond-12, respectively for “design size 8pt” and “design size 12pt”. Is there a way to make fontspec (and possibly unicode-math, too) use EB-Garamond-8 fonts for small sizes e.g. sub/superscripts, footnotes… and EB-Garamond-12 fonts for regular sizes when using local font files¹?

If I use only EBGaramond12, the following code works if I put the EBGaramond12-* .otf files in a fonts/ directory sibling to the .tex²

\setmainfont[
    Path=fonts/,
    Extension=.otf,
    UprightFont=*-Regular,
    ItalicFont=*-Italic,
    BoldFont=*-Bold,
    RawFeature={+clig,+dlig,+cv11},]{EBGaramond12}

but if I try to adapt it to use the SizeFeatures options

\setmainfont[
    Path=fonts/,
    Extension=.otf,
    UprightFont=*-Regular,
    ItalicFont=*-Italic,
    BoldFont=*-Bold,
    RawFeature={+clig,+dlig,+cv11},
    SizeFeatures={
        {Size={-12}, UprightFont=EBGaramond8-Regular, ItalicFont=EBGaramond8-Italic},
        {Size={12-}, UprightFont=EBGaramond12-Regular, ItalicFont=EBGaramond12-Italic, BoldFont=EBGaramond12-Bold}
    }]{EBGaramond12}

Then the document won't compile and I get the following error. Using complete names instead of wildcarded ones changes nothing.

The key 'fontspec/UprightFont' is unknown and is being ignored.

¹ It means that using features that automagically select the right system fonts is not an option.

² the file structure is

.
├── fonts
│   ├── EBGaramond08-Italic.otf
│   ├── EBGaramond08-Regular.otf
│   ├── EBGaramond12-Bold.otf
│   ├── EBGaramond12-Italic.otf
│   └── EBGaramond12-Regular.otf
└── paper.tex

Best Answer

Since version 2.4 of fontspec, it has been possible to use different local fonts for different optical sizes, cf. the README.

The trick is to specify the different fonts with a Font option embedded under SizeFeatures, which in turn (and this is the crucial part) must be embedded under the options UprightFeatures, ItalicFeatures, and so on, cf. the fontspec manual under section 6.6.

Here's a minimal example:

\documentclass{article}
\usepackage{fontspec}
    \setmainfont{EBGaramond}[
        Path = ./fonts/,
        Extension = .otf,
        UprightFont = *12-Regular, % set EB Garamond 12 as default upright font
        UprightFeatures = {
            SizeFeatures = {
                {Size = -10.1,
                    Font = *08-Regular}, % use EB Garamond 08 for 10pt size and smaller
                {Size = 10.1-}}}, % use default upright font for larger than 10pt size
        ItalicFont = *12-Italic, % set EB Garamond 12 as default italic font
        ItalicFeatures = {
            SizeFeatures = {
                {Size = -10.1,
                    Font = *08-Italic}, % use EB Garamond 08 for 10pt size and smaller
                {Size = 10.1-}}}, % use default italic font for larger than 10pt size
        SmallCapsFeatures = {Letters = SmallCaps},
        Ligatures = {Common, TeX},
        Numbers = {Proportional, OldStyle}]
\begin{document}

EB Garamond 08: The quick brown fox jumps over the lazy dog 1234567890

\textit{EB Garamond 08: The quick brown fox jumps over the lazy dog 1234567890}

\large
EB Garamond 12: The quick brown fox jumps over the lazy dog 1234567890

\textit{EB Garamond 12: The quick brown fox jumps over the lazy dog 1234567890}

\end{document}

enter image description here