Difference between revisions of "Commands with optional arguments"

From Wiki
Jump to navigation Jump to search
(Added example for \command[optional]{text})
Line 56: Line 56:
 
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</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.mkii syst-gen.tex] for the exact names.
 
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</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.mkii syst-gen.tex] for the exact names.
  
----
+
=== Examples ===
  
 
To define <tt>\mycommand[.1.][.2.]{.3.}</tt>, i.e., with curly braces around a non-optional third argument, you just define
 
To define <tt>\mycommand[.1.][.2.]{.3.}</tt>, i.e., with curly braces around a non-optional third argument, you just define
Line 64: Line 64:
 
\def\doMycommand[#1][#2]#3{whatever}
 
\def\doMycommand[#1][#2]#3{whatever}
 
</texcode>
 
</texcode>
 +
 +
 +
 +
 +
To define <code>\mycommand[optional]{text}</code>, do the following
 +
 +
<texcode>
 +
 +
\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}
 +
 +
\mynewcommand{Hello People}
 +
\stoptext
 +
</texcode>
 +
 +
<context>
 +
 +
\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>
 +
  
 
[[Category:Inside ConTeXt]]
 
[[Category:Inside ConTeXt]]

Revision as of 16:16, 12 November 2010

< Inside ConTeXt | Commands with KeyVal arguments >

In LaTeX you define a new command with an optional argument with "newcommand":

\newcommand{\MyCommand}[2][World]{{#2Hello #1!}}
\MyCommand{\bfseries}
\MyCommand[Hans]{\scshape}

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 \dodoubleempty (from 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.

\def\MyCommand{\dodoubleempty\doMyCommand}

We then create the "private" macro (\doMacroName 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 \ifsecondargument are used to determine whether the given argument was specified, as follows:

 \def\doMyCommand[#1][#2]{#1Hello
    \ifsecondargument
       #2%
    \else
       World%
    \fi
    !}

Note that this makes both arguments optional -- something that is much more difficult to do in LaTeX (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 \MyCommand without the second argument ends up gobbling the following spaces, so we need to explicitly include one with "\ ".)

\MyCommand[\bf]\ %
\MyCommand[\sc][Hans]

If you don't want any optional arguments, but still want your arguments enclosed in [] with appropriate handling for spaces (or line breaks) between the square brackets, use \dodoubleargument instead of \dodoubleempty. There are of course versions for other numbers of arguments, found by replacing double with single through seventuple; see syst-gen.tex for the exact names.

Examples

To define \mycommand[.1.][.2.]{.3.}, i.e., with curly braces around a non-optional third argument, you just define

\def\mycommand{\dodoubleempty\doMycommand}
\def\doMycommand[#1][#2]#3{whatever}



To define \mycommand[optional]{text}, do the following


\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}

\mynewcommand{Hello People}
\stoptext