3

In Plain TeX (and really as a TeX primitive), \font\myfont=cmr8 at 10pt and \font\myfont\cmr10 both create fonts with "sizes" of 10pt. I want to be able to use this value to generate specific vertical spacing (e.g. always double, single, 1.1, 1.2 spaced) in a redefined macro of \font (it would use \afterassignment with \font to retain the normal semantics, especially the variability of putting a) nothing, b) at, or c) scaled). \fontdimen doesn't appear to provide this dimension. Although hacky, is there a representative character that I could create a box with and measure the height of that, or is there a more natural way to get this value?

I am able to use a macro \def\font#1=#2@#3{...}, but this breaks the old semantics. Note: this is a plain TeX question, so I want to avoid relying on higher level font frameworks.

2
  • 1
    I would use \fontcharht\font`\( + \fontchardp\font`\( Jun 24, 2020 at 8:47
  • 1
    if pdftex is an option, you could use \pdffontsize
    – Robert
    Jun 24, 2020 at 14:45

2 Answers 2

5

If you want to get the actual size instead of an approximation like the size of the parenthesis, you can use \fontname: \fontname always includes the size, except when the font is loaded in it's designsize. But if the font is loaded in it's designsize, you can load it again at a scaled size and extract the designsize from that.

The actual code looks a bit complicated because we have to scan for at in cmr10 at 12pt and the at has weird catcodes:

\begingroup
  \edef\specialAt{{ \string a\string t }}%
  \def\x#1{%
    \def\getfontsize##1=##2{% The user level macro
      \expandafter\getfontsizE\fontname##2#1\relax{##1}%
    }%
    \def\getfontsizE##1#1##2\relax##3{%
      \ifx*##2*% Loaded at designsize
        \begingroup
          \font\tmpfont"##1" scaled 2000
          \getfontsize\dimen0=\tmpfont
          \divide\dimen0 by 2
          \expandafter
        \endgroup
        \expandafter\getfontsiZe\the\dimen0\relax{##3}%
      \else% Otherwise #2 holds the size. But is is followed by " at ", so we have to strip that
        \getfontsiZE{##3}##2%
      \fi
    }%
    \def\getfontsiZE##1##2#1{##1=##2}% Strip " at "
    \def\getfontsiZe##1\relax##2{##2=##1}
  }
  \expandafter\expandafter\expandafter
\endgroup
\expandafter\x\specialAt

% Let's try using it: What is the size of \tenrm?
\getfontsize\dimen0=\tenrm
{\tt\string\tenrm} has size \the\dimen0.

\font\cmrXVII=cmr17\relax
\getfontsize\dimen0=\cmrXVII
cmr17 has size \the\dimen0.
\bye

Of course, you can combine this with egreg's answer to automatically assign the size of every loaded font to some macro.

enter image description here

1
  • I have replaced this as the accepted answer because the parenthesis method breaks on ptmr/ptmb (Times New Roman) fonts. Jun 26, 2020 at 1:19
3

You can define \xfont with the same syntax as \font, but so that \xfont\foo=cmr10 will also define \foosize with replacement text the height plus depth of a parenthesis in the font (as suggested by Ulrike Fischer).

\def\xfont#1{%
  % #1 = font selection control sequence
  \begingroup\escapechar=-1
  \edef\x{\endgroup\def\noexpand\fontnametmp{\string#1}}\x
  \afterassignment\setfontsize\font#1%
}
\def\setfontsize{%
  \setbox0=\hbox{\csname\fontnametmp\endcsname(}%
  \dimen0=\ht0 \advance\dimen0 \dp0
  \expandafter\edef\csname\fontnametmp size\endcsname{\the\dimen0}%
}

\xfont\fontA=cmr8 at 10pt
\immediate\write20{\fontA = \meaning\fontA\space --- \fontAsize}

\xfont\fontB=cmr5 scaled 2000
\immediate\write20{\fontB = \meaning\fontB\space --- \fontBsize}

\xfont\fontC=cmr17
\immediate\write20{\fontC = \meaning\fontC\space --- \fontCsize}

\xfont\fontD=cmbx10
\immediate\write20{\fontD = \meaning\fontD\space --- \fontDsize}

\bye

This will produce, on the terminal,

\fontA = select font cmr8 at 10.0pt --- 10.0pt
\fontB = select font cmr5 at 10.0pt --- 10.0pt
\fontC = select font cmr17 --- 17.24997pt
\fontD = select font cmbx10 --- 10.0pt
4
  • Why does cmr17 have a size of 17.24997pt? Jun 24, 2020 at 15:30
  • 1
    @ShreevatsaR Because that's the height plus depth of the parenthesis. The size declared in the TFM file is actually (DESIGNSIZE R 17.279999), but this information is not available to TeX.
    – egreg
    Jun 24, 2020 at 15:44
  • Thanks. Why doesn't the same apply to cmr8, cmr10, cmr12 (or in the above, cmbx10) — why don't they also have higher sizes than in the font name? (Sorry, I know this isn't strictly relevant to this question/answer and maybe I can look it up somewhere, just asking because it seems interesting.) Jun 24, 2020 at 17:10
  • @ShreevatsaR I guess you have to ask Knuth for this.
    – egreg
    Jun 24, 2020 at 17:19

You must log in to answer this question.

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