# How to arbitrarily combine subscript, superscript and “midscript”

I would like to typeset formulas including "midscripts" - like subscripts and superscripts, but in the middle. This question and answer gives some solutions: Subscript. Superscript. Middlescript? . However, I would like to be able to combine midscripts, superscripts and subscripts for the same symbol. Additionally, I would like them to be horizontally centered, and maybe even occasionally right-aligned (it turns out that I am using not only middlescripts, but left middlescripts).

A partial solution is to simply write something like

$\begin{smallmatrix}i // j // k\end{smallmatrix} X$


; this takes care of the horizontal centering. This looks nice enough when all three indexes are present. However, if one of them is missing - e.g.

$\begin{smallmatrix}i // j // \end{smallmatrix} X$


, the vertical alignment is broken: the j should be a midscript, but is now hanging so low that it could be mistaken for a subscript.

Is there a way to produce a similar result but in such a way that the middle line of the index stack always has a consistent, more or less centered vertical position, regardless of whether the top and bottom lines are there and of how tall or short they are?

I do not insist on using smallmatrix: maybe the right answer involves \overset, \substack, an ordinary matrix or array combined with \scriptstyle, or something entirely different. Any solution is acceptable, as long as it allows horizontal centering (or right-alignment) and does not introduce excessive horizontal spacing between the main symbol and its indexes.

For usage in math mode:

\documentclass{article}
\usepackage{amsmath,mathtools}
\usepackage{xparse}

\makeatletter
\newenvironment{smallermatrix}[1][c]
{\null\,\vcenter\bgroup
\Let@\restore@math@cr\default@tag
\baselineskip0pt \lineskip0.4pt \lineskiplimit0pt
\ialign\bgroup\if#1l\else\hfil\fi$\m@th\scriptstyle##$\if#1r\else\hfil\fi&&\thickspace\hfil
$\m@th\scriptstyle##$\hfil\crcr
}{%
\crcr\egroup\egroup\,%
}
\makeatother

\NewDocumentCommand{\ts}{O{c} e{^?_}}{% ^ for superscript, ? for midscript, _ for subscript
\begin{smallermatrix}[#1]
\mathstrut\IfValueT{#2}{#2} \\
\mathstrut\IfValueT{#3}{#3} \\
\mathstrut\IfValueT{#4}{#4}
\end{smallermatrix}%
}

\begin{document}

$X\ts^{a}?{b}_{c}\quad X\ts?{x}_{y} \quad X\ts^{a}_{b} \quad \ts[r]^{aaa}?{b}_{cc}X \quad X\ts[l]^{[0,1]}?{b}$

\end{document}


• Thank you. I already tried something very similar, but this only works for indexes that have roughly the same height as a single "x". With $X\ts^{[0,1]}?{b}$ (which is a real occurence in the text I am writing), the b is again so low that it could be mistaken for a subscript. – Ilia Smilga Oct 17 '19 at 13:50
• @IliaSmilga Edited. – egreg Oct 17 '19 at 14:42
• Wow, that was quick! I am quite impressed with the result, but I still have some nitpicking. Now, when the indexes are not extra tall, the vertical spacing looks too wide - it is certainly wider than with the previous version. In particular the subscript, which is already lower than usual in the previous version (but this is unavoidable, to make room for the midscript), is now even lower. Unfortunately I do not understand your code, so I cannot fix this on my own... – Ilia Smilga Oct 18 '19 at 8:25
• @IliaSmilga You could change \mathstrut\IfValueT{#3}{#3} into \IfValueTF{#3}{#3}{\vphantom{x}}, so in the case there is no midscript the distance between superscript and subscript would be reduced. – egreg Oct 18 '19 at 8:49
• There is one more problem I noticed: the \mathstruts cause LaTeX to think that the formulae are taller (or deeper) than they really are, causing excessive line spacing... MWE: put a single $X\ts^{a}?{b}$, or a single $X\ts?{b}_c$, inside a long text paragraph. – Ilia Smilga Oct 18 '19 at 15:08