# Center superscript relative to subscript in math mode

I think the best explanation is my attempt :

\documentclass{report}
\usepackage{amsmath}
\usepackage{graphicx}

\newcommand{\supsub}[2]{\hspace{-0.6em}
\begin{tabular}{c}
{\scriptsize #1} \-1.3ex] {\scriptsize #2} \end{tabular}\hspace{-0.6em} } \begin{document} This is an example of how the command would be used: \begin{equation*} \text{M} \supsub{a,\dots,a}{a,a,\dots,a,a} \text{M} \end{equation*} However, you can see here that the horizontal and vertical alignment is not quite right: \begin{equation*} \text{M} \supsub{a}{a} \text{M} \quad \text{M}^a_a \text{M} \end{equation*} The subscript and the superscript are both too low, and are probably not the correct distance from the M''s. I think there is a better way to do it than eyeballing it and manually changing the dimensions. Also, it messes up the spacing for inline equations: \text{M} \supsub{a}{a} \text{M}. This line has extra vertical space above and below it, as you can see by this nonsense line that I added just to show you the spacing around it. \end{document}  ## 3 Answers Set them in equivalently-sized boxes. This is easily supported using \eqmakebox[<tag>][<align>]{<stuff>}; this sets <stuff> in a box of maximum width across all <tag>s (with a conditional <align>ment: left, centre (default) or right-aligned): \documentclass{article} \usepackage{amsmath,eqparbox} \begin{document} This is an example of how the command would be used: \[ \text{M} ^{\eqmakebox[aaa]{\scriptstyle a,\dots,a}} _{\eqmakebox[aaa]{\scriptstyle a,a,\dots,a,a}} \text{M}

\end{document}


Compile at least twice with the change of maximum width in any <tag>ged \eqmakebox.

You can absorb the subscripts and superscripts, measure them and then apply them.

\documentclass{report}
\usepackage{amsmath,xparse}

\makeatletter
\newsavebox{\supsub@sup}
\newsavebox{\supsub@sub}
\newlength{\supsub@wd}

\NewDocumentCommand{\supsub}{me{^_}}{%
\sbox\supsub@sup{$\m@th\scriptstyle\IfValueT{#2}{#2}$}%
\sbox\supsub@sub{$\m@th\scriptstyle\IfValueT{#3}{#3}$}%
\setlength{\supsub@wd}{\wd\supsub@sup}%
\ifdim\supsub@wd<\wd\supsub@sub
\setlength{\supsub@wd}{\wd\supsub@sub}%
\fi
#1%
\IfValueT{#2}{^{\makebox[\supsub@wd]{\usebox{\supsub@sup}}}}%
\IfValueT{#3}{_{\makebox[\supsub@wd]{\usebox{\supsub@sub}}}}%
}
\makeatother

\begin{document}

This is an example of how the command would be used:
\begin{equation*}
\supsub{\mathrm{M}}^{a,\dots,a}_{a,a,\dots,a,a} \mathrm{M}
\end{equation*}

\end{document}


If you prefer a different syntax:

\documentclass{report}
\usepackage{amsmath}

\makeatletter
\newsavebox{\supsub@sup}
\newsavebox{\supsub@sub}
\newlength{\supsub@wd}

\newcommand{\supsub}[2]{%
\sbox\supsub@sup{$\m@th\scriptstyle#1$}%
\sbox\supsub@sub{$\m@th\scriptstyle#2$}%
\setlength{\supsub@wd}{\wd\supsub@sup}%
\ifdim\supsub@wd<\wd\supsub@sub
\setlength{\supsub@wd}{\wd\supsub@sub}%
\fi
^{\makebox[\supsub@wd]{\usebox{\supsub@sup}}}%
_{\makebox[\supsub@wd]{\usebox{\supsub@sub}}}%
}
\makeatother

\begin{document}

This is an example of how the command would be used:
\begin{equation*}
\mathrm{M}\supsub{a,\dots,a}{a,a,\dots,a,a} \mathrm{M}
\end{equation*}

\end{document}


Note that \text{M} is incorrect: if you want to be sure that "M” is upright, use \mathrm{M}. Also a blank line before a math display environment is wrong.

Here a very short and simple solution without additional packages or macros:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\setbox0\hbox{$\scriptstyle a,a,\dots,a,a$}
\setbox2\hbox to \wd0{\hss$\scriptstyle a,\dots,a$\hss}

The correct placement:
$\text{M}_{\box0}^{\box2}\text{M}$

\end{document}