数学模式下上标相对于下标居中

数学模式下上标相对于下标居中

我认为最好的解释是我的尝试:

\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}

答案1

将它们设置在大小相等的框中。使用 可以轻松支持此功能\eqmakebox[<tag>][<align>]{<stuff>};这将设置<stuff>一个跨所有 s 的最大宽度框<tag>(带有条件<align>left、centre(默认)或r右对齐):

在此处输入图片描述

\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}

编译至少两次,并改变任何<tag>ged中的最大宽度\eqmakebox

答案2

您可以吸收下标和上标,测量它们,然后应用它们。

\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}

在此处输入图片描述

如果您喜欢不同的语法:

\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}

请注意,这\text{M}是不正确的:如果您想确保“M”是直立的,请使用\mathrm{M}。此外,在数学显示环境之前留一个空行也是错误的。

答案3

这是一个非常简短和简单的解决方案,无需额外的包或宏:

\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}

相关内容