# \setbox containing maths interferes with surrounding math mode in XeTeX

I encountered what might be a bug in XeTeX (but I would be glad to be proven wrong) while chasing a strange interaction between bytefield, unicode-math, and mathtools.

In general, I do not expect a construction of the form \setbox\mybox\hbox{...} to affect the surrounding code, as long as the box \mybox is not used anywhere else, and the ... do not perform global assignments. I would thus expect the macro \ignore in the example to typeset its argument and do nothing with it. Given what I feed it (simple characters, \mathchar), no assignment should be performed: \everymath and \everhbox are empty; besides, \tracingall confirms this absence of assignments. Despite this, the spacing of the surrounding math mode (which contains no character... this might be a clue) is affected, as can be seen by inspecting the boxes on the terminal.

\global\font\xxx"[latinmodern-math.otf]"\relax
\global\textfont 0\xxx
\newbox\mybox
\def\ignore#1{\setbox\mybox\hbox{#1}}
\def\showhbox#1{\setbox0\hbox{#1}\showbox0\relax}
\tracingall
\showhbox{${\ \over\ }\ignore{a}$}
\showhbox{${\ \over\ }\ignore{$\mathchara$}$}
\showhbox{${\ \over\ }\ignore{a}$}
\bye


Odder still, the boxes have sizes

\hbox(3.93732+3.44841)x5.73332
\hbox(3.94+3.45)x5.73332
\hbox(3.94+3.45)x5.73332


The third box, which has the exact same contents as the first, should have the same size, but it does not. I haven't checked whether this problem is specific to the font.

I guess my question is in two parts: (1) am I interpreting correctly what I see when thinking it is a bug? (2) (How) can I perform box assignments safely in XeTeX without affecting the surrounding maths?

• BTW, \setbox\mybox\hbox{...} has side effects, if LaTeX's color package is used. Internally it resets the color via \aftergroup, thus the color special can leak out of the box. Therefore LaTeX's box macros add an additional \begingroup...\endgroup layer. – Heiko Oberdiek May 19 '13 at 17:33
• It seems that XeTeX sets some parameter only when the first math formula containing a character is typeset. I get 3.93732+3.44841 if I typeset that with pdftex – egreg May 19 '13 at 18:12
• @egreg: replacing the first \  of each fraction by a still leads to differences, but they are more subtle (the horizontal rule is 0.4pt high for lines 1 and 3, and 0.39998pt high for line 2). BTW, how can you load that font with pdftex? – Bruno Le Floch May 19 '13 at 18:15
• With LuaTeX the dimensions are 3.93999+3.45. You also need to define \scriptfont1 for getting characters in the formulas. – egreg May 19 '13 at 18:18
• @BrunoLeFloch It is not relevant (therefore the "BTW"). – Heiko Oberdiek May 19 '13 at 18:21

From the following discussion I would say that this is an more or less undocumented feature of XeTeX. There seems to be two \fontdimen systems:

• The legacy \fontdimen according to Appendix G of "The TeXbook".
• The \fontdimen of the math table of the OpenType fonts.

The \fontdimens are listed in Appendices B and C of the documentation for package unicode-math.

Thus it seems that XeTeX switches from the legacy to the OpenType system, if the first OpenType math font is seen. And I suggest asking on the XeTeX mailing list for more details.

It is basically a bug.

When querying math dimensions, we check if the current font is an OpenType math font or not and either use OpenType math dimensions from the current font or traditional math dimentions from \fam1, \fam2 or \fam3. At the time the first fraction is typeset the current font is \nullfont and since that is not an OpenType math font we end up using traditional math dimensions, for the other fractions the current font is an OpenType math font so a different set of dimensions is used.

It seems the check for current font is bogus, since there is no guarantee it is the font the fraction is going to be set in (my own ignorance of the inner workings of TeX at show), so it needs to be handled in a better way, but I don’t know it yet.

• As far as I can tell, the correct fonts to look at when turning a math list to boxes are \textfont2, \scriptfont2 and \scriptscriptfont2 (see Appendix G of the TeXbook). Do you want me to report the bug on the SourceForge issue tracker? – Bruno Le Floch May 20 '13 at 1:26
• @BrunoLeFloch: The correct for what? TeX82 looks for math dimensions in \fam1, \fam2 or \fam3` according to the kind of the dimen it is looking for. But for OpenType math font everything is in the same font so we want to look into the font that will be used to typeset the subformula in question not any other arbitrary font e.g. if one switched fonts mid document the dimensions of the new font should be the one in effect. It would be nice if you can open an issue so this does not get missed. – Khaled Hosny May 20 '13 at 2:38
• Done – Bruno Le Floch May 20 '13 at 2:58
• This appears to be being fixed right now… – Will Robertson Oct 4 '16 at 23:26