Open main menu

Changes

Add pitfall with nested macro calls
In LaTeX you define a new command < [[Inside ConTeXt]] | [[Commands with an optional argument with "newcommand":KeyVal arguments]] >
In ConTeXt, the optional argument processing is handled as a two-step process. \newcommandFirst, we write the command for the end-user as a wrapper command, which calls {\MyCommand{cmd|dosingleempty}}[2][World], {{#2Hello #1!cmd|dodoubleempty}} \MyCommand, {{cmd|dotripleempty}}, ... (from {{\bfseriessrc|syst-aux.mkiv} \MyCommand[Hans]} or {{\scshapesrc|syst-gen.mkii}}) to handle the arguments properly -- including the optional ones -- and then calls a "private" command that contains the internals of the macro. Note that this function call does not explicitly refer to the arguments at all.
ConTeXtFor a command with two optional arguments, we use:<texcode>\def\MyCommand{\dodoubleempty\doMyCommand}</texcode>
There We then create the "private" macro (<tt>\doMacroName</tt> is perhaps a way to do the sametraditional ConTeXt name for these), with all the arguments defined as nonoptional. Otherwise, Default values for the arguments need to be handled somewhat more explicitly than with LaTeX; macros such as {{cmd|ifsecondargument}} are used to determine whether the key value method is preferredgiven argument was specified, see [[Define Commands]].as follows:
---- <i>The following is my understanding of how to do this. It's currently untested; someone please test this and integrate it with the above. --[[User:Brooks|Brooks]]</itexcodeIn ConTeXt, the optional argument processing is handled as a two-step process. First, the internals of the desired command are put in a "private" macro, without optional arguments:  \def\doMyCommand[#1][#2]{{#2Hello 1Hello \ifsecondargument #12% \else World% \fi !}}</texcode>
Then, Note that this makes both arguments optional -- something that is wrapped much more difficult to do in the main command, which calls <tt>\dodoubleempty<LaTeX ([http://tt> (from <tt>syst-genwww.tex<.ac.uk/cgi-bin/tt>texfaq2html?label=twooptarg but can be done]) to handle the arguments -- including the optional ones. Note This also means that this function call does not explicitly refer to we should reverse the order of arguments at all, since if the user specifies only one argument it will be treated as the first argument.
\def(Also, note that <tt>\MyCommand{\dodoubleempty </tt> without the second argument ends up gobbling the following spaces, so we need to explicitly include one with "<tt>\doMyCommand}</tt>".)
This does not, however, provide a way of directly supplying default values; instead, any values not specified by the user are given as empty. Macros such as <tttexcode>\iffirstargument</tt> are used to determine whether the given argument was specified. Thus, we could handle those in <tt>MyCommand[\bf]\ %\MyCommand[\doMyCommandsc][Hans]</tttexcode>:
<context>\def\MyCommand{\dodoubleempty\doMyCommand}\def\doMyCommand[#1][#2]{#2Hello1Hello \iffirstargumentifsecondargument #12%
\else
World%
\fi
!}
\MyCommand[\bf]\ %
\MyCommand[\sc][Hans]
</context>
 
If you ''don't'' want any optional arguments, but still want your arguments enclosed in <tt>[]</tt> with appropriate handling for spaces (or line breaks) between the square brackets, use {{cmd|dodoubleargument}} instead of {{cmd|dodoubleempty}}. There are of course versions for other numbers of arguments, found by replacing <tt>double</tt> with <tt>single</tt> through <tt>seventuple</tt>; see {{src|syst-aux.mkiv}} for the exact names.
 
=== Examples ===
 
To define <code>\mycommand[#1]{#2}</code> with one optional argument and one mandatory argument, do the following
<context source="yes">
\def\mynewcommand{\dosingleempty\doMyNewCommand}
\def\doMyNewCommand[#1]#2{%
\iffirstargument
There is an optional parameter: {\bf #1}\par%
\else
No optional parameter\par%
\fi
This is the mandatory text: {\em #2}%
}
 
\starttext
\mynewcommand[opt]{Hello People}
\blank
\mynewcommand{Hello People}
\stoptext
</context>
 
 
To define <code>\mycommand[#1][#2]{#3}</code> with two optional arguments and one mandatory argument, do
 
<texcode>
\def\mycommand{\dodoubleempty\doMycommand}
\def\doMycommand[#1][#2]#3{whatever}
</texcode>
 
=== Pitfalls ===
 
==== Passing preceding command's argument ====
 
Keep in mind that <code>\iffirstargument</code> will always return true if you put before it a command which itself has an argument. See the following example:
 
<context source="yes" text="produces">
\def\mynewcommand{\dosingleempty\doMyNewCommand}
\def\doMyNewCommand[#1]#2{%
\startalignment[center]
\iffirstargument
There is an optional parameter: {\bf #1}\par%
\else
No optional parameter\par%
\fi
This is the mandatory text: {\em #2}%
\stopalignment
}
\starttext
\mynewcommand[opt]{Hello People}
\blank
\mynewcommand{Hello People}
\stoptext
</context>
 
Use <code>\doifsomethingelse</code> instead:
 
<context source="yes" text="This time this is correct:">
\def\mynewcommand{\dosingleempty\doMyNewCommand}
\def\doMyNewCommand[#1]#2{%
\startalignment[center]%
\doifsomethingelse{#1}
{There is an optional parameter: {\bf #1}\par}
{No optional parameter\par}
This is the mandatory text: {\em #2}
\stopalignment%
}
\starttext
\mynewcommand[opt]{Hello People}
\blank
\mynewcommand{Hello People}
\stoptext
</context>
 
==== Calling the macro from within command arguments (e.g. float title) ====
 
-- I have no deeper insight yet on the why and neither on which other cases this might apply to, but I hope it may be useful to someone running into errors.
 
If you wanted to call your macro from within an "argument" such as you would have to with titles or captions in floats:
 
<texcode>
\startplacefigure[title={We look at \mynewcommand{people} in floats}]
(content)
\stopplacefigure
</texcode>
 
The above will create a `"\doMyNewCommand doesn't match its definition"` error.
This can be solved by preceding the definition that takes the optional argument with `\unexpanded`:
 
<texcode>
\unexpanded\def\doMyNewCommand[#1]#2{% ... (etc.)
</texcode>
 
=== Comparison with LaTeX ===
Note that this makes both arguments On a final note, for comparative purposes: in LaTeX, a new command with an optional -- something that argument is much more difficult to do in LaTeXdefined with <code>\newcommand</code>.
If you don't want any optional arguments, but want your arguments enclosed in <tttexcode>\newcommand{\MyCommand}[2]</tt> with appropriate handling for spaces (or line breaks) between the square brackets, use <tt>[World]{{#2Hello #1!}}\MyCommand{\bfseries}\dodoublearguments</tt> instead of <tt>MyCommand[Hans]{\dodoubleempty</tt>. There are of course versions for other numbers of arguments, found by replacing <tt>double</tt> with <tt>single</tt> through <tt>seventuple</tt>; see <tt>syst-gen.texscshape}</tttexcode> for the exact names.
----Reference: http://archive.contextgarden.net/message/20101215.225603.cc903e62.en.html
Also, does someone know how to define \mycommand[.1.[Category:Programming and Databases]][[.2.Category:Tools]]{.3.}? E.g., with curly braces around a non-optional third argument?
57

edits