Changes

Jump to navigation Jump to search
corrected order of arguments, edited description for clarity
<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]]</i>
In ConTeXt, the optional argument processing is handled as a two-step process. First, we write the internals of command for the desired end-user as a wrapper command are put in , which calls <tt>\dodoubleempty</tt> (from <tt>syst-gen.tex</tt>) to handle the arguments properly -- including the optional ones -- and then calls a "private" command that contains the internals of the macro, without optional . Note that this function call does not explicitly refer to the arguments:at all.
\def\MyCommand{\dodoubleempty\doMyCommand[#1][#2]{{#2Hello #1!}}
Then, this is wrapped in We then create the main command, which calls "private" macro (<tt>\dodoubleemptydoMacroName</tt> (from is the traditional ConTeXt name for these), with all the arguments defined as nonoptional. Default values for the arguments need to be handled somewhat more explicitly than with LaTeX; macros such as <tt>syst-gen.tex\ifsecondargument</tt>) are used to handle determine whether the arguments -- including the optional ones. Note that this function call does not explicitly refer to the arguments at all.given argument was specified, as follows:
\def\MyCommand{\dodoubleempty \doMyCommand} 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 <tt>\iffirstargument</tt> are used to determine whether the given argument was specified. Thus, we could handle those in <tt>\doMyCommand</tt>:  \def\doMyCommand[#1][#2]{#2Hello1Hello \iffirstargumentifsecondargument #12%
\else
World%
!}
Note that this makes both arguments optional -- something that is much more difficult to do in LaTeX. This also means that we should reverse the order of arguments, since if the user specifies only one argument it will be treated as the first argument.
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 <tt>\dodoublearguments</tt> instead of <tt>\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.tex</tt> for the exact names.
----
Also, does someone know how to define \mycommand[.1.][.2.]{.3.}? E.g., with curly braces around a non-optional third argument? I think this just involves adding a second, non-delimited argument to <tt>\doMyCommand</tt>, but I'm not sure. Also, can someone test to see if <tt>\dosingleempty</tt> and <tt>\dosingleargument</tt> can be chained, to get only one optional square-bracketed argument?

Navigation menu