Changes

Jump to navigation Jump to search
m
no edit summary
< [[Inside ConTeXt]] | [[Commands with KeyVal arguments]] >
In ConTeXt, 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 {{cmd|dosingleempty}}, {{cmd|dodoubleempty}} , {{cmd|dotripleempty}}, ... (from {{src|syst-aux.mkiv}} or {{src|syssyst-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.
For a command with two optional arguments, we use:
<texcode>
\def\MyCommand{\dodoubleempty\doMyCommand}
=== Examples ===
To define <ttcode>\mycommand[.#1.][.2.]{.3.}</tt>, i.e., with curly braces around a non-optional third argument, you just define <texcode>\def\mycommand{\dodoubleempty\doMycommand}\def\doMycommand[#1][#2]#3{whatever}</texcode> To define <code>\mycommand[with one optional]{text}</code>argument and one mandatory argument, do the following<texcodecontext 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
</texcodecontext>
<context>
To define <code>\def\mynewcommand{\dosingleempty\doMyNewCommand}\def\doMyNewCommandmycommand[#1][#2]{% \iffirstargument There is an optional parameter: {\bf #13}\par \else No </code> with two optional parameter\par \fi This is the arguments and one mandatory text: {\em #2}}argument, do
<texcode>\starttextdef\mynewcommand[opt]mycommand{Hello People\dodoubleempty\doMycommand}\blankdef\mynewcommanddoMycommand[#1][#2]#3{Hello Peoplewhatever}\stoptext</contexttexcode>
=== Pitfalls ===
\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
}
\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
http://archive.contextgarden.net/message/20101215.225603.cc903e62.en.html
[[Category:Inside ConTeXtProgramming and Databases]][[Category:Tools]]

Navigation menu