# Strange default exponent positioning?

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}


-

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.

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

-

The following image shows the boxes of each character as seen by tex, for each of the cases in egreg's answer.

You can clearly see how the boxes of the parenthesis are "too wide", which causes the exponent to be too far. In addition, using egreg's suggestion (\biggl() removes unwanted space before the box of the left parenthesis.

Note For generating the above image I used this answer slightly modified to work in math mode.

-
I wouldn't say that the rectangular box that encloses the parenthesis is "too wide"; in fact, its dimensions are correct (relative to the rules used in Appendix G of the TeXbook). The issue is, rather, that in the case of a right round parenthesis there's no material in the upper right-hand corner of the box, and it's this lack of material in the area of the box that's closest to the exponent that makes the positioning look wrong. –  Mico Dec 11 '13 at 9:47
@Mico in other words, it's a slightly more specifig issue of missing kerning ;) –  yo' Dec 11 '13 at 12:36
@tohecz - Yeah, I suppose one could call it that: Because TeX (by design) doesn't know the shape of the fence symbol that's contained in the box, it can't apply any fine-tuning to the placement of the box that contains the exponent w.r.t. to the box that contains the fence symbol. One might call it an issue of (lack of) "two-dimensional kerning" of the various math boxes -- as opposed to the more frequently encountered term of "one-dimensional kerning" of glyphs lined up along one and the same baseline. :-) –  Mico Dec 11 '13 at 13:17

This is a case where manual adjustment is needed. The coloring has nothing to do with it. It has to do with the shape of the parenthesis, which is quite wide.

I'd recommend using \biggl and \biggr, in particular because of the coefficient in front of the open parenthesis.

\documentclass{article}
\usepackage{xcolor}
\begin{document}
$ax^2+bx+c=a\left(x+\frac{b}{2a}\right)^{2}-\frac{b^2}{4a}+c$
$ax^2+bx+c=a\left(x+\frac{b}{2a}\right)^{\!2}-\frac{b^2}{4a}+c$
$ax^2+bx+c=a\biggl(x+\frac{b}{2a}\biggr)^{\!2}-\frac{b^2}{4a}+c$
$ax^2+bx+c=a\left(x+\frac{b}{2a}\right)^{\textcolor{red}{2}}-\frac{b^2}{4a}+c$
$ax^2+bx+c=a\left(x+\frac{b}{2a}\right)^{\!\textcolor{red}{2}}-\frac{b^2}{4a}+c$
$ax^2+bx+c=a\biggl(x+\frac{b}{2a}\biggr)^{\!\textcolor{red}{2}}-\frac{b^2}{4a}+c$
\end{document}


-