Difference between revisions of "Commands with optional arguments"

From Wiki
Jump to navigation Jump to search
(first version)
 
(Added \dodoubleempty, etc.; this needs testing and editing.)
Line 1: Line 1:
 
In LaTeX you define a new command with an optional argument with "newcommand":
 
In LaTeX you define a new command with an optional argument with "newcommand":
  
<small><pre>
+
\newcommand{\MyCommand}[2][World]{{#2Hello #1!}}
\newcommand{\MyCommand}[2][World]{{#2Hello #1!}}
+
\MyCommand{\bfseries}
\MyCommand{\bfseries}
+
\MyCommand[Hans]{\scshape}
\MyCommand[Hans]{\scshape}
 
</pre></small>
 
  
 
ConTeXt:
 
ConTeXt:
  
 
There is perhaps a way to do the same. Otherwise, the key value method is preferred, see [[Define Commands]].
 
There is perhaps a way to do the same. Otherwise, the key value method is preferred, see [[Define Commands]].
 +
 +
----
 +
 +
<i>The following is my understanding of how to do this.  It's currently untested; someone please test this and integrate it with the above.  --[[User:Brooks|Brooks]]</i>
 +
 +
In ConTeXt, the optional argument processing is handled as a two-step process.  First, the internals of the desired command are put in a "private" macro, without optional arguments:
 +
 +
\def\doMyCommand[#1][#2]{{#2Hello #1!}}
 +
 +
Then, this is wrapped in the main command, which calls <tt>\dodoubleempty</tt> (from <tt>syst-gen.tex</tt>) to handle the arguments -- including the optional ones.  Note that this function call does not explicitly refer to the arguments at all.
 +
 +
\def\MyCommand{\dodoubleempty \doMyCommand}
 +
 +
This does not, however, provide a way of directly supplying default values; instead, any values not specified by the user are given as empty.  Macros such as <tt>\iffirstargument</tt> are used to determine whether the given argument was specified.  Thus, we could handle those in <tt>\doMyCommand</tt>:
 +
 +
\def\doMyCommand[#1][#2]{#2Hello
 +
    \iffirstargument
 +
      #1%
 +
    \else
 +
      World%
 +
    \fi
 +
    !}
 +
 +
Note that this makes both arguments optional -- something that is much more difficult to do in LaTeX.
 +
 +
If you don't want any optional arguments, but want your arguments enclosed in <tt>[]</tt> with appropriate handling for spaces (or line breaks) between the square brackets, use <tt>\dodoublearguments</tt> instead of <tt>\dodoubleempty</tt>.  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 <tt>syst-gen.tex</tt> 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?

Revision as of 18:27, 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, the internals of the desired command are put in a "private" macro, without optional arguments:

\def\doMyCommand[#1][#2]{{#2Hello #1!}}

Then, this is wrapped in the main command, which calls \dodoubleempty (from syst-gen.tex) to handle the arguments -- including the optional ones. Note that this function call does not explicitly refer to the arguments at all.

\def\MyCommand{\dodoubleempty \doMyCommand}

This does not, however, provide a way of directly supplying default values; instead, any values not specified by the user are given as empty. Macros such as \iffirstargument are used to determine whether the given argument was specified. Thus, we could handle those in \doMyCommand:

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

Note that this makes both arguments optional -- something that is much more difficult to do in LaTeX.

If you don't want any optional arguments, but 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?