Centering sub/superscript of operator after scaling

I would like to make the sub-/supperscript smaller 10% and use package scalerel to do that. Everything is fine until I find out that the sub-/superscript in operators are also scaled but they are not center aligned.

Any idea for fixing this issue. Thanks.

\documentclass{article}
\usepackage{amsmath}
\usepackage{subdepth}
\usepackage{scalerel}

\mathcode_="8000
\catcode_=12
\mathcode^="8000
\catcode^=12
\begingroup
\catcode_=\active
\catcode^=\active
\gdef_#1{\ensuremath{\sb{\begingroup \hstretch{.9}{\vstretch{.9}{#1}} \endgroup}}}
\gdef^#1{\ensuremath{\sp{\begingroup \hstretch{.9}{\vstretch{.9}{#1}} \endgroup}}}
\endgroup

\begin{document}
$$a^2 \qquad \sum\limits_{2}^{3}$$
\end{document}

2 Answers

In my other answer, I suggested an alternate approach to the OP's dilemma, through the use of the \DeclareMathSizes macro. However, in a comment, the OP asked if I could also resolve the issue using his original approach.

When I first looked at the OP's result, I though the mis-centering of the scripts was related to my scalerel package. However, upon closer inspection, I found that if I replaced the OP's \hstretch{.9}{\vstretch{.9}{#1}} with a \scalebox{.9}{#1}, the problem persisted.

I found, through experimentation, that if I wrapped the super/sub-script redefinitions in an \hbox, proper centering was restored.

Using the facilities of the scalerel package to import the current math style into the \hbox, I recast the OP's redefinitions in a way that preserved horizontal centering. In this MWE, I also set the rescale to 0.7, rather than 0.9, merely to emphasize the resizing.

\documentclass{article}
\usepackage{amsmath}
\usepackage{subdepth}
\usepackage{scalerel}

\mathcode_="8000
\catcode_=12
\mathcode^="8000
\catcode^=12
\begingroup
\catcode_=\active
\catcode^=\active
\gdef_#1{\sb{\ThisStyle{\hbox{\scalebox{.7}{$\SavedStyle#1$}}}}}
\gdef^#1{\sp{\ThisStyle{\hbox{\scalebox{.7}{$\SavedStyle#1$}}}}}
\endgroup

\begin{document}
$$a^2 \qquad \sum\limits_{2}^{3}$$
\end{document}

As to why the \hbox is required, I suspected that the super/sub-scripts performed an \unhbox as part of their process, and therefore needed an \hbox on which to act.

The following MWE, in which I had \unhbox announce its arrival, confirmed my suspicions:

\documentclass{article}
\usepackage{amsmath}
\usepackage{subdepth}
\usepackage{scalerel}

\mathcode_="8000
\catcode_=12
\mathcode^="8000
\catcode^=12
\begingroup
\catcode_=\active
\catcode^=\active
\gdef_#1{\sb{\ThisStyle{\hbox{\scalebox{.7}{$\SavedStyle#1$}}}}}
\gdef^#1{\sp{\ThisStyle{\hbox{\scalebox{.7}{$\SavedStyle#1$}}}}}
\endgroup

\let\svunhbox\unhbox
\def\unhbox{*\svunhbox}
\begin{document}
$$a^2 \qquad \sum\limits_{2}^{3}$$
\end{document}

I would instead recommend the use of \DeclareMathSizes to address the issue:

\documentclass{article}
\usepackage{amsmath}
%\usepackage{subdepth}
%\usepackage{scalerel}

\DeclareMathSizes{10}{10}{6}{5}

\begin{document}
xxx$$a^2 \qquad \sum\limits_{2}^{3}$$zzz
\end{document}

For comparison, here is the reference scale, in which the scripts are slightly larger:

• thank you. I will try this. But for my old code, is there any way to utilize it? Nov 8, 2021 at 18:46
• @NuecTah It is an interesting issue. At first, I thought my scalerel package to be responsible. However, if I replace the h- and v-stretches with a simple \scalebox, the same shifted behavior occurs. I have not yet resolved why, but I will keep on looking. Nov 8, 2021 at 22:16