wrong spacing after command with optional arguments
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
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
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
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
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
participants (2)
-
Pablo Rodriguez
-
Wolfgang Schuster