ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* How to extend an existing macro to take optional parameters?
@ 2023-03-12 13:24 Bruce Horrocks via ntg-context
  2023-03-13 21:39 ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 5+ messages in thread
From: Bruce Horrocks via ntg-context @ 2023-03-12 13:24 UTC (permalink / raw)
  To: ntg-context mailing list; +Cc: Bruce Horrocks

I have an existing macro that I'd like to extend.

The current definition is \define[3]\mycommand{...} which I'd like to extend to take an optional 4th parameter plus an optional setup parameter.

Thus I currently invoke it as:

  \mycommand{aa}{bb}{cc}

and I'd like to be able to extend it so that any of the following can be used:

  \mycommand{aa}{bb}{cc}
  \mycommand{aa}{bb}{cc}{dd}
  \mycommand{aa}{bb}{cc}{dd}[ee]

or

  \mycommand[ee]{aa}{bb}{cc}{dd}  % if this is more the ConTeXt way 

where {dd} is the optional extra parameter and, if it is present, then [ee] can also be optionally added (because it controls how {dd} is printed).

If it helps, the actual body of the macro can easily be a call to Lua so it's fine if the function takes 5 params where 4 and 5 can be nil or empty strings.

I've trawled through syst-aux.mkiv but it only covers optional numbers of [ ] or { } but not combinations of both (which given the vast numbers of combinations is perhaps not surprising!). Have I missed something obvious that would help me?

Regards,
—
Bruce Horrocks
Hampshire, UK

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

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

* Re: How to extend an existing macro to take optional parameters?
  2023-03-12 13:24 How to extend an existing macro to take optional parameters? Bruce Horrocks via ntg-context
@ 2023-03-13 21:39 ` Hans Hagen via ntg-context
  2023-03-14  2:08   ` Alan Braslau via ntg-context
  2023-03-15  1:27   ` Bruce Horrocks via ntg-context
  0 siblings, 2 replies; 5+ messages in thread
From: Hans Hagen via ntg-context @ 2023-03-13 21:39 UTC (permalink / raw)
  To: Bruce Horrocks via ntg-context; +Cc: Hans Hagen

On 3/12/2023 2:24 PM, Bruce Horrocks via ntg-context wrote:
> I have an existing macro that I'd like to extend.
> 
> The current definition is \define[3]\mycommand{...} which I'd like to extend to take an optional 4th parameter plus an optional setup parameter.
> 
> Thus I currently invoke it as:
> 
>    \mycommand{aa}{bb}{cc}
> 
> and I'd like to be able to extend it so that any of the following can be used:
> 
>    \mycommand{aa}{bb}{cc}
>    \mycommand{aa}{bb}{cc}{dd}
>    \mycommand{aa}{bb}{cc}{dd}[ee]
> 
> or
> 
>    \mycommand[ee]{aa}{bb}{cc}{dd}  % if this is more the ConTeXt way
> 
> where {dd} is the optional extra parameter and, if it is present, then [ee] can also be optionally added (because it controls how {dd} is printed).
> 
> If it helps, the actual body of the macro can easily be a call to Lua so it's fine if the function takes 5 params where 4 and 5 can be nil or empty strings.
> 
> I've trawled through syst-aux.mkiv but it only covers optional numbers of [ ] or { } but not combinations of both (which given the vast numbers of combinations is perhaps not surprising!). Have I missed something obvious that would help me?
So, four optional with mandate {} plus a fallback to an optional []

\starttext

\tolerant\def\mycommand#=#=#=#=#:[#5]%
   {\ifparameter#1\or(1:#1)\fi
    \ifparameter#2\or(2:#2)\fi
    \ifparameter#3\or(3:#3)\fi
    \ifparameter#4\or(4:#4)\fi
    \ifparameter#5\or[5:#5]\fi}

\startbuffer
\mycommand{aa}{bb}{cc}
   \mycommand{aa}{bb}{cc}{dd}
   \mycommand{aa}{bb}{cc}{dd}[ee]
\mycommand{aa}{bb}{cc}
   \mycommand{aa}{bb}{cc}{dd}
   \mycommand{aa}{bb}{cc}{dd}[ee]
\stopbuffer

\typebuffer

\startlines \getbuffer \stoplines

\stoptext

I'm not saying that it's the best solution, you can add \protected in 
front of the \def if needed.

Hans

-----------------------------------------------------------------
                                           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 / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

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

* Re: How to extend an existing macro to take optional parameters?
  2023-03-13 21:39 ` Hans Hagen via ntg-context
@ 2023-03-14  2:08   ` Alan Braslau via ntg-context
  2023-03-15  1:32     ` Bruce Horrocks via ntg-context
  2023-03-15  1:27   ` Bruce Horrocks via ntg-context
  1 sibling, 1 reply; 5+ messages in thread
From: Alan Braslau via ntg-context @ 2023-03-14  2:08 UTC (permalink / raw)
  To: Bruce Horrocks via ntg-context; +Cc: Alan Braslau

Designating "optional" parameters within [...] is a LaTeX notion.
ConTeXt handles parameters differently.

Alan


> > and I'd like to be able to extend it so that any of the following
> > can be used:
> > 
> >    \mycommand{aa}{bb}{cc}
> >    \mycommand{aa}{bb}{cc}{dd}
> >    \mycommand{aa}{bb}{cc}{dd}[ee]
> > 
> > or
> > 
> >    \mycommand[ee]{aa}{bb}{cc}{dd}  % if this is more the ConTeXt way
> > 
> > where {dd} is the optional extra parameter and, if it is present,
> > then [ee] can also be optionally added (because it controls how
> > {dd} is printed).
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

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

* Re: How to extend an existing macro to take optional parameters?
  2023-03-13 21:39 ` Hans Hagen via ntg-context
  2023-03-14  2:08   ` Alan Braslau via ntg-context
@ 2023-03-15  1:27   ` Bruce Horrocks via ntg-context
  1 sibling, 0 replies; 5+ messages in thread
From: Bruce Horrocks via ntg-context @ 2023-03-15  1:27 UTC (permalink / raw)
  To: ntg-context mailing list; +Cc: Bruce Horrocks



> On 13 Mar 2023, at 21:39, Hans Hagen via ntg-context <ntg-context@ntg.nl> wrote:
> 
> On 3/12/2023 2:24 PM, Bruce Horrocks via ntg-context wrote:
>> I have an existing macro that I'd like to extend.
>> The current definition is \define[3]\mycommand{...} which I'd like to extend to take an optional 4th parameter plus an optional setup parameter.
>> Thus I currently invoke it as:
>>   \mycommand{aa}{bb}{cc}
>> and I'd like to be able to extend it so that any of the following can be used:
>>   \mycommand{aa}{bb}{cc}
>>   \mycommand{aa}{bb}{cc}{dd}
>>   \mycommand{aa}{bb}{cc}{dd}[ee]
>> or
>>   \mycommand[ee]{aa}{bb}{cc}{dd}  % if this is more the ConTeXt way
>> where {dd} is the optional extra parameter and, if it is present, then [ee] can also be optionally added (because it controls how {dd} is printed).
>> If it helps, the actual body of the macro can easily be a call to Lua so it's fine if the function takes 5 params where 4 and 5 can be nil or empty strings.
>> I've trawled through syst-aux.mkiv but it only covers optional numbers of [ ] or { } but not combinations of both (which given the vast numbers of combinations is perhaps not surprising!). Have I missed something obvious that would help me?
> So, four optional with mandate {} plus a fallback to an optional []
> 
> \starttext
> 
> \tolerant\def\mycommand#=#=#=#=#:[#5]%
>  {\ifparameter#1\or(1:#1)\fi
>   \ifparameter#2\or(2:#2)\fi
>   \ifparameter#3\or(3:#3)\fi
>   \ifparameter#4\or(4:#4)\fi
>   \ifparameter#5\or[5:#5]\fi}
> 
> \startbuffer
> \mycommand{aa}{bb}{cc}
>  \mycommand{aa}{bb}{cc}{dd}
>  \mycommand{aa}{bb}{cc}{dd}[ee]
> \mycommand{aa}{bb}{cc}
>  \mycommand{aa}{bb}{cc}{dd}
>  \mycommand{aa}{bb}{cc}{dd}[ee]
> \stopbuffer
> 
> \typebuffer
> 
> \startlines \getbuffer \stoplines
> 
> \stoptext
> 
> I'm not saying that it's the best solution, you can add \protected in front of the \def if needed.
> 
> Hans

Thanks Hans,

This works well. I'll try and add something to the Wiki but I don't understand how #: does what it does.

Regards,
—
Bruce Horrocks
Hampshire, UK

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

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

* Re: How to extend an existing macro to take optional parameters?
  2023-03-14  2:08   ` Alan Braslau via ntg-context
@ 2023-03-15  1:32     ` Bruce Horrocks via ntg-context
  0 siblings, 0 replies; 5+ messages in thread
From: Bruce Horrocks via ntg-context @ 2023-03-15  1:32 UTC (permalink / raw)
  To: ntg-context mailing list; +Cc: Bruce Horrocks



> On 14 Mar 2023, at 02:08, Alan Braslau via ntg-context <ntg-context@ntg.nl> wrote:
> 
> Designating "optional" parameters within [...] is a LaTeX notion.
> ConTeXt handles parameters differently.
> 
> Alan

Thanks Alan but I've never used LaTeX so I've no idea what the philosophical differences are.

I wanted three mandatory and two optional params. One of those optional params is not typeset so I assumed it should be [ ] delimited and not { }.

> 
>>> and I'd like to be able to extend it so that any of the following
>>> can be used:
>>> 
>>>   \mycommand{aa}{bb}{cc}
>>>   \mycommand{aa}{bb}{cc}{dd}
>>>   \mycommand{aa}{bb}{cc}{dd}[ee]
>>> 
>>> or
>>> 
>>>   \mycommand[ee]{aa}{bb}{cc}{dd}  % if this is more the ConTeXt way
>>> 
>>> where {dd} is the optional extra parameter and, if it is present,
>>> then [ee] can also be optionally added (because it controls how
>>> {dd} is printed).

—
Bruce Horrocks
Hampshire, UK

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2023-03-15  1:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-12 13:24 How to extend an existing macro to take optional parameters? Bruce Horrocks via ntg-context
2023-03-13 21:39 ` Hans Hagen via ntg-context
2023-03-14  2:08   ` Alan Braslau via ntg-context
2023-03-15  1:32     ` Bruce Horrocks via ntg-context
2023-03-15  1:27   ` Bruce Horrocks via ntg-context

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