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}
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}
Variant=1
in the\fontspec
declarations; if I add it in the code for\bigfont
, I get the same shapes for “Big”.setmainfont[Variant = 1...
inside the preamble, I would expect that property to be carried forward. Or do you mean that everytimefontspec
is called it resets theVariant
to a default?\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.\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.\setmainfont[SizeFeatures={Size=20}]{Zapfino}
in the preamble appears to work, but not when combined with other options?