Difference between revisions of "Commands with optional arguments"

From Wiki
Jump to navigation Jump to search
(corrected order of arguments, edited description for clarity)
m (Edited interrogatory at bottom of page slightly.)
Line 33: Line 33:
 
----
 
----
  
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?
+
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>\dodoubleargument</tt> can be chained, to get two square-bracketed arguments only one of which is optional?

Revision as of 18:55, 4 August 2004

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

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

ConTeXt:

There is perhaps a way to do the same. Otherwise, the key value method is preferred, see Define Commands.


The following is my understanding of how to do this. It's currently untested; someone please test this and integrate it with the above. --Brooks

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. 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 [] with appropriate handling for spaces (or line breaks) between the square brackets, use \dodoublearguments 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.


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 \doMyCommand, but I'm not sure. Also, can someone test to see if \dosingleempty and \dodoubleargument can be chained, to get two square-bracketed arguments only one of which is optional?