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.
If you look at the status bar (bottom of the LyX window) when you click the appropriate menu item, you'll see the function appear there for a second or two. The two functions you're after are
script-insert superscript
script-insert subscript
Best Answer
Probably the simplest way to do this is to use the option
mathescape
in a\lstset
command. This will not only allow you to use superscripts and subscripts, but will also give you access to other things you can do in mathmode. It may also do subtle and strange things to your formatting, but you can perhaps account for this where it matters to you.Example.
Result.