[Tex/LaTex] Strange default exponent positioning

horizontal alignmentmath-modesuperscriptstypography

Why is this red exponent so far away from the right parenthesis? To my eye it is too far away.

\documentclass{article}
\usepackage{xcolor}
\begin{document}
\[ax^2+bx+c=a\left(x+\frac{b}{2a}\right)^{\color{red}2}-\frac{b^2}{4a}+c\]
\end{document}

enter image description here

Best Answer

When TeX assembles the various "atoms" of a formula into a whole (a "molecule", if you will), it mainly keeps track of each atom's enclosing rectangular "box", and it arranges the boxes horizontally and vertically according to the rules listed in Appendix G of the TeXbook. In the process of arranging the boxes, TeX doesn't actually "know" what's inside each box. E.g., a box containing a closing "fence" symbol could contain a right round parenthesis, a right square bracket, a right angle bracket, a right curly brace, etc. All TeX knows at this stage of the typesetting process is (i) the outer limits of each atom's rectangular box and (ii) certain properties of each box, e.g., if it's a box of type ord, rel, bin, open, close, punct, etc.

It can thus happen that a positioning rule is not quite optimal ex post for a given fence symbol. In particular, while the fence-exponent positioning rule may be perfectly adequate for vertical bars and square brackets -- which have material in the upper-right corner of the box -- it may not be the case for right curly braces, right round parentheses and, even more so, right angle brackets -- for which the material at the top of the box is mostly in the left-hand corner. If need be, this deficiency can (and should) be corrected by inserting a negative thin space, \!, at the start of the material contained in the exponent.

The examples below illustrate this issue. The exponents in the first two lines, which feature a plain vertical bar and a right square bracket, look to be positioned just about right relative to the fence symbol. A slight improvement results from inserting \! after a right curly brace (line 3). The \! adjustment is arguably more necessary for right round parentheses (line 4) and right angle brackets (line 5). Finally, I'd say that inserting two \! directives in the right angle bracket case (line 5, far right) is probably too much of a good thing -- and hence no longer good...

A separate positioning issue arises from the fact that \left and \right also insert some white space before and after the material enclosed by this group (but not between the right-hand closing fence symbol and any immediately following superscript/subscript material). To avoid having TeX insert this extra whitespace, you could use explicit sizing instructions for the fence symbols (e.g., \Bigg and \bigg), or you could load the mleftright package and (i) issue the command \mleftright if you want to suppress the extra whitespace globally or (ii) use the commands \mleft and \mright if you want to address the matter in a case by case way.

enter image description here

\documentclass{article}
\usepackage{xcolor,amsmath}
\newcommand\myfrac{\frac{b}{2a}}
\begin{document}
\begin{align*}
&\left\lvert \myfrac \right\rvert^{\color{red}2}\\
&\left[  \myfrac \right]^{\color{red}2}\\
&\left\{ \myfrac \right \}^{\color{red}2} \text{ vs.\ }
 \left\{ \myfrac \right\}^{\!\color{red}2}\\
&\left(  \myfrac \right)^{\color{red}2} \text{ vs.\ }
 \left(  \myfrac \right)^{\!\color{red}2}\\
&\left\langle \myfrac \right\rangle^{\color{red}2} \text{ vs.\ }
 \left\langle \myfrac \right\rangle^{\!\color{red}2} \text{ vs.\ }
 \left\langle \myfrac \right\rangle^{\!\!\color{red}2}
\end{align*}
\end{document}