[texhax] Parsing ^ and _

Uwe Lück uwe.lueck at web.de
Thu Sep 14 11:24:08 CEST 2006


Admittedly, my previous proposal
can hardly be considered simple, although it's short.

Hopefully more clearly than before on key/value
(to be sure, I am no expert on implementing
  key/value things -- have only be thinking for a while
  if I should use keyval macros, and perhaps you
  find ideas in such packages that help you):
The matter would be very simple with TeX's
parsing mechanism if one certain token ends
the keys/values list ends
(e.g. the \bracket ending the optional argument
  of the geometry package). E.g.:

   \def\lookforsub#1_#2#3\@nil{%
     \def\subcontent{#2}\kill at rest}
   \def\kill at rest#1\@nil{}

   \lookforsub<key-values-list>_{<default>}@\@nil
   [to be continued for other keys]

... requiring that rather something like \to{^{ooo}_{oo}}
is given.

Otherwise you must always test wether the next
token(s) is/are a keyword (not found so far).
This is a little more complex -- determining
\hrule and \vrule dimensions is an example
(so have a look on TeX's WEB source!?).
My previous proposal for just two keys does
not really reveal how to do this in general.

... ... ... Uwe Lueck.
__________________________________

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_%
       {\load at and\subcontent^\supcontent}%
       {\@ifnextchar^%
         {\load at and\supcontent_\subcontent}%
         \tooodooo}}
   \newcommand*{\load at and}[5]{%
     \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}$}%
     \addtolength{\subsupwidth}{1em}%
     \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:

   \newcommand{\SEPARATE}{\qquad}%%%\par}

   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
-- start with thinking of primitive TeX' width, height, depth ...



More information about the texhax mailing list