# Define macro that scans ahead for super- and/or subscript, “absorbs” them and moves their argument

I have a lot of mathematical macros that already have a super- and/or subscript by definition, i.e. something like

\newcommand*{\mymathsym}{x^{\text{foo}}_{\text{bar}}}


In the text body, these symbols frequently need an extra sup-/subscript. This means, the author must remember to put the main symbol into an pair of {}-braces, otherwise a double sup-/subscript error occurs.

$${\mymathsym}^{\text{extra}}$$


The extra superscript becomes a secondary superscript and it set slightly higher and smaller: This has two drawbacks: a) In the special field of application, both superscripts stand on the same hierarchy level from a conceptional viewpoint. In other words, both superscripts should actually be printed as a list "foo, extra" and the opposed order "extra, foo" would be equally good. b) If the primary sup- and subscript are very unbalanced in length, the secondary superscript is set far apart, e.g.

\newcommand*{\mymathsymlong}{x^{\text{foo}}_{\text{very long foobar}}}


and

$${\mymathsymlong}^{\text{extra}}$$


yields

As a workaround, I currently use the following definition that takes an optional argument and appends the argument to the internal superscript:

\newcommand*{\mymathsymext}[1][]{x^{\text{foo}\if!#1!\else, #1\fi}_{\text{very long foobar}}}


(N.b. I know that the condition \if!#1! is not the correct way to test for an empty argument, because it fails if the argument expands to a !. But I think you get the idea what the macro does.)

It is used as

$$\mymathsymext \qquad\text{vs.}\qquad \mymathsymext[\text{extra}]$$


and yields However, this has two major drawbacks: a) \newcommand only supports a single optional argument. Hence I need to decide at design time, if either an additional superscript or an additional subscript might eventually be needed. I cannot support both. b) The user has to remember an unusual syntax for placing additional sup-/subscripts.

Question:

How does one define a macro \mymathsymsuper that

• scans ahead, if it is followed by a supscript character ^<tok> and/or subscript character _<tok> each followed by an additional token <tok>
• "absorbs them", and
• moves <tok> to end of its internal sup-/subscript separated by a comma?

Full MWE:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\newcommand*{\mymathsym}{x^{\text{foo}}_{\text{bar}}}

\newcommand*{\mymathsymlong}{x^{\text{foo}}_{\text{very long foobar}}}

\newcommand*{\mymathsymext}[1][]{x^{\text{foo}\if!#1!\else, #1\fi}_{\text{very long foobar}}}

\begin{document}

Here, the author must know that \verb#\mymathsym# has already a super- and subscript and must remember to put the main symbol into a pair of \{\}-braces, otherwise a double sup-/subscript error occurs.
The extra superscript becomes a secondary superscript and it set slightly higher and smaller:
$${\mymathsym}^{\text{extra}}$$

If the primary sup- and subscript are very unbalanced in their length, the secondary subscript is set very far apart:
$${\mymathsymlong}^{\text{extra}}$$

This extended macro takes an optional argument and absorbs'' the extra superscript into the primary superscript:
$$\mymathsymext \qquad\text{vs.}\qquad \mymathsymext[\text{extra}]$$
Still, the author must remember this unusual'' syntax and it only supports either an extra super- or subscript, bot not both.

\paragraph{Question:}
How does one define a macro \verb#\mymathsymsuper# that
\begin{itemize}
\item scans ahead if it followed by a superscript character $\verb!^!\langle \mathit{token}_\text{sup}\rangle$ and/or subscript character $\verb!_!\langle \mathit{token}_\text{sub}\rangle$ each followed by an    additional token $\mathit{token}_\text{sup}$ and $\mathit{token}_\text{sub}$ resp.
\item absorbs them'', and
\item moves $\mathit{token}_\text{sup}$ and/or $\mathit{token}_\text{sub}$ to end of its internal sup-/subscript separated by a comma?
\end{itemize}

\end{document}

• First of \text is the wrong command to use in this context, it does not do what you think it does. If your languages is English, use \mathrm instead, else use \textup. See the xparse package for something capable of picking up sub- and super script. – daleif Aug 7 at 13:30
• @daleif I am very sure, that \text is the right command. It temporarily leaves math mode and allows to enter text in normal mode. My super- and subscripts are "normal" text, i.e. telling labels, like "next", "first", "prev", "last", "origin" and so on. They are not math variables but static entities that must be treated as a single object, i.e. it is always "next" not "n ext" nor "ne xt" nor similiar. – nagmat84 Aug 7 at 13:37
• Nope, try this: \textit{test $\text{test}$ test} all three is now in italic. Textual indicies like your extra should be upright, always. Good macros should not depend on which context you use this. Wrong usage of the \text command is by far one of the most common error I see in manuscripts. – daleif Aug 7 at 13:42
• The \text command switches to the surrounding text context, not upright. So in say a theorem, the context will be italic and \text gives the wrong output. Leave \text for textual comments in displayed math and use the proper macros for the rest. – daleif Aug 7 at 13:45

It's a breeze with xparse:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentCommand{\mymathsym}{e{^_}}{%
x^{\mathrm{foo}\IfValueT{#1}{,#1}}_{\mathrm{bar}\IfValueT{#2}{,#2}}%
}

\begin{document}

\begin{gather}
\mymathsym \\
\mymathsym^{\mathrm{extrasup}} \\
\mymathsym_{\mathrm{extrasub}} \\
\mymathsym^{\mathrm{extrasup}}_{\mathrm{extrasub}} \\
\mymathsym_{\mathrm{extrasub}}^{\mathrm{extrasup}}
\end{gather}

\end{document}


With e{^_} the macro looks ahead for ^ or _ (in either order) and assigns #1 to the superscript, #2 to the subscript. You can test for the presence with \IfValueT (or \IfValueTF if you want to do some action with an absent sub/superscript).