# unicode-math superscripts don't line up (using XeLaTeX)

Pretty much what the title says, and that is bugging me a lot. I'm using the unicode-math package because I enjoy the upright bold greek letters it provides, as I use them for vector notation, but I noticed this weird problem. PS: I'm using XeLaTeX.

MWE:

\documentclass{article}

\usepackage{unicode-math}

\begin{document}
$\sin^{2}x+\cos^{2}x=1$
\end{document}


PS 2: This is a Windows ruler I got from the fullscreen snip tool.

Subscripts seem to be much less affected.

Edit: As Ulrike Fischer pointed out, I had to fix the other trig macros as well, so I copy-pasted his code for other operators (all based on the spacing of the \cos macro).

I'll leave the code just below, so that anyone with the same problems doesn't have to bother.

\makeatletter
\def\sin{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font sin}}}
\def\sinh{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font sinh}}}
\def\cosh{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font cosh}}}
\def\tan{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font tan}}}
\def\tanh{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font tanh}}}
\def\cot{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font cot}}}
\def\coth{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font coth}}}
\def\sec{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font sec}}}
\def\csc{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font csc}}}
\makeatother


Edit 2: I've recently run into some problems using Ulrike Fischer's solution, namely when using hyperref's \texofpdfstring command with the altered macros. A viable solution I found is to redefine the macros above with

\let\cmd\relax\DeclareMathOperator{\cmd}{definition}


instead of just using \def\cmd.

As before, I'll provide the code for the other macros:

\makeatletter
\let\sin\relax\DeclareMathOperator{\sin}{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font sin}}}
\let\sinh\relax\DeclareMathOperator{\sinh}{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font sinh}}}
\let\cosh\relax\DeclareMathOperator{\cosh}{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font cosh}}}
\let\tan\relax\DeclareMathOperator{\tan}{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font tan}}}
\let\tanh\relax\DeclareMathOperator{\tanh}{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font tanh}}}
\let\cot\relax\DeclareMathOperator{\cot}{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font cot}}}
\let\coth\relax\DeclareMathOperator{\coth}{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font coth}}}
\let\sec\relax\DeclareMathOperator{\sec}{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font sec}}}
\let\csc\relax\DeclareMathOperator{\csc}{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font csc}}}
\makeatother

• On a MacTeX2020 system, I can confirm that this issue arises not only with XeLaTeX but also with LuaLaTeX, though happily not quite as extremely. Until the unicode-math package is fixed, a quick-and-dirty fix is to run \usepackage{amsmath} \let\oldsin\sin \let\sin\relax \DeclareMathOperator{\sin}{\smash[t]{\oldsin}}.
– Mico
Jun 3, 2020 at 2:19
• Just for completeness: \smash[t] requires the amsmath package. \smash (without [t]) should work as well.
– Mico
Jun 3, 2020 at 2:27
• Although it is not a solution, I just noticed that the issue is not present when using XITS Math as math font.
– Ivan
Jun 20, 2020 at 8:22

As wipet pointed out the placement depends on the height of the operator. You can see a similar effect (but much less pronounced) also with classical tex if you use large enough letters:

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}

\def\sin{\qopname\relax o{SIN}}
\begin{document}
$\tikz[overlay]\draw[red](0,4.4pt)--++(5,0); \sin^{2}x+\cos^{2}x=1$
\end{document}


This basically means that the amsopn definitions of these operators assume that the height differences don't matter -- an assumption which is no longer true in the context of unicode-math.

You can correct the height for example like this. As can be seen such corrections would be needed for other operators too:

\documentclass{article}

\usepackage{unicode-math}
\usepackage{amsmath}
\usepackage{tikz}
\makeatletter
\def\sin{\qopname\relax o{\vphantom{\operator@font cos}\smash{\operator@font sin}}}
\begin{document}
$\tikz[overlay]\draw[red](0,4.4pt)--++(5,0); \cot^2 \tanh^2 \sin^{2}x+\cos^{2}x=1$
\end{document}


I experimented with LuaTeX, no XeTeX, but the problems are similar.

There are many position parameters read from Unicode math font. They are listed at pages 104--108 of LuaTeX manual. Main rules of vertical position of the exponent are controlled by two parameters:

• \Umathsupshiftup minimal shifting up. (10pt typesetting, Latin Modern math, displaystyle, value: 3.63pt)

• \Umathsupshiftdrop bottom of the exponent is shifted up to the height of the nucleus minus this value (10pt typesetting, Latin Modern math, displaystyle, value: 2.5pt)

Suppose, that we have "cos" in the nucleus and "2" in the exponent. First, the "2" is shifted up to the height of "cos" and then shifted down by \Umathsupshiftdrop. If the distance from baseline to bottom of such "2" is less than \Umathsupshiftup then forget previous calculation and shift up the "2" from baseline to \Umathsupshiftup. This second case is true when "cos" in in nucleus. But if "sin" is in the nucleus, then first case is true because "i" is higher than "cos". We can set

\Umathsupshiftdrop\displaystyle=3pt


then the second case will be true for both: "sin" and "cos" and the exponent will be in the same height.

The parameters mentioned here are read from Unicode math font table (MathConstants), so the different positions of the exponent in "sin" and "cos" can be considered as bug (or feature?) of Latin Modern fonts.

I don't know, why XeTeX gives much more difference in this positioning than LuaTeX. The \Umathsupshiftdrop is represented as \fontdimen24 in XeTeX. You can do experiments with this.

• Are you sure that what the OP has encountered is a bug/feature of the Latin Modern fonts? Given that the odd exponent positioning behavior (a) remains if one adds an instruction such as \setmathfont{Asana Math} and (b) disappears if the unicode-math package is not loaded, I would surmise that we're dealing with a bug in unicode-math.
– Mico
Jun 3, 2020 at 7:37
• @Mico I am convniced that unicode-math package only copies the mentioned parameters from the Latin Modern Math font. For example, I am not using this package but I see the same issue in LuaTeX with Unicode Latin Modern Math. TeX macro package can do nothing more. Unless it include something like "if LMmath loaded then do correction". But this is unconceptual. Jun 3, 2020 at 8:35
• If unicode-math is not loaded in LaTeX, then it means that another metric information from another 7bit fonts (similar to Latin Modern) is used. And exactly appendix G of TeX book is used for positioning in math. But nothing from these is true when Unicode math is loaded. Moreover, the metric from 7bit and Unicode fonts differ: there is 1/1000 em precision limit in OTF but 1/2^16 pt precision limit in TFM. Jun 3, 2020 at 8:40
• The same behaviour (i.e. superscripts don't line up in xelatex) also occurs with the stix2 math fonts with or without the unicode-math package. But not, as far as I can tell, with the commercial (and excellently designed) minion math fonts. Jun 9, 2020 at 19:19