Difference between revisions of "Commands with optional arguments"

From Wiki
Jump to navigation Jump to search
(State \dosingleempty as well, to avoid confusion.)
(Add pitfall with nested macro calls)
 
(4 intermediate revisions by 2 users not shown)
Line 46: Line 46:
 
=== Examples ===
 
=== 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 <code>\mycommand[#1]{#2}</code> with one optional argument and one mandatory argument, do the following
 
+
<context source="yes">
<texcode>
 
\def\mycommand{\dodoubleempty\doMycommand}
 
\def\doMycommand[#1][#2]#3{whatever}
 
</texcode>
 
 
 
To define <code>\mycommand[optional]{text}</code>, do the following
 
<texcode>
 
 
\def\mynewcommand{\dosingleempty\doMyNewCommand}
 
\def\mynewcommand{\dosingleempty\doMyNewCommand}
 
\def\doMyNewCommand[#1]#2{%
 
\def\doMyNewCommand[#1]#2{%
 
  \iffirstargument
 
  \iffirstargument
   There is an optional parameter: {\bf #1}\par
+
   There is an optional parameter: {\bf #1}\par%
 
  \else
 
  \else
   No optional parameter\par
+
   No optional parameter\par%
 
  \fi
 
  \fi
   This is the mandatory text: {\em #2}
+
   This is the mandatory text: {\em #2}%
 
}
 
}
  
 
\starttext
 
\starttext
 
\mynewcommand[opt]{Hello People}
 
\mynewcommand[opt]{Hello People}
 
+
\blank
 
\mynewcommand{Hello People}
 
\mynewcommand{Hello People}
 
\stoptext
 
\stoptext
</texcode>
+
</context>
  
<context>
 
  
\def\mynewcommand{\dosingleempty\doMyNewCommand}
+
To define <code>\mycommand[#1][#2]{#3}</code> with two optional arguments and one mandatory argument, do
\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
+
<texcode>
\mynewcommand[opt]{Hello People}
+
\def\mycommand{\dodoubleempty\doMycommand}
\blank
+
\def\doMycommand[#1][#2]#3{whatever}
\mynewcommand{Hello People}
+
</texcode>
\stoptext
 
</context>
 
  
 
=== Pitfalls ===
 
=== Pitfalls ===
  
Please keep in mind that <code>\iffirstargument</code> will always return true if you put before it a command which itself has an argument. See the following example:
+
==== Passing preceding command's argument ====
 +
 
 +
Keep in mind that <code>\iffirstargument</code> will always return true if you put before it a command which itself has an argument. See the following example:
  
 
<context source="yes" text="produces">
 
<context source="yes" text="produces">
Line 100: Line 84:
 
  \startalignment[center]
 
  \startalignment[center]
 
  \iffirstargument
 
  \iffirstargument
   There is an optional parameter: {\bf #1}\par
+
   There is an optional parameter: {\bf #1}\par%
 
  \else
 
  \else
   No optional parameter\par
+
   No optional parameter\par%
 
  \fi
 
  \fi
   This is the mandatory text: {\em #2}
+
   This is the mandatory text: {\em #2}%
 
  \stopalignment
 
  \stopalignment
 
}
 
}
Line 116: Line 100:
 
Use <code>\doifsomethingelse</code> instead:
 
Use <code>\doifsomethingelse</code> instead:
  
<context source="yes" text="this time is correct:">
+
<context source="yes" text="This time this is correct:">
 
\def\mynewcommand{\dosingleempty\doMyNewCommand}
 
\def\mynewcommand{\dosingleempty\doMyNewCommand}
 
\def\doMyNewCommand[#1]#2{%
 
\def\doMyNewCommand[#1]#2{%
  \startalignment[center]
+
  \startalignment[center]%
 
  \doifsomethingelse{#1}
 
  \doifsomethingelse{#1}
 
   {There is an optional parameter: {\bf #1}\par}
 
   {There is an optional parameter: {\bf #1}\par}
 
   {No optional parameter\par}
 
   {No optional parameter\par}
 
   This is the mandatory text: {\em #2}
 
   This is the mandatory text: {\em #2}
  \stopalignment
+
  \stopalignment%
 
}
 
}
 
\starttext
 
\starttext
Line 133: Line 117:
 
</context>
 
</context>
  
 +
==== Calling the macro from within command arguments (e.g. float title) ====
 +
 +
-- I have no deeper insight yet on the why and neither on which other cases this might apply to, but I hope it may be useful to someone running into errors.
 +
 +
If you wanted to call your macro from within an "argument" such as you would have to with titles or captions in floats:
 +
 +
<texcode>
 +
\startplacefigure[title={We look at \mynewcommand{people} in floats}]
 +
(content)
 +
\stopplacefigure
 +
</texcode>
 +
 +
The above will create a `"\doMyNewCommand doesn't match its definition"` error.
 +
This can be solved by preceding the definition that takes the optional argument with `\unexpanded`:
 +
 +
<texcode>
 +
\unexpanded\def\doMyNewCommand[#1]#2{% ... (etc.)
 +
</texcode>
 +
 +
=== Comparison with LaTeX ===
  
 
On a final note, for comparative purposes: in LaTeX, a new command with an optional argument is defined with <code>\newcommand</code>.
 
On a final note, for comparative purposes: in LaTeX, a new command with an optional argument is defined with <code>\newcommand</code>.
Line 145: Line 149:
 
http://archive.contextgarden.net/message/20101215.225603.cc903e62.en.html
 
http://archive.contextgarden.net/message/20101215.225603.cc903e62.en.html
  
[[Category:Inside ConTeXt]]
+
[[Category:Programming and Databases]]
 +
[[Category:Tools]]

Latest revision as of 20:38, 14 August 2021

< 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 \dosingleempty, \dodoubleempty, \dotripleempty, ... (from syst-aux.mkiv or syst-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:

\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-aux.mkiv for the exact names.

Examples

To define \mycommand[#1]{#2} with one optional argument and one mandatory argument, 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}
\blank
\mynewcommand{Hello People}
\stoptext


To define \mycommand[#1][#2]{#3} with two optional arguments and one mandatory argument, do

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

Pitfalls

Passing preceding command's argument

Keep in mind that \iffirstargument will always return true if you put before it a command which itself has an argument. See the following example:

\def\mynewcommand{\dosingleempty\doMyNewCommand}
\def\doMyNewCommand[#1]#2{%
 \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
}
\starttext
\mynewcommand[opt]{Hello People}
\blank
\mynewcommand{Hello People}
\stoptext

produces

Use \doifsomethingelse instead:

\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
\mynewcommand[opt]{Hello People}
\blank
\mynewcommand{Hello People}
\stoptext

This time this is correct:

Calling the macro from within command arguments (e.g. float title)

-- I have no deeper insight yet on the why and neither on which other cases this might apply to, but I hope it may be useful to someone running into errors.

If you wanted to call your macro from within an "argument" such as you would have to with titles or captions in floats:

\startplacefigure[title={We look at \mynewcommand{people} in floats}]
(content)
\stopplacefigure

The above will create a "\doMyNewCommand doesn't match its definition" error. This can be solved by preceding the definition that takes the optional argument with \unexpanded:

\unexpanded\def\doMyNewCommand[#1]#2{% ... (etc.)

Comparison with LaTeX

On a final note, for comparative purposes: in LaTeX, a new command with an optional argument is defined with \newcommand.

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

Reference: http://archive.contextgarden.net/message/20101215.225603.cc903e62.en.html