ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Question about defining a math macro
@ 2007-02-24  8:55 Aditya Mahajan
  2007-02-24  9:52 ` Taco Hoekwater
  0 siblings, 1 reply; 5+ messages in thread
From: Aditya Mahajan @ 2007-02-24  8:55 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi,

Can I define a math macro, say \EXP that does the following:

1) \EXP{something} gives me, say E(something)
2) \EXP_a^b{something} gives my E_a^b(something)

That is, I want to write a macro that will only be used in the math 
mode, that does something with its argument. But, I also want it to 
handle _ and ^ if they are present.

The only method that I can think of is to check _ or ^ with 
\ifnextcharacter and go through all 5 cases (nothing, only _, only ^, 
_ followed by ^, ^ followed by _) but this seems very inefficient. Is 
there a better way?

Aditya

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

* Re: Question about defining a math macro
  2007-02-24  8:55 Question about defining a math macro Aditya Mahajan
@ 2007-02-24  9:52 ` Taco Hoekwater
  2007-02-24  9:59   ` Aditya Mahajan
  0 siblings, 1 reply; 5+ messages in thread
From: Taco Hoekwater @ 2007-02-24  9:52 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Aditya Mahajan wrote:
> Hi,
> 
> Can I define a math macro, say \EXP that does the following:
> 
> 1) \EXP{something} gives me, say E(something)
> 2) \EXP_a^b{something} gives my E_a^b(something)
> 
> That is, I want to write a macro that will only be used in the math 
> mode, that does something with its argument. But, I also want it to 
> handle _ and ^ if they are present.
> 
> The only method that I can think of is to check _ or ^ with 
> \ifnextcharacter and go through all 5 cases (nothing, only _, only ^, 
> _ followed by ^, ^ followed by _) but this seems very inefficient. Is 
> there a better way?

Actually, there is, by using a bit of black magic in the
macro definition:

   \def\EXP#1#{\bgroup\def\EXParg{#1}\doEXP}
   \def\doEXP#1{\mfunction{E}\EXParg({\rm #1})\egroup}

That extra hash mark has the effect of putting everything
upto the next explicit left brace into #1.

Cheers, Taco

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

* Re: Question about defining a math macro
  2007-02-24  9:52 ` Taco Hoekwater
@ 2007-02-24  9:59   ` Aditya Mahajan
  2007-02-24 11:20     ` Hans Hagen
  0 siblings, 1 reply; 5+ messages in thread
From: Aditya Mahajan @ 2007-02-24  9:59 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Sat, 24 Feb 2007, Taco Hoekwater wrote:

> Aditya Mahajan wrote:
>> Hi,
>>
>> Can I define a math macro, say \EXP that does the following:
>>
>> 1) \EXP{something} gives me, say E(something)
>> 2) \EXP_a^b{something} gives my E_a^b(something)
>>
>> That is, I want to write a macro that will only be used in the math
>> mode, that does something with its argument. But, I also want it to
>> handle _ and ^ if they are present.
>>
>> The only method that I can think of is to check _ or ^ with
>> \ifnextcharacter and go through all 5 cases (nothing, only _, only ^,
>> _ followed by ^, ^ followed by _) but this seems very inefficient. Is
>> there a better way?
>
> Actually, there is, by using a bit of black magic in the
> macro definition:
>
>   \def\EXP#1#{\bgroup\def\EXParg{#1}\doEXP}
>   \def\doEXP#1{\mfunction{E}\EXParg({\rm #1})\egroup}
>
> That extra hash mark has the effect of putting everything
> upto the next explicit left brace into #1.

Wow Taco. This is great.

Thanks,
Aditya

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

* Re: Question about defining a math macro
  2007-02-24  9:59   ` Aditya Mahajan
@ 2007-02-24 11:20     ` Hans Hagen
  2007-02-24 22:56       ` Aditya Mahajan
  0 siblings, 1 reply; 5+ messages in thread
From: Hans Hagen @ 2007-02-24 11:20 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Aditya Mahajan wrote:
> On Sat, 24 Feb 2007, Taco Hoekwater wrote:
>
>   
>> Aditya Mahajan wrote:
>>     
>>> Hi,
>>>
>>> Can I define a math macro, say \EXP that does the following:
>>>
>>> 1) \EXP{something} gives me, say E(something)
>>> 2) \EXP_a^b{something} gives my E_a^b(something)
>>>
>>> That is, I want to write a macro that will only be used in the math
>>> mode, that does something with its argument. But, I also want it to
>>> handle _ and ^ if they are present.
>>>
>>> The only method that I can think of is to check _ or ^ with
>>> \ifnextcharacter and go through all 5 cases (nothing, only _, only ^,
>>> _ followed by ^, ^ followed by _) but this seems very inefficient. Is
>>> there a better way?
>>>       
>> Actually, there is, by using a bit of black magic in the
>> macro definition:
>>
>>   \def\EXP#1#{\bgroup\def\EXParg{#1}\doEXP}
>>   \def\doEXP#1{\mfunction{E}\EXParg({\rm #1})\egroup}
>>
>> That extra hash mark has the effect of putting everything
>> upto the next explicit left brace into #1.
>>     
>
> Wow Taco. This is great.
>   
some applications of this mechanism can be found in the context source code (supp-box etc) 

Hans 


-----------------------------------------------------------------
                                          Hans Hagen | PRAGMA ADE
              Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
                                             | www.pragma-pod.nl
-----------------------------------------------------------------

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

* Re: Question about defining a math macro
  2007-02-24 11:20     ` Hans Hagen
@ 2007-02-24 22:56       ` Aditya Mahajan
  0 siblings, 0 replies; 5+ messages in thread
From: Aditya Mahajan @ 2007-02-24 22:56 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Sat, 24 Feb 2007, Hans Hagen wrote:

> Aditya Mahajan wrote:
>> On Sat, 24 Feb 2007, Taco Hoekwater wrote:
>>
>>
>>> Aditya Mahajan wrote:
>>>
>>>> Hi,
>>>>
>>>> Can I define a math macro, say \EXP that does the following:
>>>>
>>>> 1) \EXP{something} gives me, say E(something)
>>>> 2) \EXP_a^b{something} gives my E_a^b(something)
>>>>
>>>> That is, I want to write a macro that will only be used in the math
>>>> mode, that does something with its argument. But, I also want it to
>>>> handle _ and ^ if they are present.
>>>>
>>>> The only method that I can think of is to check _ or ^ with
>>>> \ifnextcharacter and go through all 5 cases (nothing, only _, only ^,
>>>> _ followed by ^, ^ followed by _) but this seems very inefficient. Is
>>>> there a better way?
>>>>
>>> Actually, there is, by using a bit of black magic in the
>>> macro definition:
>>>
>>>   \def\EXP#1#{\bgroup\def\EXParg{#1}\doEXP}
>>>   \def\doEXP#1{\mfunction{E}\EXParg({\rm #1})\egroup}
>>>
>>> That extra hash mark has the effect of putting everything
>>> upto the next explicit left brace into #1.
>>>
>>
>> Wow Taco. This is great.
>>
> some applications of this mechanism can be found in the context source code (supp-box etc)

Yes, those were parts of the core that I could never understand. Now, 
I do understand them a little.

Thanks,
Aditya

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

end of thread, other threads:[~2007-02-24 22:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-24  8:55 Question about defining a math macro Aditya Mahajan
2007-02-24  9:52 ` Taco Hoekwater
2007-02-24  9:59   ` Aditya Mahajan
2007-02-24 11:20     ` Hans Hagen
2007-02-24 22:56       ` Aditya Mahajan

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).