ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Macro with variable number of arguments
@ 2010-02-28  8:54 Otared Kavian
  2010-02-28  9:17 ` Wolfgang Schuster
  0 siblings, 1 reply; 4+ messages in thread
From: Otared Kavian @ 2010-02-28  8:54 UTC (permalink / raw)
  To: mailing list for ConTeXt users


Hi all,

I would like to write a macro which may have sometimes two, sometimes three arguments. The first and the last one are always present, but not necessarily the middle one. The unsatisfactory solution I have now is something like the example below:

\define[2]\myMacroTwo{Name: #1\crlf Email: #2\crlf\thinrule\blank[small]}
\define[3]\myMacroThree{Name: #1\crlf Address: #2 \crlf Email: #3\crlf\thinrule\blank[small]}

\starttext
\myMacroTwo{Hans}{hans@hagen.context.nl}
\myMacroThree{Wolfgang}{Paradise of TeX, 2010}{wolfgang@schuster.context.de}
\stoptext

I think that (or rather I am certain…) in ConTeXt it is possible to define a unique macro called \myMacro which automatically tests the number of arguments: if there are  only two of them the commands in \myMacroTwo are executed, while if three arguments are present, the commands in \myMacroThree are executed.

Can anyone give a hint on this matter? 
___________________________________________________________________________________
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: Macro with variable number of arguments
  2010-02-28  8:54 Macro with variable number of arguments Otared Kavian
@ 2010-02-28  9:17 ` Wolfgang Schuster
  2010-02-28 11:44   ` Otared Kavian
  0 siblings, 1 reply; 4+ messages in thread
From: Wolfgang Schuster @ 2010-02-28  9:17 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Am 28.02.10 09:54, schrieb Otared Kavian:
> Hi all,
>
> I would like to write a macro which may have sometimes two, sometimes three arguments. The first and the last one are always present, but not necessarily the middle one. The unsatisfactory solution I have now is something like the example below:
>
> \define[2]\myMacroTwo{Name: #1\crlf Email: #2\crlf\thinrule\blank[small]}
> \define[3]\myMacroThree{Name: #1\crlf Address: #2 \crlf Email: #3\crlf\thinrule\blank[small]}
>
> \starttext
> \myMacroTwo{Hans}{hans@hagen.context.nl}
> \myMacroThree{Wolfgang}{Paradise of TeX, 2010}{wolfgang@schuster.context.de}
> \stoptext
>
> I think that (or rather I am certain…) in ConTeXt it is possible to define a unique macro called \myMacro which automatically tests the number of arguments: if there are  only two of them the commands in \myMacroTwo are executed, while if three arguments are present, the commands in \myMacroThree are executed.
>
> Can anyone give a hint on this matter?
>    
\def\myMacro
   {\dotriplegroupempty\domyMacro}

\def\domyMacro#1#2#3%
   {\doifelsenothing{#3}
      {Name: #1\crlf Email: #2\crlf}
      {Name: #1\crlf Address: #2 \crlf Email: #3\crlf}%
    \thinrule\blank[small]}

Wolfgang

___________________________________________________________________________________
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: Macro with variable number of arguments
  2010-02-28  9:17 ` Wolfgang Schuster
@ 2010-02-28 11:44   ` Otared Kavian
  2010-02-28 11:48     ` Hans Hagen
  0 siblings, 1 reply; 4+ messages in thread
From: Otared Kavian @ 2010-02-28 11:44 UTC (permalink / raw)
  To: mailing list for ConTeXt users


On 28 févr. 2010, at 10:17, Wolfgang Schuster wrote:

> \def\myMacro
>  {\dotriplegroupempty\domyMacro}
> 
> \def\domyMacro#1#2#3%
>  {\doifelsenothing{#3}
>     {Name: #1\crlf Email: #2\crlf}
>     {Name: #1\crlf Address: #2 \crlf Email: #3\crlf}%
>   \thinrule\blank[small]}
> 
> Wolfgang

Many thanks Wolfgang! Your solution solves my question.

Now for a complete understanding of the solution (and before putting your solution on the wiki), here is another question.
After reading your solution, I searched in the source files and saw that in the file
syst-gen.mkii
the command \dosinglegroupempty, until \doquintuplegroupempty are defined.
For instance
\def\dotriplegroupempty#1%
  {\def\dodogetargument##1%
     {\def\dodogetargument####1%
        {\def\dodogetargument%
           {\dontpermitspacesbetweengroups
            #1{##1}{####1}}%
         \dogetgroupargument\thirdargumenttrue\thirdargumentfalse}%
      \dogetgroupargument\secondargumenttrue\secondargumentfalse}%
   \dogetgroupargument\firstargumenttrue\firstargumentfalse}

Can one use other constructions like these, for example can one define
\dosepttuplegroupempty 
in order to have macros with 7 arguments, of which 5 are always present, while the sixth and seventh may be absent in some occasions?

Thanks again: OK
___________________________________________________________________________________
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

* Re: Macro with variable number of arguments
  2010-02-28 11:44   ` Otared Kavian
@ 2010-02-28 11:48     ` Hans Hagen
  0 siblings, 0 replies; 4+ messages in thread
From: Hans Hagen @ 2010-02-28 11:48 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Otared Kavian

On 28-2-2010 12:44, Otared Kavian wrote:
>
> On 28 févr. 2010, at 10:17, Wolfgang Schuster wrote:
>
>> \def\myMacro
>>   {\dotriplegroupempty\domyMacro}
>>
>> \def\domyMacro#1#2#3%
>>   {\doifelsenothing{#3}
>>      {Name: #1\crlf Email: #2\crlf}
>>      {Name: #1\crlf Address: #2 \crlf Email: #3\crlf}%
>>    \thinrule\blank[small]}
>>
>> Wolfgang
>
> Many thanks Wolfgang! Your solution solves my question.
>
> Now for a complete understanding of the solution (and before putting your solution on the wiki), here is another question.
> After reading your solution, I searched in the source files and saw that in the file
> syst-gen.mkii
> the command \dosinglegroupempty, until \doquintuplegroupempty are defined.
> For instance
> \def\dotriplegroupempty#1%
>    {\def\dodogetargument##1%
>       {\def\dodogetargument####1%
>          {\def\dodogetargument%
>             {\dontpermitspacesbetweengroups
>              #1{##1}{####1}}%
>           \dogetgroupargument\thirdargumenttrue\thirdargumentfalse}%
>        \dogetgroupargument\secondargumenttrue\secondargumentfalse}%
>     \dogetgroupargument\firstargumenttrue\firstargumentfalse}
>
> Can one use other constructions like these, for example can one define
> \dosepttuplegroupempty
> in order to have macros with 7 arguments, of which 5 are always present, while the sixth and seventh may be absent in some occasions?

sure, you can just test .. \ifsecondargument ... \iffirstargument

but in such cases using key/values might make mode sense


-----------------------------------------------------------------
                                           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
-----------------------------------------------------------------
___________________________________________________________________________________
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://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


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

end of thread, other threads:[~2010-02-28 11:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-28  8:54 Macro with variable number of arguments Otared Kavian
2010-02-28  9:17 ` Wolfgang Schuster
2010-02-28 11:44   ` Otared Kavian
2010-02-28 11:48     ` 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).