On 6/26/2020 3:47 AM, Aditya Mahajan wrote:
On Thu, 25 Jun 2020, Jairo A. del Rio wrote:
Hi, list! I've seen the following in Stack Exchange:
https://tex.stackexchange.com/questions/503208/define-macro-that-scans-ahead-for-super-and-or-subscript-absorbs-them-and-mo
The answer is:
\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}
Such a macro contextually defines a behavior for a subscript and a
superscript even if they aren't displayed in a specific order, so
\mymacro_{a}^{b}
\mymacro^{b}_{a}
have the same output, even if the intended behavior is not the typical
one (I could redefine it so subscripts change colors, for instance).
Although I could just port the macro using xparse-generic because it
works in ConTeXt too, how to define a macro like that the ConTeXt way,
i.e., using only ConTeXt macros?
Return of the dodo :-)
% Must be outside \unprotect .. \protect so that _ has usual catcodes
\def\capturemathoplimits#1%
{\doifnextcharelse _%
{\docapturemathoplimitsA{#1}}
{\doifnextcharelse ^%
{\docapturemathoplimitsB{#1}}
{\redocapturemathoplimits{#1}}}}
\def\docapturemathoplimitsA#1#2#3%
{\doifnextcharelse ^%
{\dodocapturemathoplimitsA{#1}{#2}{#3}}
{\dodocapturemathoplimitsA{#1}{#2}{#3}\empty\empty}}
\def\dodocapturemathoplimitsA#1#2#3#4#5{#1{#5}{#3}}
\def\docapturemathoplimitsB#1#2#3%
{\doifnextcharelse _%
{\dodocapturemathoplimitsB{#1}{#2}{#3}}
{\dodocapturemathoplimitsB{#1}{#2}{#3}\empty\empty}}
\def\dodocapturemathoplimitsB#1#2#3#4#5{#1{#3}{#5}}
\def\redocapturemathoplimits#1{#1\empty\empty\empty\empty\empty}
% Usage:
\define\mymacro{\capturemathoplimits\domymacro}
\def\domymacro#1#2%
% #1 is supscript, #2 is subscript
{x^{\mathrm{foo}\doifsomething{#1}{,#1}}_{\mathrm{bar}\doifsomething{#2}{,#2}}}
\starttext
\startlines
$\mymacro$ $\mymacro^{\mathrm{extrasup}}$
$\mymacro_{\mathrm{extrasub}}$
$\mymacro^{\mathrm{extrasup}}_{\mathrm{extrasub}}$
$\mymacro_{\mathrm{extrasub}}^{\mathrm{extrasup}}$ \stoplines
\stoptext
We could have this:
\unexpanded\def\scripts
{\scratchtoksone\emptytoks
\scratchtokstwo\emptytoks
\doscriptscheck}
\def\doscriptscheck
{\doifnextcharelse _%
\doscriptssub
{\doifnextcharelse ^%
\doscriptssup
\doscriptsflush}}
\def\doscriptsflush
{\doifsometoks\scratchtoksone{^{\the\scratchtoksone}}%
\doifsometoks\scratchtokstwo{_{\the\scratchtokstwo}}}
\def\doscriptssup ^#1%
{\doifelsesometoks\scratchtoksone
{\appendtoks,{#1}\to\scratchtoksone}
{\scratchtoksone{{#1}}}%
\doscriptscheck}
\def\doscriptssub _#1%
{\doifelsesometoks\scratchtokstwo
{\appendtoks,{#1}\to\scratchtokstwo}
{\scratchtokstwo{{#1}}}%
\doscriptscheck}
% % a bit more low level:
% \unexpanded\def\scripts
% {\scratchtoksone\emptytoks
% \scratchtokstwo\emptytoks
% \doscriptscheck}
%
% \def\doscriptscheck
% {\doifnextcharelse ^%
% {\doscriptsstep\scratchtoksone}
% {\doifnextcharelse _%
% {\doscriptsstep\scratchtokstwo}
% {\doscriptsflush}}}
%
% \def\doscriptsflush
%
{\iftok\scratchtoksone\emptytoks\else\Usuperscript{\the\scratchtoksone}\fi%
% \iftok\scratchtokstwo\emptytoks\else\Usubscript
{\the\scratchtokstwo}\fi}
%
% \def\doscriptsstep#1#1#3%
% {\etoksapp#1{\iftok#1\emptytoks\else,\fi\normalunexpanded{#3}}%
% \doscriptscheck}
\starttext
\startlines
$z\scripts _{\rm one} _{\bf two} ^{one} _{ three} ^{\bi two}$
\stoplines
\stoptext
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the
Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________