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.

Unfortunately I do not understand your code, so I cannot fix this on my own...
– Ilia Smilga Oct 18 '19 at 08:25\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 08:49\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:08amsmath, the commandsmashtakes a[b]or[t]option that allows you to "smash" a box only from the top or from the bottom. (I was not aware of this fact until today!) – Ilia Smilga Oct 18 '19 at 18:21