2

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.

1 Answers1

3

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}

enter image description here

egreg
  • 1,121,712
  • 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 08: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 08: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
  • @IliaSmilga I see no way to avoid it: that thing is big anyway. – egreg Oct 18 '19 at 15:10
  • Isn't there a way to typeset the matrix without any whitespace (except possibly in the middle), and then tell LaTeX that the center of the middle row of the matrix should be vertically aligned with the center of the line of text? – Ilia Smilga Oct 18 '19 at 16:54
  • @IliaSmilga You'll get different heights for different letters in the matrix. – egreg Oct 18 '19 at 17:21
  • @IliaSmilga Only ensuring that the superscript and subscript have the same height you can hope that the midscript is centered with respect to the math axis. – egreg Oct 18 '19 at 17:30
  • OK, I figured out how to do solve the extra line spacing problem. Actually, it turns out to be incredibly simple: it turns out that with amsmath, the command smash takes 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
  • One last question: I would like to acknowledge your help on this typesetting problem in the paper I am writing. Would you rather go by your full name, or by your StackExchange handle (egreg)? – Ilia Smilga Oct 21 '19 at 12:30
  • @IliaSmilga Don't worry about it. – egreg Oct 21 '19 at 13:27