# [texhax] Parsing ^ and _

Uwe Lück uwe.lueck at web.de
Thu Sep 14 00:15:55 CEST 2006

At 00:12 12.09.06, Alex Scorpan wrote
[Michael Barr]:
> > Is there any simple way of parsing ^ and _ that mimics what TeX
> > does, but
> > allows other procedures than just putting in super and subscripts?
> > For
> > example, I have a \to procedure that replaces the plain tex (which
> > is a
> > synonym for \rightarrow) and measures the sizes of the scripts and
> > uses
> > that information to choose the length of the arrow.  As it is, I
> > can use
> > any of \to^{...}, \to_{...}, \to^{...}_{...} but not \to_{...}^
> > {...}.  I
> > already have three uses of \@ifnextchar and the only way I can
> > think to
> > implement allowing all four would use several more.  Is there a
> > simpler
> > way of doing this.

Perhaps the following helps to estimate how far Michael Barr
was from an ideal solution:

\makeatletter
\DeclareRobustCommand{\tooo}{%
\begingroup % drop when sure that \subcontent/supcontent
% not used for a different purpose outside
\let\subcontent\empty \let\supcontent\empty
\@ifnextchar_%
{\@ifnextchar^%
\tooodooo}}
\def#1{#5}%
\@ifnextchar#2%
{\@firstoftwo{\afterassignment\tooodooo \def#3}}%
\tooodooo}
% \CheckCommand{\@firstoftwo}[2]{#1} % reminder (preamble only)
\makeatother

\newlength{\subsupwidth}

\newcommand{\tooodooo}{% replace this:
%   [arrow long enough for both of \subcontent'/\supcontent']%
\settowidth{\subsupwidth}{$\mathop{}_{\subcontent}^{\supcontent}$}%
\ensuremath{%
{\mathop{\makebox[\subsupwidth]{\rightarrowfill}}%
\limits_{\subcontent}^{\supcontent}}}%
% ... by your \to_{\subcontent}^{\supcontent}', e.g.
\endgroup % drop when sure that \subcontent/supcontent
% not used for a different purpose outside
}

% outcome:

X \tooo_{oo}^{ooo} Y \SEPARATE
X \tooo^{oo}_{ooo} Y \SEPARATE
X \tooo_{oooo}     Y \SEPARATE
X \tooo^{ooooo}    Y \SEPARATE

-- Spent hours instead of a few minutes -- as often ...

Note that it may be considered a key-value situation

>On one hand, \xrightarrow[bottom]{top} does pretty much what you want
>in amsmath.

`