# How do I access \fontdimen parameters of math fonts in the preamble?

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?

-

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.

-
Thanks a lot for these explanations! –  Hendrik Vogt Jan 30 '13 at 17:48

One approach is to do the set up inside a throw-away box at the start of the document

\documentclass{article}
\makeatletter
\AtBeginDocument{
\sbox{\@tempboxa}{%
\ensuremath{\xdef\mylength{\the\fontdimen17\textfont2}}%
}%
}
\makeatother
\begin{document}

\show\mylength

\end{document}


The reason this is needed is that LaTeX does not load maths fonts until required (due to the TeX limit on math alphabets), so we have to wait until the last moment to get the info, and also need to force math mode to actually load the fonts. At the same time, we don't want any output, hence using a box.

(siunitx uses the same trick to find out the relationship between \mathsf/\mathtt and \fam value.)

-
Thanks a lot for your answer! Maybe a stupid question: I just saw an old question of mine in the "Related" bar, where I tried \check@mathfonts as a way out. Can you say how this compares with your solution? –  Hendrik Vogt Jan 30 '13 at 16:56
\check@mathfonts? –  egreg Jan 30 '13 at 16:56
@HendrikVogt Also seems to work: not an approach I've tried before, so you might want to post your own answer. In the case where I use this approach I need to have \mathsf or \mathtt active, so have to use a box containing math mode. –  Joseph Wright Jan 30 '13 at 17:07
@egreg Does work here: try replacing the box with \check@mathfonts followed by the \edef part. –  Joseph Wright Jan 30 '13 at 17:07
@egreg: I gather this means "yes" - if you make this an answer, I'll accept it :-) (You see, I know a lot of TeX core stuff, but hardly any LaTeX.) –  Hendrik Vogt Jan 30 '13 at 17:26