Why does fontspec not handle setting the font size in points

fontsfontsizefontspecpoints

I would like to set the exact size, in points, of a font in my TeX document. Perusing through the fontspec documentation, I did not find a lot of information related to setting the font size. So I turned to google. Several posts suggest using \fontsize combined with \selectfont, including:

My question is twofold:

  • Why does fontspec not handle this?
  • Where do \fontsize and \selectfont come from? Are the LaTeX primitives? Are they part of a library?

Best Answer

You asked, "Where do \fontsize and \selectfont come from? Are the[y] LaTeX primitives? Are they part of a library?"

  • Both macros are defined by the LaTeX2e kernel, or format. The term "primitive" [command] has a special meaning in TeX and LaTeX circles, so it's not wise to call \fontsize and \selectfont "primitives".

  • Just as the original "Plain TeX" format consists of a set of primitives (frequently, but not universally, referred to as "Knuth TeX") and a set of macros that build on these primitives (both the primitives and the aforementioned macros are explained in full detail in the TeXbook), the LaTeX2e format consists of a set of primitives (which these days come from eTeX, not Knuth TeX) and a set of macros defined in the LaTeX2e kernel. The LaTeX kernel may be found in the file latex.ltx.

  • In the current version of latex.ltx ("LaTeX2e <2021-06-01> patch level 1"), the macro \fontsize is defined on lines 7036 and 7037 of the file, as follows:

    \DeclareRobustCommand\fontsize[2]
       {\set@fontsize\baselinestretch{#1}{#2}}
    

    The macro \set@fontsize is defined on lines 7985 thru 8002 as follows:

    \def\set@fontsize#1#2#3{%
        \@defaultunits\@tempdimb#2pt\relax\@nnil
        \edef\f@size{\strip@pt\@tempdimb}%
        \@defaultunits\@tempskipa#3pt\relax\@nnil
        \edef\f@baselineskip{\the\@tempskipa}%
        \edef\f@linespread{#1}%
        \let\baselinestretch\f@linespread
          \def\size@update{%
            \baselineskip\f@baselineskip\relax
            \baselineskip\f@linespread\baselineskip
            \normalbaselineskip\baselineskip
            \setbox\strutbox\hbox{%
              \vrule\@height.7\baselineskip
                    \@depth.3\baselineskip
                    \@width\z@}%
            \let\size@update\relax}%
      }
    
  • The macro \selectfont, in turn, is defined on lines 7954 thru 7985 as follows:

    \DeclareRobustCommand\selectfont
            {%
        \ifx\f@linespread\baselinestretch \else
          \set@fontsize\baselinestretch\f@size\f@baselineskip \fi
        \ifx\delayed@f@adjustment\@empty
        \else
          \let\f@shape@saved\f@shape
          \let\f@series@saved\f@series
          \delayed@f@adjustment
          \maybe@load@fontshape
          \ifcsname \f@encoding/\f@family/\f@series/\f@shape \endcsname
          \else
            \let\f@shape\f@shape@saved
            \let\f@series\f@series@saved
            \let\delayed@merge@font@shape\merge@font@shape
            \let\delayed@merge@font@series\merge@font@series
            \delayed@f@adjustment
            \let\delayed@merge@font@shape\merge@font@shape@without@substitution
            \let\delayed@merge@font@series\merge@font@series@without@substitution
          \fi
          \let\delayed@f@adjustment\@empty
        \fi
        \@forced@seriesfalse
        \xdef\font@name{%
          \csname\curr@fontshape/\f@size\endcsname}%
        \pickup@font
        \font@name
        \UseHook{selectfont}%
        \size@update
        \enc@update
        }
    \NewHook{selectfont}
    
  • As the code demonstrates, both macros are quite complex and rely on a sizable number of subsidiary macros to get much of their work done.

  • You also asked, "Why does fontspec not handle this?" As @UlrikeFischer and @Cicada have already pointed out in comments, there's no need or justifiable reason for making fontspec perform tasks that are handled perfectly well by the existing LaTeX macros \fontsize and \selectfont.