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.
\begin{equation}
{\mymathsym}^{\text{extra}}
\end{equation}
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
\begin{equation}
{\mymathsymlong}^{\text{extra}}
\end{equation}
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
\begin{equation}
\mymathsymext \qquad\text{vs.}\qquad \mymathsymext[\text{extra}]
\end{equation}
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:
\begin{equation}
{\mymathsym}^{\text{extra}}
\end{equation}
If the primary sup- and subscript are very unbalanced in their length, the secondary subscript is set very far apart:
\begin{equation}
{\mymathsymlong}^{\text{extra}}
\end{equation}
This extended macro takes an optional argument and ``absorbs'' the extra superscript into the primary superscript:
\begin{equation}
\mymathsymext \qquad\text{vs.}\qquad \mymathsymext[\text{extra}]
\end{equation}
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}
\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 thexparse
package for something capable of picking up sub- and super script.\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.\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.\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.