# Superscript and subscript together in text mode

I want to use super and subscript at the same time in text mode. I cannot use math mode because I created macros which only work in text mode.

I found this question: Vertically aligning textsuperscript and textsubscript together

But the suggested answer does not work because the width of the resulting text is based only on the subscript, if the superscript is longer the text overlaps with the content afterwards.

I created the following versions for longer sub / superscripts but i need them combined:

\documentclass{scrartcl}
\usepackage[english]{babel}

\def\SPSBbottom#1#2{\rlap{\textsuperscript{#1}}\textsubscript{#2}}
\def\SPSBtop#1#2{\rlap{\textsubscript{#2}}\textsuperscript{#1}}

\begin{document}

prefix\SPSBbottom{long}{1}text \\[1cm]
prefix\SPSBbottom{1}{long}text \\[1cm] % Working combination
prefix\SPSBtop{long}{1}text \\[1cm] % Working combination
prefix\SPSBtop{1}{long}text \\[1cm]

\end{document}


If I could place two text at the same place and the width is taken from the longer one than using \textsuperscript{#1 'and' \phantom{#2}}\textsubscript{#2 'and' \phantom{#2}} could work but I do not know how to do this.

Alternatively one could use the math form $A^{top}_{bottom}$ where top and bottom are in a nested normal mode (\text{} does not work). But I found no way to do this as well.

• Welcome to TeX - LaTeX! What do you mean with »\text{} does not work«? $A^{\text{top}}_{\text{bottom}}$ works fine (if the amstext or amsmath package has been loaded) – cgnieder Sep 14 '15 at 17:35
• I was really sure it did not work because of nested math modes and stuff. However it seems that I changed my macros over time in such a way that it does work now :-). Thanks – Manuel Sep 15 '15 at 17:10

Duplicate the definition of \textsuperscript:

\documentclass{article}

\makeatletter
\DeclareRobustCommand{\textsupsub}[2]{{%
\m@th\ensuremath{%
^{\mbox{\fontsize\sf@size\z@#1}}%
_{\mbox{\fontsize\sf@size\z@#2}}%
}%
}}
\makeatother

\begin{document}

X\textsupsub{ab}{c}X\textsupsub{c}{ab}X

\end{document}


Add \strut to the second argument if you prefer.

The stackengine package provides this feature directly, in the form of \stackanchor. If the stacking type is Short, the gap between the sub and superscript is set to a specified length. This mimics, in form, the natural behavior of the sub and superscripts in math mode. In the case of short stacks, the gap is centered about the math axis.

On the other hand, if the application requires sub and superscripts to be set at fixed vertical locations, regardless of the natural height of sub and superscript, then one can set the stacking type to Long, which uses a specified baselineskip between the sub and superscript. In the case of long stacks, the baselines of the sub and superscripts are symmetric about the baseline of the text

\documentclass{article}
\usepackage{stackengine}
\setstackgap{S}{2pt}% SHORT STACK DEFAULT GAP
\setstackgap{L}{.7\baselineskip}% LONG STACK DEFAULT BASELINESKIP
\def\stackalignment{l}% LEFT STACKING ALIGNMENT
\begin{document}
%\def\stacktype{S}% SHORT STACK (DEFAULT) SETS INTER-LINE GAP
X\stackanchor{ab}{c}X\stackanchor{c}{ab}X

\def\stacktype{L}% LONG STACK SETS INTER-LINE BASELINESKIP
X\stackanchor{ab}{c}X\stackanchor{c}{ab}X
\end{document}


For completeness, if one did want the stacks to be set in math mode, one can either declare \stackMath (a global declaration), or use \ensurestackMath{} on the \stackanchor.