# Uneven height when using superscript in frac

I want to fine tune some of mine equations, when I encountered this

Notice, how the superscript in the derivative operator fits nicely and doesn't lower the symbols in the denominator, while the \sin^2\vartheta lies below every other term. I thought this due to the operator definition of \sin but replacing it by \mathrm{sin} still produces the same output.

Since this term appears in a line of all similar terms this stands out noticeably.

What I got so far is an intermediate solution with the subdepth package, which produces

where at least the single term is on the same line. I would be thankful for any creative solution to tackle this :)

-

The problem is in the “i”, where the ascender is setting the overall height of the nucleus in \sin^2.

You can fix this by smashing the “i”; however, this would create problems if you do \overline{\sin x}; a solution is to add another phantom before the real operator. I add a \mathop atom containing the phantom for the real name, followed by \! so the thin space that TeX inserts between two consecutive \mathop atoms is removed.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\sinname}{sin}% change here

\newcommand{\sin@name}{%
\smash{\operator@font\sinname}\vphantom{s}%
}
\DeclareRobustCommand{\sin}{%
\mathop{\vphantom{\operator@font\sinname}}\!%
\qopname\relax o{\sin@name}%
}
\makeatother

\begin{document}
\begin{gather*}
\frac{1}{\sin\theta}+\frac{1}{\sin\alpha}\ne
\frac{1}{\sin^2\theta}+\frac{1}{\sin^2\alpha}
\\
\frac{1}{\sin\theta}\frac{\partial}{\partial\theta}\ne
\frac{1}{\sin^2\theta}\frac{\partial^2}{\partial\theta^2}
\\
\sqrt{\sin\alpha}+\sqrt{\cos\alpha}
\\
\frac{1}{\sqrt{\sin\alpha}}+\frac{1}{\sqrt{\cos\alpha}}
\\
\overline{\sin\alpha}
\end{gather*}
\end{document}


Nothing's perfect in this world! You can see the third line has the square root sign at different heights. A possible solution for this would be redefining \cos to have the same height as \sin:

\DeclareRobustCommand{\cos}{%
\mathop{\vphantom{\operator@font\sinname}}\!%
\qopname\relax o{cos}%
}


Since some typographic traditions use a different tag for \sin (it might be “sen”), I only provide \sinname as it shouldn't be necessary for \cos to have a changing name.

With this definition, the overline would be at the same level for \sin and \cos, which might or not be desired.

-
I was about to say the same thing: Appendix G, Rule 18a. +1 :-) – Gustavo Mezzetti Feb 24 at 11:35

Obviously (;-) @egreg’s answer is great and goes right to the point, but it does have an infinitesimal drawback: a pervert who tried $\overline{\sin x}$ would get a surprise. Moreover, one could argue that the placement of the radical sign in $\sqrt{\sin x}$ is suboptimal.

The positioning of superscripts in math formulas is a very low-level feature of TeX, which is described, along with many other “intimate” details of math typesetting, in Appendix G of The TeXbook, to which you are referred for an explanation of the following solution.

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath}

% egreg's recipe:
\makeatletter
\DeclareRobustCommand{\sinname}{%
\smash{\operator@font sin}\vphantom{s}%
}
% ... but let us use a different name for the operator:
\newcommand{\ssin}{\qopname\relax o{\sinname}}
\makeatother

% Our recipe:
\setbox0 = \hbox{} % load math fonts
\fontdimen18\scriptfont2 = 3.78970pt % turns out to be just enough

\begin{document}

$\sqrt{\sin x}+\sqrt{\ssin x} + \overline{\sin x}+\overline{\ssin x}$
That's not fine.

But, with our correction, the exponent of the usual'' \verb|\sin| will not be
rised that much:
$\frac{1}{\cos^{2}x} + \frac{1}{\sin^{2}x} + \frac{1}{x^{2}}$
For an explanation, see \textsl{The \TeX book}, Appendix~G, Rule~18a; the
relevant passage is set $$u\gets h-q$$\,\ldots\ where $q$\,\ldots\ [is] the
[value] of~$\sigma_{18}$\,\ldots\ in the font corresponding to
[style]~$C{\uparrow}$''.  What we are doing is to increase~$q$.  The amended
value of~$u$ will be subsequently used in Rule~18c to position the superscript.

\end{document}


Here’s the output:

Let us also magnify the crucial portion:

Of course, this solution could have drawbacks too: changing in this way a parameter that operate at so low a level of TeX might cause side-effects which are not evident at first sight.

-
Thank you, a beautiful answer. I'll consider this the next time I have to struggle with my superscripts :) – manthano Feb 24 at 14:53
A comment to myself: of course, “hard-wiring” the corrected value of \fontdimen18 \scriptfont2, as I do here, isn’t very nice: should be computed. I will translate in TeX the computation I did in a spreadsheet after taking a nap. (:-) (Of course, if nobody else has already done it in the meanwhile… ;-) – Gustavo Mezzetti Feb 24 at 14:56
@GustavoMezzetti Thanks for noting the issue (I had said that something wrong could happen). I fixed it without monkeying with \fontdimen, which I consider the very last resort. – egreg Feb 24 at 16:10
@egreg: I you could have added “Elementary, Watson!”, Watson being me, of course! :-) Indeed, there is no need to lower superscripts on tall boxes in a generalized way. But my actual intent, here, was to draw people’s attention to Appendix G—kind of “advertizing” it, since, perhaps, ii is not always read with all the attention it deserves. – Gustavo Mezzetti Feb 24 at 21:15

You can adjust the amount of space left for denominators using font dimen 11,

\documentclass{article}
\sbox0{$1$}

\fontdimen11\textfont2=8pt

\begin{document}

$\frac{1}{\sin\vartheta} \frac{1}{\sin^{2}\vartheta}$

\end{document}

-

Thanks to LaRiFaRi's original post I found a way to solve the problem. The superscript treats the \sin token different to a letter, so using \sin\phantom{}^2 gives the wanted result. This works without subdepth.

-
Well, no: you get a thin space between “sin” and the exponent. – egreg Feb 24 at 11:23

I do not know this \sin^2 syntax and therefore I have never seen this problem before. You might want to change to something more common as in my MWE below. The other issue is fixed in the same time as you do not have to compensate the exaggerated superscript from that sinus term.

% arara: pdflatex

\documentclass{article}
\usepackage{mathtools}

\begin{document}
$\frac{1}{\sin\vartheta}\frac{\partial}{\partial\vartheta} \text{ vs. }\frac{1}{\sin(\vartheta)^2}\frac{\partial}{\partial\vartheta^2}$
\end{document}


If you want to stick to this syntax, you will have to add a strut to your very left term. This will imitate the superscript from the right side and get everything nicely aligned (you will need the subdepth fix, still).

\frac{1}{\sin^{\vphantom{2}}\vartheta}


@egreg just told me that one could redefine \sin to

\def\sin{\mathop{\smash{\mathrm{sin}}\vphantom{s}}\nolimits}


in order to get this right. However it is not recommended to do so. You should know very well, what you are doing, if you redefine some core commands.

-
In the physics community this notation is quite common. I'm designing a poster accordingly to a published paper, where we used this syntax, thus I'm not flexible to change it. – manthano Feb 24 at 10:52
I think with the brackets it would mean something different, sin(theta squared) whereas sin^2 theta is (sin theta) squared. – David Carlisle Feb 24 at 11:00
@DavidCarlisle From my personal taste, I disagree. You case would be \sin(\vartheta^2). The sine and its argument form one expression, one term. Maybe I have seen this wrong all the time.. – LaRiFaRi Feb 24 at 11:03
\sin(\vartheta)^2 is a bit ambiguous, although all programming languages, including Mathematica (with square brackets instead of round ones), would interpret it as LaRiFaRi. If the brackets are preceded by \left/\right and if the mleftright package is not loaded, the interpretation of David might be favored because of the thin space inserted after \sin. Now, this space is quite difficult to notice! Another solution would be to write (\sin\vartheta)^2. – Michel Fioc Mar 2 at 13:38