I experienced a similar problem with superscripts. Searching for a solution I encountered your question.
Problem Description: Just typeset
\begin{equation}
F\le\frac{c^4}{4G}\frac{Mm}{(M+m)^2}
\end{equation}
The alignment is wrong.
Further search, which might contribute to a solution, revealed that:
Q#1 It is not a xelatex
bug, because typesetting with eulervm
as a math font and the mathspec
package does not lead to this behavior.
It is also not an error of opentype font metrics, because the above formulae appear correctly in Word 2010 using Cambria Math (this is embarrassing).
Therefore, I am led to believe that there is something rotten in the code of the unicode-math
package.
Q#2: Reading the documentation of the unicode-math
package, I didn't find any way of correcting this behavior except for modifying the source code, which I don't know how to do.
I think that this should be filed as an example of an unwanted behavior (or bug) to the maintainer of the unicode-math
package, in order to be corrected in future versions.
Compiling with lualatex
cannot be a general solution, because (at least in my case) I need polyglossia
's features, as my text involves English, Greek and some Russian text and polyglossia
has its problems with lualatex
(or the other way around?).
You can change more or less anything without changing your markup, the font size and position of superscripts is all customisable with care.
You didn't say too much about what you didn't like about the default setting. This shows some possibilities using different font sizes or setting the fontdimen parameters to affect the script positioning.
\documentclass{article}
\textwidth3cm
\begin{document}
1\[
A^{a_{{}_1}}
\]
2\[
A^{a_1}
\]
3\[
A^{\textstyle a_1}
\]
4\[
A^{\textstyle a_{\scriptscriptstyle 1}}
\]
\fontdimen16\the\scriptfont2=5pt
5\[
A^{a_1}
\]
\end{document}
Best Answer
I think you can justly regard this strange behaviour as kind of a bug in TeX's sub- and superscript positioning algorithm; it doesn't make sense that both the sub- and the superscript are raised. See below for some suggestions on how to fix the problem. At the end I offer a new positioning algorithm, and I compare it with the old algorithm. (Sorry for the length of this answer!)
What's happening?
The positioning algorithm is laid down in Rule 18 of Appendix G in the TeXbook. Here's what's happening: first, tentative positions of the sub- and superscripts are defined (Rules 18c and 18d). The problem arises in Rule 18e: only if the sub- and superscript are too close together (less than four times the default rule thickness, according to the tentative positions), TeX makes sure that the bottom of the superscript is at least as high above the baseline as 80% of the x-height. In this case, the superscript may be raised more than necessary for achieving the minimum distance of four times the default rule thickness, and then the subscript will be raised, too!
To illustrate the problem, I use an
m
as a subscript, and a rule with height 0 and different depths as superscripts:In the first case, the depth is
0.34ex
, in the second case it's0.35ex
: an imperceptible difference leading to rather different outputs. I don't think that this behaviour is desirable. Note that this only happens due to them
having a small height, not for letters likef
with ascenders.Let me point out that the same effect can occur in
\displaystyle
: then superscripts are set a bit higher by default so that the effect appears at a depth of0.46ex
versus0.47ex
. So it is a case of good luck that\displaystyle
helps in your case (as observed in David's answer). Let me point out that in "cramped" style (as used under\overline
and\sqrt
) the effect can be quite drastic:$\overline{a_n^ia_n^j}$
givesPossible ways to fix the problem
I see at least three completely different ways out of the problem; I'm not sure which one to prefer. The first idea (see David's second comment to this answer) is to add a strut to the subscript to give it so much height that TeX won't raise it. The following code defines a
\scriptstrut
that is just large enough; however, I think that the output is still not optimal: the second superscript is raised too much.Now you'd have to add a
\scriptstrut
to all the subscripts that are raised too high. One could try to automatize this by making the underscore_
active (see also mafp's second comment), but it's not easy to do this in a safe way: note that, e.g.,$a_\frac12$
is valid (albeit not recommendable) TeX code.Here's my second idea: by default, TeX places subscripts lower if a superscript is present. You can prevent this by adding
to your preamble. This doesn't completely suppress the problem, but it makes it a lot less visible. However, there could be other side effects.
The new positioning algorithm
Probably the best way is to use another algorithm altogether for the vertical positioning of a sub-/superscript combination. I offer such a new algorithm in the code below, where I define an
\nss
command (like "natural sub- and superscripts") that takes three arguments, the 2nd being the sub- and the 3rd being the superscript of the 1st.The rules I use are as follows, keeping as close to the original algorithm as possible: Use the tentative default positions as the TeX engine would do it. To achieve that the sub- and superscript are at least four times the default rule thickness apart, raise the superscript, but only as long as its bottom is at most 80% of the x-height above the baseline. If the distance between sub- and superscript is still too small, lower the subscript.
Finally, a somewhat more extensive comparison between the original and the improved algorithm. I use a box of width
2pt
as a subscript and a box of width1pt
as a superscript. The subscript has varying heights (but depth0pt
), the superscript has varying depths (but height0pt
). You see a lot of wobbling with the original algorithm!