ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] wrong spacing after command with optional arguments
@ 2024-04-26 15:29 Pablo Rodriguez via ntg-context
  2024-04-26 16:18 ` [NTG-context] " Wolfgang Schuster
  0 siblings, 1 reply; 5+ messages in thread
From: Pablo Rodriguez via ntg-context @ 2024-04-26 15:29 UTC (permalink / raw)
  To: ConTeXt users; +Cc: Pablo Rodriguez

Dear list,

I have the following sample:

  \starttext
  \def\MyCommand{\doquadruplegroupempty\doMyCommand}

  \def\doMyCommand#1#2#3#4{%
    \iffourthargument
      #4%
    \orelse\ifthirdargument
      #3%
    \else
      #2%
    \fi}

  \MyCommand{}{second}{third}{fourth},\\
  \MyCommand{}{second}{third}{fourth} e,\\

  \MyCommand{}{second}{third},\\
  \MyCommand{}{second}{third} e,\\

  \MyCommand{}{second},\\
  \MyCommand{}{second} e,\\
  \stoptext

I don‘t know why only the command gets the space after right only when
the four arguments are provided.

What is wrong in my definition above?

Many thanks for your help,

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

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

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

* [NTG-context] Re: wrong spacing after command with optional arguments
  2024-04-26 15:29 [NTG-context] wrong spacing after command with optional arguments Pablo Rodriguez via ntg-context
@ 2024-04-26 16:18 ` Wolfgang Schuster
  2024-04-26 17:04   ` Pablo Rodriguez via ntg-context
  0 siblings, 1 reply; 5+ messages in thread
From: Wolfgang Schuster @ 2024-04-26 16:18 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Pablo Rodriguez via ntg-context
  Cc: Pablo Rodriguez

Pablo Rodriguez via ntg-context schrieb am 26.04.2024 um 17:29:
> Dear list,
> 
> I have the following sample:
> 
>    \starttext
>    \def\MyCommand{\doquadruplegroupempty\doMyCommand}
> 
>    \def\doMyCommand#1#2#3#4{%
>      \iffourthargument
>        #4%
>      \orelse\ifthirdargument
>        #3%
>      \else
>        #2%
>      \fi}
> 
>    \MyCommand{}{second}{third}{fourth},\\
>    \MyCommand{}{second}{third}{fourth} e,\\
> 
>    \MyCommand{}{second}{third},\\
>    \MyCommand{}{second}{third} e,\\
> 
>    \MyCommand{}{second},\\
>    \MyCommand{}{second} e,\\
>    \stoptext
> 
> I don‘t know why only the command gets the space after right only when
> the four arguments are provided.
> 
> What is wrong in my definition above?

There is nothing wrong, this is just a side effect of the scanner used 
with the \do...groupempty commands. To have more control about this 
behavior use the \tolerant modifier for \def to change it.

%%%% begin example
\tolerant\def\CommandA#_#*#_#*#_#*#_{(#1)(#2)(#3)(#4)}
\tolerant\def\CommandB#_#,#_#,#_#,#_{(#1)(#2)(#3)(#4)}
	
\starttext

A: \CommandA{a}          xxx\par
B: \CommandB{a}          xxx\blank

A: \CommandA{a}{b}       xxx\par
B: \CommandB{a}{b}       xxx\blank

A: \CommandA{a}{b}{c}    xxx\par
B: \CommandB{a}{b}{c}    xxx\blank

A: \CommandA{a}{b}{c}{d} xxx\par
B: \CommandB{a}{b}{c}{d} xxx\blank

\stoptext
%%%% end example

Another way to avoid this is to create a command with a key-value list.

%%%% begin example
\tolerant\def\Command[#1]%
   {\begingroup
    \getdummyparameters[#1]%
    \doifsomething{\dummyparameter{a}}{(\dummyparameter{a})}%
    \doifsomething{\dummyparameter{b}}{(\dummyparameter{b})}%
    \doifsomething{\dummyparameter{c}}{(\dummyparameter{c})}%
    \doifsomething{\dummyparameter{d}}{(\dummyparameter{d})}%
    \endgroup}

\starttext

\Command[a=1,b=2,c=3,d=4] xxx\par
\Command[a=1,c=3] xxx\par

\stoptext
%%%% end example
Wolfgang
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

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

* [NTG-context] Re: wrong spacing after command with optional arguments
  2024-04-26 16:18 ` [NTG-context] " Wolfgang Schuster
@ 2024-04-26 17:04   ` Pablo Rodriguez via ntg-context
  2024-04-26 17:50     ` Wolfgang Schuster
  0 siblings, 1 reply; 5+ messages in thread
From: Pablo Rodriguez via ntg-context @ 2024-04-26 17:04 UTC (permalink / raw)
  To: ntg-context; +Cc: Pablo Rodriguez

On 4/26/24 18:18, Wolfgang Schuster wrote:
> Pablo Rodriguez via ntg-context schrieb am 26.04.2024 um 17:29:
>> What is wrong in my definition above?
>
> There is nothing wrong, this is just a side effect of the scanner used
> with the \do...groupempty commands. To have more control about this
> behavior use the \tolerant modifier for \def to change it.

Many thanks for your reply, Wolfgang.

I tried this approach (I hope it isn’t wrong):

  \starttext
  \expanded\tolerant\def\MyCommand#_#,#_#,#_#,#_{%
    \doiftextelse{#4}%
      {#4}
      {\doiftextelse{#3}
        {#3}
        {#2}}}

  \MyCommand{}{second}{third}{fourth},\\
  \MyCommand{}{second}{third}{fourth} e,\\

  \MyCommand{}{second}{third},\\
  \MyCommand{}{second}{third} e,\\

  \MyCommand{}{second},\\
  \MyCommand{}{second} e,\\
  \stoptext

BTW, this is the first time I see #_ instead of #1.

There is no way to search for "#_" in the wiki.

Grepping the source, "#_" seems to be used for optional arguments
(mainly in syst-aux.mkxl).

But what are these "#_" (which don’t seem to come from TeX)?

Many thanks for your help,

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

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

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

* [NTG-context] Re: wrong spacing after command with optional arguments
  2024-04-26 17:04   ` Pablo Rodriguez via ntg-context
@ 2024-04-26 17:50     ` Wolfgang Schuster
  2024-04-26 18:59       ` Pablo Rodriguez via ntg-context
  0 siblings, 1 reply; 5+ messages in thread
From: Wolfgang Schuster @ 2024-04-26 17:50 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Pablo Rodriguez via ntg-context
  Cc: Pablo Rodriguez

Pablo Rodriguez via ntg-context schrieb am 26.04.2024 um 19:04:
> On 4/26/24 18:18, Wolfgang Schuster wrote:
>> Pablo Rodriguez via ntg-context schrieb am 26.04.2024 um 17:29:
>>> What is wrong in my definition above?
>>
>> There is nothing wrong, this is just a side effect of the scanner used
>> with the \do...groupempty commands. To have more control about this
>> behavior use the \tolerant modifier for \def to change it.
> 
> Many thanks for your reply, Wolfgang.
> 
> I tried this approach (I hope it isn’t wrong):
> 
>    \starttext
>    \expanded\tolerant\def\MyCommand#_#,#_#,#_#,#_{%
>      \doiftextelse{#4}%
>        {#4}
>        {\doiftextelse{#3}
>          {#3}
>          {#2}}}

The \expanded modifier is wrong (I guess you meant \unexpanded) but 
which check for the number of arguments you use doesn't matter in a 
document.

The LMTX version of the \iffourthargument etc. checks is the 
\ifarguments commands where you use \or to set a case for the number of 
argument passed.

\protected\tolerant\def\MyCommand#_#,#_#,#_#,#_%
   {\ifarguments
      % no arguments are passed
    \or
      #1% one argument is passed
    \or
      #2% two arguments are passed
    \or
      #3% three arguments are passed
    \or
      #4% four arguments are passed
    \fi}


>    \MyCommand{}{second}{third}{fourth},\\
>    \MyCommand{}{second}{third}{fourth} e,\\
> 
>    \MyCommand{}{second}{third},\\
>    \MyCommand{}{second}{third} e,\\
> 
>    \MyCommand{}{second},\\
>    \MyCommand{}{second} e,\\
>    \stoptext
> 
> BTW, this is the first time I see #_ instead of #1.
> 
> There is no way to search for "#_" in the wiki.
> 
> Grepping the source, "#_" seems to be used for optional arguments
> (mainly in syst-aux.mkxl).
> 
> But what are these "#_" (which don’t seem to come from TeX)?

Hans added many additions to command parameters in Luametatex and #_ 
means the argument to a commands has to be enclosed in braces while #1
allows you to pass text without it.

In the following example \CommandA take in the first case "a" as 
argument while CommandB ignores it and would only accept "{a}" with 
braces around it.

%%%% begin example
          \def\CommandA#1{(#1)}
\tolerant\def\CommandB#_{(#1)}

\starttext

\CommandA abc\par
\CommandB abc\blank

\CommandA {ab}c\par
\CommandB {ab}c\blank

\stoptext
%%%% end example

You can find a list with all new command parameters in lowlevel-macros.pdf.

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

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

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

* [NTG-context] Re: wrong spacing after command with optional arguments
  2024-04-26 17:50     ` Wolfgang Schuster
@ 2024-04-26 18:59       ` Pablo Rodriguez via ntg-context
  0 siblings, 0 replies; 5+ messages in thread
From: Pablo Rodriguez via ntg-context @ 2024-04-26 18:59 UTC (permalink / raw)
  To: ntg-context; +Cc: Pablo Rodriguez

On 4/26/24 19:50, Wolfgang Schuster wrote:
> Pablo Rodriguez via ntg-context schrieb am 26.04.2024 um 19:04:
>> I tried this approach (I hope it isn’t wrong):
>>
>>    \starttext
>>    \expanded\tolerant\def\MyCommand#_#,#_#,#_#,#_{%
>
> The \expanded modifier is wrong (I guess you meant \unexpanded) but
> which check for the number of arguments you use doesn't matter in a
> document.

You are right, I meant \unexpanded.

> The LMTX version of the \iffourthargument etc. checks is the
> \ifarguments commands where you use \or to set a case for the number of
> argument passed.

Many thanks for explaining this too.

>> But what are these "#_" (which don’t seem to come from TeX)?
>
> Hans added many additions to command parameters in Luametatex and #_
> means the argument to a commands has to be enclosed in braces while #1
> allows you to pass text without it.

I get it now.

> You can find a list with all new command parameters in lowlevel-macros.pdf.

An interesting reading for the weekend.

Many thanks for your help,

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

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2024-04-26 19:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-26 15:29 [NTG-context] wrong spacing after command with optional arguments Pablo Rodriguez via ntg-context
2024-04-26 16:18 ` [NTG-context] " Wolfgang Schuster
2024-04-26 17:04   ` Pablo Rodriguez via ntg-context
2024-04-26 17:50     ` Wolfgang Schuster
2024-04-26 18:59       ` Pablo Rodriguez 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).