# By default, how much do ^ and _ raise and lower the text box in superscript and subscript respectively?

I'm trying to define a set of commands for superscript and subscript, but cannot use the characters ^ and _ in doing so. I've been able to define the correct font size by using \check@mathfonts\fontsize\sf@size\z@\math@fontsfalse\selectfont, but can not work out the default values for how much the text box is raised and lowered.

How does TeX/LaTeX define these values?

-
it depends on several of the math font dimen parameters and is not a simple formula the amount a superscript is raised depends on the size of the superscript, whether there is subscript, the size of the subscript, and the size of the base. see appendix G of the texbook. If the character ^ is not available \sp should give access to the same functionality –  David Carlisle Dec 28 '12 at 1:39
@DavidCarlisle Make that an answer, please. –  lockstep Dec 29 '12 at 21:09
What are you trying to do? I'd guess there are much simpler ways to do whatever you are trying to accomplish... –  vonbrand Jan 11 '13 at 18:24

The algorithm for placing sub- and superscripts is "hard-wired" in the TeX engine, and it's not as simple as you might think. First of all, there's a special treatment for sub- and superscripts of "simple characters" like a, \alpha or \mathcal{A}. In this case you also have some horizontal displacement of the subscript that is determined by the italic correction of the character: for example, the subscript should be shifted to the left for an F, but not for an A.

The default values for raising and lowering are as follows in the case of "simple characters". The superscript is (tentatively!) raised by the maximum of (depth of superscript) + (1/4 of the x-height) and σk, where the x-height is the \fontdimen parameter number 5, and σk is \fontdimen parameter number k (of \textfont2 unless the whole formula is in a sub- or superscript itself). Now it depends on the current style (which, unfortunately, the TeX user can only access via \mathpalette or \mathchoice): In \displaystyle k=13 is used, in "cramped style" k=15, and k=14 otherwise.

How much the subscript is lowered depends on the presence of a superscript. If there's a superscript, then the subscript is (tentatively!) lowered by σ17, otherwise by the maximum of (height of subscript) - (80% of the x-height) and σ16. In the case of a sub- and superscript combination there's an additional adjustment (with some unpleasant side effects), see e.g. this answer of mine.

If the sub- or superscript is not attached to a "simple character" but to some other nucleus, then additional minimum shift values are used: the superscript is raised by at least (height of nucleus) - σ18, the subscript is lowered by at least (depth of nucleus) + σ19, where σ18 and σ19 are to be taken from \scriptfont2 unless the whole formula is in a sub- or superscript itself (in which case \scriptscriptfont2 should be used).

I'm not sure if you really want to implement this ... if you do, have a look at Rule 18 of Appendix G in the TeXbook for details.

-

There is a TUGboat paper titled Understanding the aesthetics of math typesetting with all the details of the typesetting algorithm as well as visual representation of all the parameters. A second paper Appendix G illuminated has also a lot of useful information.

-

TeX doesn't care which specific characters are used to enter a super- or a subscript; it only looks at the catcodes. So if you can't use ^ and _ for some reason, assign another character their catcodes...

\begingroup
\catcode\.=7 % superscript
\gdef\superscript#1#2{{#1}.{#2}}
\catcode\.=8 % subscript
\gdef\subscript#1#2{{#1}.{#2}}
\endgroup

$\superscript{x}{2}$ $\subscript{x}{2}$
\bye


It is perhaps unfortunate that there are no primitive control sequences that duplicate the effects of characters with catcodes 3, 4, 7, and 8 (&, \$, ^, and _ in plain TeX), but that's the way it was done back in 1982 and now we're stuck with it.

-