Changes

Jump to navigation Jump to search
m
no edit summary
< [[Inside ConTeXt]] | [[Commands with KeyVal arguments]] >
In LaTeX you define ConTeXt, the optional argument processing is handled as a two-step process. First, we write the command for the end-user as a new wrapper command with an , which calls {{cmd|dosingleempty}}, {{cmd|dodoubleempty}}, {{cmd|dotripleempty}}, ... (from {{src|syst-aux.mkiv}} or {{src|syst-gen.mkii}}) to handle the arguments properly -- including the optional argument with ones -- and then calls a "newcommandprivate":command that contains the internals of the macro. Note that this function call does not explicitly refer to the arguments at all.
For a command with two optional arguments, we use:
<texcode>
\newcommand{\MyCommand}[2][World]{{#2Hello #1!}}def\MyCommand{\bfseries}\MyCommand[Hans]{dodoubleempty\scshapedoMyCommand}
</texcode>
In ConTeXt, We then create the optional argument processing is handled as a two-step process. First, we write the command for the end-user as a wrapper command, which calls "private" macro (<cmdtt>dodoubleempty\doMacroName</cmdtt> (from [http://source.contextgarden.net/tex/context/base/syst-gen.tex syst-gen.tex]is the traditional ConTeXt name for these) to handle , with all the arguments properly -- including the optional ones -- and then calls a "private" command that contains the internals of the macrodefined as nonoptional. Note that this function call does not Default values for the arguments need to be handled somewhat more explicitly refer than with LaTeX; macros such as {{cmd|ifsecondargument}} are used to determine whether the arguments at all.given argument was specified, as follows:
<texcode>
\def\MyCommanddoMyCommand[#1][#2]{#1Hello \dodoubleemptyifsecondargument #2% \doMyCommandelse World% \fi !}
</texcode>
We then create Note that this makes both arguments optional -- something that is much more difficult to do in LaTeX ([http://www.tex.ac.uk/cgi-bin/texfaq2html?label=twooptarg but can be done]). This also means that we should reverse the order of arguments, since if the "private" macro user specifies only one argument it will be treated as the first argument. (Also, note that <tt>\doMacroNameMyCommand</tt> is without the second argument ends up gobbling the traditional ConTeXt name for these)following spaces, with all the arguments defined as nonoptional. Default values for the arguments so we need to be handled somewhat more explicitly than include one with LaTeX; macros such as "<cmdtt>ifsecondargument\ </cmdtt> are used to determine whether the given argument was specified, as follows:".)
<texcode>
\MyCommand[\bf]\ %\MyCommand[\sc][Hans]</texcode> <context>\def\MyCommand{\dodoubleempty\doMyCommand}\def\doMyCommand[#1][#2]{#1Hello
\ifsecondargument
#2%
\fi
!}
\MyCommand[\bf]\ %\MyCommand[\sc][Hans]</texcodecontextIf 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 ===
Note that this makes both arguments To define <code>\mycommand[#1]{#2}</code> with one optional -- something that is much more difficult to argument and one mandatory argument, do in LaTeX (the following<context source="yes">\def\mynewcommand{\dosingleempty\doMyNewCommand}\def\doMyNewCommand[http#1]#2{% \iffirstargument There is an optional parameter://www.tex.ac.uk/cgi-bin/texfaq2html?label=twooptarg but can be done]). {\bf #1}\par% \else No optional parameter\par% \fi This also means that we should reverse is the order of arguments, since if the user specifies only one argument it will be treated as the first argument.mandatory text: {\em #2}%}
If you ''don't'' want any optional arguments, but still want your arguments enclosed in <tt>\starttext\mynewcommand[opt]{Hello People}\blank\mynewcommand{Hello People}\stoptext</tt> with appropriate handling for spaces (or line breaks) between the square brackets, use <cmdcontext>dodoublearguments</cmd> instead of <cmd>dodoubleempty</cmd>. 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 [http://source.contextgarden.net/tex/context/base/syst-gen.tex syst-gen.tex] for the exact names.
----
To define <ttcode>\mycommand[.#1.][.#2.]{.#3.}</ttcode>, i.e., with curly braces around a non-two optional third arguments and one mandatory argument, you just definedo
<texcode>
</texcode>
----=== Pitfalls === Please 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 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>  On a final note, for comparative purposes: in LaTeX, a new command with an optional argument is defined with <code>\newcommand</code>. <texcode>\newcommand{\MyCommand}[2][World]{{#2Hello #1!}}\MyCommand{\bfseries}\MyCommand[Hans]{\scshape}</texcode> Reference: http://archive.contextgarden.net/message/20101215.225603.cc903e62.en.html
<i>Can someone test to see if <cmd>dosingleempty</cmd> [[Category:Programming and <cmd>dodoubleargument</cmd> can be chained, to get two square-bracketed arguments only one of which is optional?</i> --Databases]][[UserCategory:Brooks|BrooksTools]]

Navigation menu