Changes

Jump to navigation Jump to search
Added examples.
\MyCommand[Hans]{\scshape}
</texcode>
 
<context>
{\bf Hello World!}
{\sc Hello Hans!}
</context>
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>dodoubleempty</cmd> (from [http://source.contextgarden.net/tex/context/base/syst-gen.tex syst-gen.tex]) 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.
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 user specifies only one argument it will be treated as the first argument.
 
(Also, note that <tt>\MyCommand</tt> without the second argument ends up gobbling the following spaces, so we need to explicitly include one with "<tt>\ </tt>".)
 
<texcode>
\MyCommand[\bf]\ %
\MyCommand[\sc][Hans]
</texcode>
 
<context>
\def\MyCommand{\dodoubleempty\doMyCommand}
\def\doMyCommand[#1][#2]{#1Hello
\ifsecondargument
#2%
\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>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.
Anonymous user

Navigation menu