Control horizontal alignment of sub/superscripts

Is it possible to modify the horizontal alignment of the sub and superscript of a symbol? That is to say, is it possible to get this pair of right-aligned sub/superscripts

instead of this left-aligned pair of sub/superscripts

when I have code like this:

\documentclass{standalone}

\usepackage{amsmath}

\begin{document}
$x_{101}^{1}$
\end{document}


Note: I am not looking for a manual solution (e.g. using hphantom which I used to create the above example) but a real automated one-shot-hits-all solution.

Edit: I'm fine with a solution that does not patch the existing sub/superscript mechanism (although I am curious as to whether that is possible). The solution could also employ a custom comman, i.e. \foo{x}{101}{1} as long as it adheres to the proper mathmode sub/superscript spacing etc.

• tricky with that syntax (or at least tricky not to break everything else) with a syntax \foo{x}{101}{1} it would be easy to measure the two scripts and pad the shorter one automatically. Feb 5 '16 at 13:49
• @DavidCarlisle Alright, maybe I was a bit too strict in my problem description. I'm fine with using a custom function rather than the normal sub/superscript syntax as long as the solution is automated otherwise and uses the correct mathmode sub/superscript spacing. Feb 5 '16 at 14:03

I'm not sure what's the purpose of this, but here it is:

\documentclass{article}
\usepackage{mathtools}

\DeclareRobustCommand{\subsup}[3]{{%
\mathpalette\makesubsup{{#1}{#2}{#3}}%
}}

\makeatletter
\providecommand{\@firstofthree}[3]{#1}
\providecommand{\@secondofthree}[3]{#2}
\providecommand{\@thirdofthree}[3]{#3}
\newcommand{\makesubsup}[2]{%
\sbox\z@{$\m@th#1{}_{\@secondofthree#2}$}%
\sbox\tw@{$\m@th#1{}^{\@thirdofthree#2}$}%
\dimen@=\wd\z@
\ifdim\wd\tw@>\wd\z@ \dimen@=\wd\tw@\fi
{\mkern0mu \@firstofthree#2}%
_{\mathmakebox[\dimen@][r]{\@secondofthree#2}}%
^{\mathmakebox[\dimen@][r]{\@thirdofthree#2}}%
}
\makeatother

\begin{document}

$\subsup{x}{101}{1}-\subsup{y}{1}{101}$

$A_{\subsup{x}{101}{1}}$

\end{document}


Just measure the subscript and superscript and make boxes as wide as the larger one. Note that this works also for second level sub/superscripts.

• I think I understand how this works. But, I am curious about why you've added \mkern0mu. Is there a particular reason for this? Feb 5 '16 at 14:50
• @A.Ellett See the comments to wipet's answer Feb 5 '16 at 14:50
• I see the misalignment, but how does placing the \mkern0mu correct this? Feb 5 '16 at 14:52
• @wipet Uh! And what about a hypothetical \subsup{\max}{11}{2}? You surely know where the problem would be. In the example above, ${{}\max}_{1}^2$ would have an unwanted thin space. With \mkern0mu this doesn't happen, of course. Feb 5 '16 at 15:02
• @wipet Four braces yours; four tokens mine. This is getting silly. Feb 5 '16 at 16:38

Here's a start, with \Ss{}{}{}. EDITED to work with displaystyle stuff, too. EDITED to automatically work with subscripting styles, as well. FIXED obvious bug that macro did not behave properly if superscript length exceeded subscript length.

\documentclass{article}
\usepackage{amsmath,stackengine,scalerel}
\newcommand\Ss[3]{\setbox0=\hbox{$#2$}\setbox2=\hbox{$#3$}\ifdim\wd2<\wd0%
\ThisStyle{\ensurestackMath{\stackengine%
{0pt}{\SavedStyle#1_{#2}^{}}{\SavedStyle{\phantom{#1}}^{#3}}{O}{r}{F}{F}{L}}}%
\else
\ThisStyle{\ensurestackMath{\stackengine%
{0pt}{\SavedStyle{\phantom{#1}}_{#2}^{}}{\SavedStyle#1^{#3}}{O}{r}{F}{F}{L}}}%
\fi
}
\begin{document}
\parskip 4pt\centering
$x_{101}^{1}$
$\Ss{x}{101}{1}$

$X_{101}^{1}$
$\Ss{X}{101}{1}$

$(A)_{123}^{ijklm}$
$\Ss{(A)}{123}{ijklm}$
$\biggl(\frac{1}{2}\biggr)_{123}^{4}\quad \Ss{\biggl(\frac{1}{2}\biggr)}{123}{4}$
$A_{\Ss{x}{101}{1}}$
\end{document}


For example, you can try this:

\def\sf#1_#2^#3{%
\setbox1=\hbox{$\scriptstyle#2$}%
\setbox2=\hbox{$\scriptstyle#3$}%
\ifdim\wd2>\wd1 \dimen0=\wd2 \else \dimen0=\wd1 \fi
\setbox2=\hbox to\dimen0{\hss\box2}%
\setbox1=\hbox to\dimen0{\hss\box1}%
#1_{\box1}^{\box2}
}

$\sf x_{100}^{2}$

\bye

• Not really: try $\sf y_{1}^{200}$ Feb 5 '16 at 14:34
• @egreg It works. Maybe, you have a bad computer. Feb 5 '16 at 14:37
• My computer is good and you're wrong: check the picture where the misalignment is very evident. You surely know where the problem is. Feb 5 '16 at 14:40
• I meant that this is desired feature: to apply the slant property of nucleus to the aligning of subscripts. But if no, then user can use, of course {{}#1}_{\box1}^{\box2}. Feb 5 '16 at 14:46