17

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

6
  • You don't have Variant=1 in the \fontspec declarations; if I add it in the code for \bigfont, I get the same shapes for “Big”.
    – egreg
    Commented May 15, 2016 at 11:12
  • How do you mean? It is in setmainfont[Variant = 1... inside the preamble, I would expect that property to be carried forward. Or do you mean that everytime fontspec is called it resets the Variant to a default?
    – PatrickT
    Commented May 15, 2016 at 11:15
  • With \fontspec you're declaring a “new font” to use. With \newcommand{\bigfont}[1]{{\fontsize{30}{36}\normalfont #1}} you achieve the result. Note the additional braces.
    – egreg
    Commented May 15, 2016 at 11:17
  • I see. So everytime I invoke \fontspec I need to recall all the font properties, even if my purpose was only to, say, change the size? I think this would answer my question, if you wish to go ahead I'll accept it. Thanks.
    – PatrickT
    Commented May 15, 2016 at 11:20
  • Oh, and do you know why \setmainfont[SizeFeatures={Size=20}]{Zapfino} in the preamble appears to work, but not when combined with other options?
    – PatrickT
    Commented May 15, 2016 at 11:21

1 Answer 1

18

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

5
  • 1
    Thanks! I was confused by various things, but the statement Variant=1 is not inherited sums up my biggest misunderstanding.
    – PatrickT
    Commented May 15, 2016 at 11:42
  • 1
    Am I correct that I can pass Color and Variant to \addfontfeature{} but I cannot pass a size argument such as Size or similar?
    – PatrickT
    Commented May 15, 2016 at 11:55
  • 2
    @PatrickT Yes, font size should be controlled on the LaTeX side.
    – egreg
    Commented May 15, 2016 at 12:27
  • 2
    The answer answers OP's question 'How do you switch font size inside the document?', but it doesn't answer OP's other question: 'How do you set font size inside the preamble?' Do you use \fontsize{<fontsize>pt}{<baselineskip>pt}\selectfont in the preamble too? Is there a way to pass the size option to fontspec or do you need to let LaTeX take care of it by using \fontsize?
    – Evan Aad
    Commented Jun 13, 2017 at 7:34
  • 5
    @EvanAad You can use Scale=<factor>
    – egreg
    Commented Jun 13, 2017 at 8:08

You must log in to answer this question.

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