[Tex/LaTex] How to access \fontdimen parameters of math fonts in the preamble

fontsmath-mode

I want to do some computation with the \fontdimen parameters of the standard math fonts to compute the appropriate length of a \scriptstrut (see this answer of mine). I'd prefer to do the computation once in the preamble and not at every occurence of the \scriptstrut. However, the problem is that the standard math fonts are only loaded when the first bit of math occurs, so something like

\edef\mylength{\the\fontdimen17\textfont2}

doesn't work in the preamble. One could use \AtBeginDocument, but then still the fonts are not loaded. Is there some "official" way around this problem?

Best Answer

Only the four main math families have a fixed family number: number 0 is the "math roman" font, number 1 is "math italic", number 2 is "math symbols" and number 3 is "math extensions". All the other defined alphabets or math symbol fonts get a family number (mathgroup number in LaTeXspeak) at their first use.

The main LaTeX macro that does the business of associating fonts to math families is \check@mathfonts, that gets called for any math formula. The correspondence is not fixed, because fonts must change according to the current font size.

You can access the fontdimen parameters once a font has been associated to a mathgroup. So

\newlength{\mylen}
\makeatletter
\AtBeginDocument{
  \check@mathfonts\setlength{\mylen}{\fontdimen17\textfont2}
}
\makeatother

should do. It's important to do it \AtBeginDocument since many math font packages do their business at that moment.

The "box" approach is handy if what you want to access to is not one of the main four families; something has to be typeset in order to associate to, say, \mathtt, a mathgroup number. For instance

\sbox0{$\mathtt{\global\mylen=\fontdimen2\textfont\mathgroup}$}

will access the second fontdimen parameter relative to the font associated to \mathtt.