All of LaTeX's math spacing comes out of three numbers. They sit in the last three lines of fontmath.ltx: \thinmuskip=3mu, \medmuskip=4mu plus 2mu minus 4mu, \thickmuskip=5mu plus 5mu. Thin, medium and thick — 3, 4 and 5 — settle the gaps around the + in a+b and the = in a=b alike. And the medium space can shrink by a full 4 mu, meaning it can vanish altogether, which few references bother to mention. This page lays out the rules for the superscript ^ and the subscript _, and the measured widths of \,, \:, \;, \!, \quad and \qquad. Whether you arrived searching for how to adjust spacing in math or chasing a ! Double superscript error, the answer is here.
^ and _ attach to exactly one token
In math mode ^ makes a superscript and _ a subscript, but each attaches to exactly one token that follows — one character, or one command. To raise or lower more than that, group it with braces { }. The awkward part is that forgetting them is not an error. x^{10} makes the whole “10” an exponent, whereas x^10 without braces raises only the “1” and leaves the “0” at full size on the baseline. It compiles silently, so this is the kind of mistake you find in print. Train your hands: braces around any script longer than one character.
$x^{10}$ % the whole 10 is the exponent
$x^10$ % only the 1 is raised; the 0 stays full size
$a_{ij}$ % ij together as one subscript
$2^{n+1}$ % n+1 as one exponent
$x_i^2$ % both scripts; the order does not matterA single symbol can carry a superscript and a subscript at once. Write x_i^2 or x^2_i — LaTeX stacks them the same way either order. Scripts nest, too, but each level needs its own braces: in x^{y^z}, z is the exponent of y and the whole y^z is the exponent of x. Drop the braces and x^y^z raises the error of the next section. Note that on big operators such as \sum and \lim, ^ and _ may move directly above and below the symbol, and \limits / \nolimits force the position; that behaviour belongs to the operator, so the page on sums, integrals and big operators covers it.
! Double superscript. and ! Double subscript.
This is the error you get for putting a superscript on the same base twice. Compile $x^y^z$ and pdfLaTeX from TeX Live 2024 halts with ! Double superscript.; for subscripts, $x_i_j$ gives ! Double subscript. Both mean “this base already has a script”. The fix is to write the nesting you meant, with braces: x^{y^z} makes z the exponent of y, while {x^y}^z raises all of x^y to the power z. Those two are mathematically different, so putting a brace wherever it happens to silence the error will change what the formula says.
The derivative prime, f'(x) — and why f^' is wrong
Just type the apostrophe '. It is already a superscript, so never add a ^ of your own. In latex.ltx the ' is made an active character in math and expands to ^\bgroup\prim@s; measured, f'(x) and f^{\prime}(x) both come to 22.27089 pt, identical. Two primes are f''(x), three f'''(x). What is curious is that the machinery swallows a following ^: f'^2 raises no error and sets to 12.76395 pt, exactly like f^{\prime2}. So if you meant “prime, then squared”, check that the result says what you intended.
Scripts on the left: {}^{A}_{Z}X and \sideset
To put a superscript or subscript to the left of a symbol — isotope notation, say — use an empty pair of braces {} as the base. Writing {}^{14}_{6}\mathrm{C} places {} as a zero-width ordinary symbol, hangs the scripts on it, and then sets the C. Leave the {} out and the scripts attach themselves to whatever precedes: written as = ^{14}_{6}C, they dangle off the =, and if what precedes already carries a script you get ! Double superscript. To place scripts on both sides of a big operator, amsmath offers \sideset{_a^b}{_c^d}\sum — but that is a tool for the operator, so see the page on sums and integrals. mathtools' \prescript is another answer to the same need.
\[
{}^{14}_{6}\mathrm{C}, \qquad {}^{t}\!A, \qquad
x^{y^z} \ne {x^y}^z, \qquad f''(x)
\]The mu: one eighteenth of an em, and it shrinks inside scripts
One mu is 1/18 em, so 18 mu is exactly one em. The em here is tied to the size of the math symbol font, which means a space written in mu follows the size of the type. In a 10 pt article, the width difference between a\,b and ab measures 1.66662 pt — matching 3 mu = 1/6 em = 1.6667 pt. Put the same \, inside a superscript, as in x^{a\,b}, and it measures 1.36574 pt; one level deeper it drops to 1.22685 pt. Move to a 12 pt document and it becomes 1.99997 pt. The space really does scale with the letters around it.
But \quad and \qquad are not part of that family. latex.ltx defines them as \def\quad{\hskip1em\relax} and two ems for \qquad — the unit is not mu but an em of the text font. So they do not shrink inside a script: measured, x^{a\quad b} is 10.00002 pt wider than a plain x^{ab}, a full text-size em. And in a 12 pt document \quad measures 11.74988 pt while 18 mu is exactly 12 pt, so \quad and 18 mu are not the same length. The safe division of labour is by role: the mu commands (\,, \:, \;, \!) for fine adjustment, the em commands (\quad, \qquad) for large partitions.
| Command | Defined amount | Measured in a 10pt article |
|---|---|---|
\, | \thinmuskip = 3 mu (alias \thinspace) | 1.66662 pt; the nudge you reach for most |
\: | \medmuskip = 4 mu plus 2 minus 4 (\medspace, \>) | 2.22216 pt; elastic, so it can vanish |
\; | \thickmuskip = 5 mu plus 5 (\thickspace) | 2.77771 pt; can stretch to double |
\! | −3 mu (identical to \negthinspace) | −1.66663 pt; the exact inverse of \, |
\negmedspace | −4 mu; no amsmath needed (it is in latex.ltx) | −2.22217 pt |
\negthickspace | −5 mu; likewise in the LaTeX kernel | −2.77771 pt |
\quad | \hskip1em; an em of the text font, not mu | 10.00002 pt; does not shrink inside scripts |
\qquad | \hskip2em; twice a \quad | 20.00003 pt |
(backslash-space) | the control space; the same gap as between words | 3.33332 pt |
\enspace | a fixed half-em | 5 pt |
The reason \, works in ordinary text as well as in math is that \tmspace in latex.ltx branches on \ifmmode: in math it emits \mskip\thinmuskip (3 mu), in text a fixed \kern .16667em. One command, two systems of units. While you are in that file, \let\>=\: records that \> is an alias for \:. And \negmedspace and \negthickspace from the table are often described as coming from amsmath, but both are in the LaTeX kernel of TeX Live 2024 and have been confirmed to work without \usepackage{amsmath}.
Why scripts shrink: \displaystyle and the four math styles
TeX sets every formula in one of four styles, and the style decides both the character size and what a mu is actually worth. From largest to smallest: \displaystyle (displayed equations), \textstyle (inline), \scriptstyle (a script), \scriptscriptstyle (a script on a script). Measuring the height of X in 10pt text gives 6.83331pt for display and text — they are identical — then 4.78334pt for script and 3.41667pt for scriptscript. The shrinking only happens in the two steps into scripts, and it stops there: there is no fifth, smaller style. That is the machinery behind the mu shrinking described above. What matters day to day is forcing a style: inline, $\sum_{k=1}^{n}$ is squashed to 8.04175pt tall, while $\displaystyle\sum_{k=1}^{n}$ measures 16.51393pt — the displayed shape, with the summation limits back above and below. The cost is uneven line spacing, so if you find yourself reaching for it repeatedly in running text, the formula probably wants to be displayed instead.
The three you will actually use: \, before dx, \int\!\!\int, and after a function name
Start with the integral. \int f(x)dx measures 38.71877 pt and \int f(x)\,dx 40.38539 pt — a difference of exactly 3 mu, or 1.66662 pt, and that thin space is what separates the integrand from the dx. Without it, f(x)dx reads as one unbroken run of symbols. Next, multiple integrals. \int\!\!\int is the classical idiom of pulling two integral signs together with two negative spaces, and measured it comes out at exactly the same width as amsmath's \iint (both 19.3056 pt; a plain \int\int is 22.63885 pt). In other words, \iint is that hand-made adjustment turned into an official command.
\[
\int f(x)\,dx, \qquad \int\!\!\int f \;(=\ \iint f),
\qquad \sin x, \qquad \operatorname{sinc} x
\]The third case is the gap after a function name, and here you should not add anything yourself. \sin is declared as an operator (Op), so a thin space appears automatically between it and what follows. Measured, \sin x is 19.6597 pt while \mathrm{sin}x is 17.99307 pt — a difference of 1.66663 pt, exactly 3 mu. So writing your own function name with \mathrm{} is precisely what loses that space. The correct tool is amsmath's \operatorname{sinc}; measured, \operatorname{sin}x comes to 19.6597 pt, the same as \sin x. If the name recurs, define it once with \DeclareMathOperator.
Invisible boxes that level heights and widths: \phantom, \vphantom, \mathstrut
“Reserve the room, show nothing in it” — that recurring typesetting need is what the \phantom family is for. \phantom{…} makes an empty box with the same height, depth and width as its argument set normally. Nothing prints, but exactly that much room is held open. There are versions restricted to one dimension: \hphantom{…} keeps only the width (zero height and depth), and \vphantom{…} only the height and depth (zero width). \vphantom is the one for “claim the vertical extent but take up no horizontal room”.
\[
\sum_{j \in \{0,\ldots,10\} \vphantom{3^{3^{3^j}}}}
\sum_{i \in \{0,\ldots,3^{3^{3^j}}\}} i \cdot j
\]
% level the radicals: each argument gets a parenthesis of vertical room
$\sqrt{\mathstrut a}\;\sqrt{\mathstrut a^2}\;\sqrt{\mathstrut b}$The example above is the case where two summation signs refuse to line up. The right-hand sum carries a tall tower 3^{3^{3^j}} in its subscript, so TeX drops the body of that sum to make room and the two \sum signs no longer sit level. Putting \vphantom{3^{3^{3^j}}} in the left-hand one reserves the same height while staying invisible, and the two align. Its close cousin \mathstrut is a purpose-built strut that takes no argument: internally it is \vphantom(, so it has the height and depth of an opening parenthesis and zero width. Placed at the start of each argument when \sqrt{a} and \sqrt{a^2} sit side by side, it levels the roofs of the radicals and the row settles down. In short: \hphantom to match widths, \vphantom or \mathstrut to match heights, \phantom for both — all of them one idea, manipulating dimensions alone through an invisible box.