[Tex/LaTex] the technical reason for the difference of $\sum{}^{…}$ and $\sum^{…}$

macros

Because space is a delimiter when parsing macro names, I got used to ending macros in running text with {} like in text \foobar{} text, because I find the escaped space in text \foobar\ text disturbing.
Sometimes this habit spills over into math mode. Obviously

\[
    \sum{}^{...}
\]

does not work as intended when really

\[
    \sum^{...}
\]

was meant. How are they parsed and what are the differences?

Edit: I meant the \foo\ solution, not a double space.

Best Answer

TeX creates math list which is (roughly speaking) a sequence of atoms, each atom has nucleus+subscript+superscript. Each part of the atom can be empty. The sequence of atoms are converted to the definite \hboxes (raised or lowered) in the second pas of scanning math mode material.

First example \sum{}^a creates first atom with nucleus: sum character, subscript and superscript are empty. It is followed by second atom with empty nucleus, empty subscript and superscript is a.

Second example \sum^a creates only one atom with sum character in nucleus and a in superscript. The subscript is empty.

The differences in visible output is given by differences in interpreting the two atoms versus one atom in second pass over math material. The positioning of superscript depends on the size of the nucleus and on the type of the atom.