[Tex/LaTex] Change font size with fontspec package

fontsfontspecxetex

What is the recommended approach to change font size with the fontspec package?

I have found several ways that appear to work, but some unexpected behavior along the way. What am I doing wrong?

I switch font size with \fontsize{30pt}{36pt}\selectfont and also define a command based on it. Quite unexpectedly, as different switches are applied, the font switches across different variants. Unexpected to me. Please tell me why this is happening.

I am compiling the document with XeLaTeX. In my MWE I use the Zapfino font because that's what I was experimenting with when the unexpected behaviour occurred.

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode
\documentclass{article}
\usepackage{fontspec}
%% with fontspec package
%% How do you set font size inside the preamble?
%%
%% IN PREAMBLE
%%     1. SUCCESS
       \setmainfont[Variant = 1, Ligatures = {Common,Rare}]{Zapfino}%
%%     \setmainfont[SizeFeatures={Size=20}]{Zapfino}% appears to work...
%%     % The option SizeFeatures is not intended to set a size. It is intended to set features that are activated only for some font sizes.
%%
%%     2. CRASHES
%%     \setmainfont[Variant = 1,
%%                  SizeFeatures={Size=20}, # why doesn't this crash above?
%%                  Ligatures = {Common,Rare}
%%                  ]{Zapfino}%
%%
%%     3. NO EFFECT
%%     \fontsize{20pt}{24pt}\selectfont
%%

\begin{document}
%% %% How do you switch font size inside the document?
%% Options set after \begin{document}
%%     4. SUCCESS
%%     \fontspec[Ligatures={Common,Rare}]{Zapfino}%
%%     \fontsize{20pt}{24pt}\selectfont
%%
%%     5. ERROR. 
%%     \addfontfeature{Size=20}
%%
%%     6. CRASHES
%%     \addfontfeature{SizeFeatures={Size=20}}
%%
%%     7. SUCCESS
     \newcommand{\smallfont}[1]{%
         \fontspec[Ligatures={Common,Rare}]{Zapfino}%
         \fontsize{10pt}{12pt}\selectfont #1}
     \newcommand{\normfont}[1]{%
         \fontspec[Ligatures={Common,Rare}]{Zapfino}%
         \fontsize{20pt}{24pt}\selectfont #1}
     \newcommand{\bigfont}[1]{%
         \fontspec[Ligatures={Common,Rare}]{Zapfino}%
         \fontsize{30pt}{36pt}\selectfont #1}

\fontsize{30pt}{36pt}\selectfont
    Big 
\fontsize{20pt}{24pt}\selectfont
    and 
\fontsize{10pt}{12pt}\selectfont
    Small

\bigfont{Big} \normfont{and} \smallfont{Small}

Why are different variants of the font selected?

\end{document}

enter image description here

As egreg has explained in a comment and further in an answer, "With SizeFeatures={Size=20} you're telling to use Zapfino ONLY at size 20pt." Illustration:

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode
\documentclass{article}
\usepackage{fontspec}
\setmainfont[Variant = 1, Ligatures = {Common,Rare}]{Zapfino}%
\setmainfont[SizeFeatures={Size=20}]{Zapfino}%

\begin{document}

\fontsize{30pt}{36pt}\selectfont
    Big is not Big

\fontsize{20pt}{24pt}\selectfont
    20pt is 20pt

\fontsize{10pt}{12pt}\selectfont
    Small is not Small

\end{document}

enter image description here

Best Answer

First of all, let's get away with the SizeFeatures option. If you declare

\setmainfont{Zapfino}[
  SizeFeatures={Size=20},
  % ... other options ...
]

you're basically telling to use size 20 independently of the context. If I do it and ask for \fontsize{30}{36}\selectfont, I get

LaTeX Font Warning: Font shape `EU1/Zapfino(0)/m/n' in size <30> not available
(Font)              size <20> substituted on input line 22.

Second issue. When you do \fontspec{<font>}[<options>], you're declaring a new font. In your case, Variant=1 is not inherited.

You have to do nothing special to change size:

\documentclass{article}
\usepackage{fontspec}

\setmainfont{Zapfino}[
  Variant = 1,
  Ligatures = {Common,Rare},
]

\newcommand{\smallfont}[1]{{%
  \fontsize{10pt}{12pt}\normalfont #1%
}}
\newcommand{\normfont}[1]{{%
  \fontsize{20pt}{24pt}\normalfont #1%
}}
\newcommand{\bigfont}[1]{{%
  \fontsize{30pt}{36pt}\normalfont #1%
}}

\begin{document}

\fontsize{30pt}{36pt}\selectfont
    Big 
\fontsize{20pt}{24pt}\selectfont
    and 
\fontsize{10pt}{12pt}\selectfont
    Small

\normalsize

\bigfont{Big} \normfont{and} \smallfont{Small}

Why are different variants of the font selected?

\end{document}

enter image description here