ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Macros which scan subscripts/superscripts in ConTeXt
@ 2020-06-26  0:43 Jairo A. del Rio
  2020-06-26  1:47 ` Aditya Mahajan
  0 siblings, 1 reply; 3+ messages in thread
From: Jairo A. del Rio @ 2020-06-26  0:43 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1133 bytes --]

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?

Thank you in advance.

Jairo :)

[-- Attachment #1.2: Type: text/html, Size: 6491 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Macros which scan subscripts/superscripts in ConTeXt
  2020-06-26  0:43 Macros which scan subscripts/superscripts in ConTeXt Jairo A. del Rio
@ 2020-06-26  1:47 ` Aditya Mahajan
  2020-06-26  8:03   ` Hans Hagen
  0 siblings, 1 reply; 3+ messages in thread
From: Aditya Mahajan @ 2020-06-26  1:47 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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


Aditya

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Macros which scan subscripts/superscripts in ConTeXt
  2020-06-26  1:47 ` Aditya Mahajan
@ 2020-06-26  8:03   ` Hans Hagen
  0 siblings, 0 replies; 3+ messages in thread
From: Hans Hagen @ 2020-06-26  8:03 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Aditya Mahajan

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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-06-26  8:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-26  0:43 Macros which scan subscripts/superscripts in ConTeXt Jairo A. del Rio
2020-06-26  1:47 ` Aditya Mahajan
2020-06-26  8:03   ` Hans Hagen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).