# Bug in xelatex + unicode+math + vphantom [closed]

This question is a simplified version of a slightly more complicated problem with XITS Math: Consider the following code:

\documentclass{article}
\usepackage{unicode-math}
\begin{document}
$$|^a$$ $$\vphantom{|}^a$$

$$|^a$$ $$\mathord{\vphantom{|}}^a$$

$$|^a$$ $$\mbox{\vphantom{|}}^a$$
\end{document}


In the first line, the vertical positions of the two occurrences of 'a' are the same when compiling with lualatex (which is expected) but not when compiling with xelatex (which I consider a bug). The second and third lines represent unsuccessful attempts to repair. Any hints?

• A box can never be the nucleus for a math atom, so it cannot receive superscripts. You have to brace the \vphantom. – egreg Feb 26 '16 at 21:08
• Like this: \mbox{{\vphantom{$|$}}}^a ? – Frank Feuerstern Feb 26 '16 at 21:12
• Yes, that's what I mean. – egreg Feb 26 '16 at 21:17
• Additionally, there are fontdimen scaling problems in XeTeX, see tex.stackexchange.com/questions/281549/… – Henri Menke Feb 26 '16 at 21:19
• I'm voting to close this question as off-topic because the bug is fixed in TL2017. – Henri Menke Jun 11 '17 at 1:21

I am making this an answer only because it doesn’t fit in a comment: It looks like XeTeX “remembers” the math style that was in force when it last typeset a symbol inside a formula, and continues to apply it for choosing certain \fontdimen parameters in subsequent formulas as long as they begin with an atom whose nucleus is empty (or contains an empty math list, which has the same effect). Consider

\documentclass[a4paper]{article}
\usepackage{unicode-math}

\begin{document}
Just try:
$^a$ $\scriptscriptstyle x$ $^a$ $\mathchoice{}{}{}{}^a$ $x^a$ $^a$
\end{document}


whose output is

However, this only happens when the unicode-math package is loaded.

This error seems to occur during the process of transforming a math list into its horizontal equivalent, as if XeTeX failed to initialize the “current style” in some situations, or more precisely some information pertaining to the choice of the \fontdimen parameters involved in typesetting the superscript. But this is only a guess (after all, the question asks for “any hints”).

Where does the behavior described in the question come from, then? Well, \vphantom{|}, when used in math mode, invokes a primitive command of TeX called \mathchoice, which—to put it simply—typesets the “|” character in all the four math styles (\displaystyle, \textstyle, \scriptstyle, and \scriptscriptstyle) in order to figure out how much tall the phantom should be in each of the four styles. Thus, the last thing \vphantom{|} does is to typeset $\scriptscriptstyle |$, and when the closing $ is sensed, this formula is translated into a horizontal list (and saved into a temporary box, but this doesn’t matter here); if our guess is correct, this leaves in some place the information that the \fontdimen parameters should be chosen for a “current style” equal to \scriptscriptstyle. Then, (Xe)TeX returns to the (still empty) math list that it was constructing when it saw the \vphantom{|}, and appends a “four-way choice” node to it, containing the four versions of the phantom, one for each of the possible styles. Next comes the ^a. Now, a fact that seems to be easily forgotten is that a math list can contain several kinds of nodes (The TeXbook lists them all at the bottom of page 157), but that only atoms can carry sub/superscripts; a “four-way choice” node can’t, so (page 291, description of the “superscript” command) an Ord atom with an empty nucleus is appended to the list, and the “a” becomes it superscript. At this point the math list looks like this: \mathchoice D\mathord D.\hbox(7.5+2.5)x0.0 T\mathord T.\hbox(7.5+2.5)x0.0 S\mathord S.\hbox(5.24998+1.75)x0.0 s\mathord s.\hbox(3.75+1.25)x0.0 \mathord ^\fam0 𝑎  This list is then converted to its horizontal equivalent. According to our hypthesis, XeTeX still has some “dirty” variable saying that the \fontdimen parameters should be those of \scriptscriptstyle; at this point, everything goes as in the case of $\mathchoice{}{}{}{}^a\$ in our code, above: for some reason, the “four-way choice” node containing just empty boxes does not reset the \fontdimen information, and, according to Rule 4 of Appendix G, leaves just an Ord atom without sub/superscript, which is processed by Rules 14, 17, and 18, yielding simply an empty box of some (unimportant) height; then comes the Ord atom with empty nucleus, and Rule 14 just passes it over to Rule 17, that in turn, being the nucleus empty, goes on to Rule 18, which is the rule that takes care of positioning the sub/superscript. Thus we see that, in our hypotheses, Rule 18 is applied while the \fontdimen information is still “dirty”.

(Deep breath.)