https://wiki.contextgarden.net/api.php?action=feedcontributions&user=Jaystrict&feedformat=atomWiki - User contributions [en]2024-03-29T06:19:25ZUser contributionsMediaWiki 1.34.1https://wiki.contextgarden.net/index.php?title=Multiline_equations&diff=22908Multiline equations2015-06-22T09:12:58Z<p>Jaystrict: Add ties (non-breaking spaces) before equation numbers and variable names.</p>
<hr />
<div>< [[Main Page]] | [[Math]] | [[Math with newmat]] | [[MathML]] ><br />
<br />
Be sure to also read [http://dl.contextgarden.net/myway/mathalign.pdf Using \startalign and friends] written by Aditya Mahajan.<br />
<br />
== Basic Alignment ==<br />
<br />
<br />
Two modes of input <br />
<br />
* Latex style<br />
<br />
<context source=yes text="Note the \\ in the last line, above. This appears as follows:"><br />
\startformula \startalign<br />
v &= u + at \\<br />
h &= ut + \frac12 gt^2 \\<br />
\stopalign \stopformula<br />
</context><br />
<br />
* Context Style<br />
<br />
<context source=yes><br />
\startformula \startalign<br />
\NC v \NC = u + at \NR<br />
\NC h \NC = ut + \frac12 gt^2 \NR<br />
\stopalign \stopformula<br />
</context><br />
<br />
(The examples here will focus on the context style, having two styles can be confusing --[[User:Adityam| Aditya ]] )<br />
<br />
== Changing the number of columns ==<br />
<br />
The above equations were aligned at <code>=</code>. Suppose you also want the <code>+</code> to align. Well, this is simple in context, simply specify the number of columns with <code>\startalign</code><br />
<br />
<context source=yes><br />
\startformula \startalign[n=3]<br />
\NC v \NC = u \NC+ at \NR<br />
\NC h \NC= ut \NC+ \frac12 gt^2 \NR<br />
\stopalign \stopformula<br />
</context><br />
<br />
== Alignment of each column ==<br />
If you want more control over the formatting, and want the middle column to be center aligned, you can do that by <br />
<br />
<context source=yes><br />
\startformula \startalign[n=3,align={right,middle,left}]<br />
\NC v \NC = u \NC+ at \NR<br />
\NC h \NC= ut \NC+ \frac12 gt^2 \NR<br />
\stopalign \stopformula<br />
</context><br />
<br />
This mechanism allows fancier alignments like<br />
<br />
<context source=yes><br />
\startformula \startalign[n=4,align={left,right,middle,left}]<br />
\NC \text{We have} \quad \NC v \NC = u \NC+ at \NR<br />
\NC \text{and} \NC h \NC= ut \NC+ \frac12 gt^2 \NR<br />
\stopalign \stopformula<br />
</context><br />
<br />
== Working with equation numbering ==<br />
<br />
aligned equations can be numbered by placing a tag after {{cmd|NR}}<br />
<br />
<context source=yes><br />
\setuplayout[scale=0.8,width=13cm]<br />
\placeformula \startformula \startalign<br />
\NC v \NC = u + at \NR[eq:v]<br />
\NC h \NC = ut + \frac12 gt^2 \NR[eq:h]<br />
\stopalign \stopformula <br />
Equation~(\in[eq:v]) tells the final velocity after time~$t$ and equation~(\in[eq:h]) tells the distance travelled in time~$t$.<br />
</context><br />
<br />
The numbering can be changed to a subformula style using {{cmd|placesubformula}} instead of {{cmd|placeformula}} and numbering the subformulas using {{cmd|NR|2=[+][a]}}, {{cmd|NR|2=[+][b]}}, … instead:<br />
<br />
<context source=yes><br />
\setuplayout[scale=0.8,width=13cm]<br />
\placesubformula \startformula \startalign<br />
\NC v \NC = u + at \NR[+][eq:v]<br />
\NC h \NC = ut + \frac12 gt^2 \NR[+][eq:h]<br />
\stopalign \stopformula <br />
Equation~(\in[eq:v]) tells the final velocity after time~$t$ and equation (\in[eq:h]) tells the distance travelled in time~$t$.<br />
</context><br />
<br />
== Changing the number of columns ==<br />
<br />
== Defining new alignment structures ==<br />
<br />
New alignment can be defined using {{cmd|definemathalignment}}. For example, to emulate <code>gather</code> environment of amsmath, we can use<br />
<br />
<context source=yes><br />
\definemathalignment<br />
[gather]<br />
[n=1,align={middle}]<br />
<br />
\startformula \startgather<br />
\NC ax^2 + bx + c = 0 \NR<br />
\NC \text{roots} = \frac{ -b \pm \sqrt{b^2 - 4ac}}{2a} \NR<br />
\stopgather \stopformula<br />
<br />
</context><br />
<br />
<br />
== Cases ==<br />
<br />
Context provides a {{cmd|startmathcases}} {{cmd|stopmathcases}} pair to make it easy to get cases. <br />
<br />
<context source=yes text="gives"><br />
\startformula<br />
f(x) = \startmathcases<br />
\NC x, \NC if $0 \le x \le \frac12$ \NR<br />
\NC 1-x ,\NC if $\frac12 \le x \le 1$ \NR<br />
\stopmathcases<br />
\stopformula<br />
</context><br />
<br />
The cases environment consists of two columns, separated by {{cmd|NC}}. The second column is by default in text mode. An alternative way of getting the same result is to define the second column as a mathcolumn with {{cmd|MC}} like so:<br />
<br />
<texcode><br />
\startformula<br />
f(x) = \startmathcases<br />
\NC x, \MC \text{if } 0 \le x \le \frac12 \NR<br />
\NC 1-x ,\MC \text{if } \frac12 \le x \le 1 \NR<br />
\stopmathcases<br />
\stopformula<br />
</texcode><br />
<br />
Each line must end with a {{cmd|NR}}.<br />
<br />
== Numbered Cases ==<br />
<br />
[[Category:Math]]</div>Jaystricthttps://wiki.contextgarden.net/index.php?title=Commands_with_optional_arguments&diff=22907Commands with optional arguments2015-06-22T09:00:11Z<p>Jaystrict: Change order in which examples appear.</p>
<hr />
<div>< [[Inside ConTeXt]] | [[Commands with KeyVal arguments]] ><br />
<br />
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|dosingleempty}}, {{cmd|dodoubleempty}}, {{cmd|dotripleempty}}, ... (from {{src|syst-aux.mkiv}} or {{src|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.<br />
<br />
For a command with two optional arguments, we use:<br />
<texcode><br />
\def\MyCommand{\dodoubleempty\doMyCommand}<br />
</texcode><br />
<br />
We then create the "private" macro (<tt>\doMacroName</tt> 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 {{cmd|ifsecondargument}} are used to determine whether the given argument was specified, as follows:<br />
<br />
<texcode><br />
\def\doMyCommand[#1][#2]{#1Hello<br />
\ifsecondargument<br />
#2%<br />
\else<br />
World%<br />
\fi<br />
!}<br />
</texcode><br />
<br />
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.<br />
<br />
(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>".)<br />
<br />
<texcode><br />
\MyCommand[\bf]\ %<br />
\MyCommand[\sc][Hans]<br />
</texcode><br />
<br />
<context><br />
\def\MyCommand{\dodoubleempty\doMyCommand}<br />
\def\doMyCommand[#1][#2]{#1Hello<br />
\ifsecondargument<br />
#2%<br />
\else<br />
World%<br />
\fi<br />
!}<br />
\MyCommand[\bf]\ %<br />
\MyCommand[\sc][Hans]<br />
</context><br />
<br />
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}} instead of {{cmd|dodoubleempty}}. 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 {{src|syst-aux.mkiv}} for the exact names.<br />
<br />
=== Examples ===<br />
<br />
To define <code>\mycommand[#1]{#2}</code> with one optional argument and one mandatory argument, do the following<br />
<context source="yes"><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par%<br />
\else<br />
No optional parameter\par%<br />
\fi<br />
This is the mandatory text: {\em #2}%<br />
}<br />
<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
<br />
To define <code>\mycommand[#1][#2]{#3}</code> with two optional arguments and one mandatory argument, do<br />
<br />
<texcode><br />
\def\mycommand{\dodoubleempty\doMycommand}<br />
\def\doMycommand[#1][#2]#3{whatever}<br />
</texcode><br />
<br />
=== Pitfalls ===<br />
<br />
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:<br />
<br />
<context source="yes" text="produces"><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\startalignment[center]<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par%<br />
\else<br />
No optional parameter\par%<br />
\fi<br />
This is the mandatory text: {\em #2}%<br />
\stopalignment<br />
}<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
Use <code>\doifsomethingelse</code> instead:<br />
<br />
<context source="yes" text="this time is correct:"><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\startalignment[center]%<br />
\doifsomethingelse{#1}<br />
{There is an optional parameter: {\bf #1}\par}<br />
{No optional parameter\par}<br />
This is the mandatory text: {\em #2}<br />
\stopalignment%<br />
}<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
<br />
On a final note, for comparative purposes: in LaTeX, a new command with an optional argument is defined with <code>\newcommand</code>.<br />
<br />
<texcode><br />
\newcommand{\MyCommand}[2][World]{{#2Hello #1!}}<br />
\MyCommand{\bfseries}<br />
\MyCommand[Hans]{\scshape}<br />
</texcode><br />
<br />
Reference: <br />
http://archive.contextgarden.net/message/20101215.225603.cc903e62.en.html<br />
<br />
[[Category:Inside ConTeXt]]</div>Jaystricthttps://wiki.contextgarden.net/index.php?title=Commands_with_optional_arguments&diff=22906Commands with optional arguments2015-06-22T08:48:37Z<p>Jaystrict: Add <code> block around command.</p>
<hr />
<div>< [[Inside ConTeXt]] | [[Commands with KeyVal arguments]] ><br />
<br />
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|dosingleempty}}, {{cmd|dodoubleempty}}, {{cmd|dotripleempty}}, ... (from {{src|syst-aux.mkiv}} or {{src|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.<br />
<br />
For a command with two optional arguments, we use:<br />
<texcode><br />
\def\MyCommand{\dodoubleempty\doMyCommand}<br />
</texcode><br />
<br />
We then create the "private" macro (<tt>\doMacroName</tt> 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 {{cmd|ifsecondargument}} are used to determine whether the given argument was specified, as follows:<br />
<br />
<texcode><br />
\def\doMyCommand[#1][#2]{#1Hello<br />
\ifsecondargument<br />
#2%<br />
\else<br />
World%<br />
\fi<br />
!}<br />
</texcode><br />
<br />
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.<br />
<br />
(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>".)<br />
<br />
<texcode><br />
\MyCommand[\bf]\ %<br />
\MyCommand[\sc][Hans]<br />
</texcode><br />
<br />
<context><br />
\def\MyCommand{\dodoubleempty\doMyCommand}<br />
\def\doMyCommand[#1][#2]{#1Hello<br />
\ifsecondargument<br />
#2%<br />
\else<br />
World%<br />
\fi<br />
!}<br />
\MyCommand[\bf]\ %<br />
\MyCommand[\sc][Hans]<br />
</context><br />
<br />
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}} instead of {{cmd|dodoubleempty}}. 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 {{src|syst-aux.mkiv}} for the exact names.<br />
<br />
=== Examples ===<br />
<br />
To define <code>\mycommand[#1][#2]{#3}</code>, i.e., with curly braces around a non-optional third argument, you just define<br />
<br />
<texcode><br />
\def\mycommand{\dodoubleempty\doMycommand}<br />
\def\doMycommand[#1][#2]#3{whatever}<br />
</texcode><br />
<br />
To define <code>\mycommand[optional]{text}</code>, do the following<br />
<texcode><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par%<br />
\else<br />
No optional parameter\par%<br />
\fi<br />
This is the mandatory text: {\em #2}%<br />
}<br />
<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</texcode><br />
<br />
<context><br />
<br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par%<br />
\else<br />
No optional parameter\par%<br />
\fi<br />
This is the mandatory text: {\em #2}%<br />
}<br />
<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
=== Pitfalls ===<br />
<br />
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:<br />
<br />
<context source="yes" text="produces"><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\startalignment[center]<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par%<br />
\else<br />
No optional parameter\par%<br />
\fi<br />
This is the mandatory text: {\em #2}%<br />
\stopalignment<br />
}<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
Use <code>\doifsomethingelse</code> instead:<br />
<br />
<context source="yes" text="this time is correct:"><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\startalignment[center]%<br />
\doifsomethingelse{#1}<br />
{There is an optional parameter: {\bf #1}\par}<br />
{No optional parameter\par}<br />
This is the mandatory text: {\em #2}<br />
\stopalignment%<br />
}<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
<br />
On a final note, for comparative purposes: in LaTeX, a new command with an optional argument is defined with <code>\newcommand</code>.<br />
<br />
<texcode><br />
\newcommand{\MyCommand}[2][World]{{#2Hello #1!}}<br />
\MyCommand{\bfseries}<br />
\MyCommand[Hans]{\scshape}<br />
</texcode><br />
<br />
Reference: <br />
http://archive.contextgarden.net/message/20101215.225603.cc903e62.en.html<br />
<br />
[[Category:Inside ConTeXt]]</div>Jaystricthttps://wiki.contextgarden.net/index.php?title=Commands_with_optional_arguments&diff=22905Commands with optional arguments2015-06-22T08:45:52Z<p>Jaystrict: Add some % at end-of-lines (not so sure whether they are all correct, but better than before).</p>
<hr />
<div>< [[Inside ConTeXt]] | [[Commands with KeyVal arguments]] ><br />
<br />
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|dosingleempty}}, {{cmd|dodoubleempty}}, {{cmd|dotripleempty}}, ... (from {{src|syst-aux.mkiv}} or {{src|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.<br />
<br />
For a command with two optional arguments, we use:<br />
<texcode><br />
\def\MyCommand{\dodoubleempty\doMyCommand}<br />
</texcode><br />
<br />
We then create the "private" macro (<tt>\doMacroName</tt> 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 {{cmd|ifsecondargument}} are used to determine whether the given argument was specified, as follows:<br />
<br />
<texcode><br />
\def\doMyCommand[#1][#2]{#1Hello<br />
\ifsecondargument<br />
#2%<br />
\else<br />
World%<br />
\fi<br />
!}<br />
</texcode><br />
<br />
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.<br />
<br />
(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>".)<br />
<br />
<texcode><br />
\MyCommand[\bf]\ %<br />
\MyCommand[\sc][Hans]<br />
</texcode><br />
<br />
<context><br />
\def\MyCommand{\dodoubleempty\doMyCommand}<br />
\def\doMyCommand[#1][#2]{#1Hello<br />
\ifsecondargument<br />
#2%<br />
\else<br />
World%<br />
\fi<br />
!}<br />
\MyCommand[\bf]\ %<br />
\MyCommand[\sc][Hans]<br />
</context><br />
<br />
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}} instead of {{cmd|dodoubleempty}}. 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 {{src|syst-aux.mkiv}} for the exact names.<br />
<br />
=== Examples ===<br />
<br />
To define <tt>\mycommand[.1.][.2.]{.3.}</tt>, i.e., with curly braces around a non-optional third argument, you just define<br />
<br />
<texcode><br />
\def\mycommand{\dodoubleempty\doMycommand}<br />
\def\doMycommand[#1][#2]#3{whatever}<br />
</texcode><br />
<br />
To define <code>\mycommand[optional]{text}</code>, do the following<br />
<texcode><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par%<br />
\else<br />
No optional parameter\par%<br />
\fi<br />
This is the mandatory text: {\em #2}%<br />
}<br />
<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</texcode><br />
<br />
<context><br />
<br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par%<br />
\else<br />
No optional parameter\par%<br />
\fi<br />
This is the mandatory text: {\em #2}%<br />
}<br />
<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
=== Pitfalls ===<br />
<br />
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:<br />
<br />
<context source="yes" text="produces"><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\startalignment[center]<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par%<br />
\else<br />
No optional parameter\par%<br />
\fi<br />
This is the mandatory text: {\em #2}%<br />
\stopalignment<br />
}<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
Use <code>\doifsomethingelse</code> instead:<br />
<br />
<context source="yes" text="this time is correct:"><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\startalignment[center]%<br />
\doifsomethingelse{#1}<br />
{There is an optional parameter: {\bf #1}\par}<br />
{No optional parameter\par}<br />
This is the mandatory text: {\em #2}<br />
\stopalignment%<br />
}<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
<br />
On a final note, for comparative purposes: in LaTeX, a new command with an optional argument is defined with <code>\newcommand</code>.<br />
<br />
<texcode><br />
\newcommand{\MyCommand}[2][World]{{#2Hello #1!}}<br />
\MyCommand{\bfseries}<br />
\MyCommand[Hans]{\scshape}<br />
</texcode><br />
<br />
Reference: <br />
http://archive.contextgarden.net/message/20101215.225603.cc903e62.en.html<br />
<br />
[[Category:Inside ConTeXt]]</div>Jaystricthttps://wiki.contextgarden.net/index.php?title=Commands_with_optional_arguments&diff=22904Commands with optional arguments2015-06-22T08:16:11Z<p>Jaystrict: State \dosingleempty as well, to avoid confusion.</p>
<hr />
<div>< [[Inside ConTeXt]] | [[Commands with KeyVal arguments]] ><br />
<br />
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|dosingleempty}}, {{cmd|dodoubleempty}}, {{cmd|dotripleempty}}, ... (from {{src|syst-aux.mkiv}} or {{src|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.<br />
<br />
For a command with two optional arguments, we use:<br />
<texcode><br />
\def\MyCommand{\dodoubleempty\doMyCommand}<br />
</texcode><br />
<br />
We then create the "private" macro (<tt>\doMacroName</tt> 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 {{cmd|ifsecondargument}} are used to determine whether the given argument was specified, as follows:<br />
<br />
<texcode><br />
\def\doMyCommand[#1][#2]{#1Hello<br />
\ifsecondargument<br />
#2%<br />
\else<br />
World%<br />
\fi<br />
!}<br />
</texcode><br />
<br />
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.<br />
<br />
(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>".)<br />
<br />
<texcode><br />
\MyCommand[\bf]\ %<br />
\MyCommand[\sc][Hans]<br />
</texcode><br />
<br />
<context><br />
\def\MyCommand{\dodoubleempty\doMyCommand}<br />
\def\doMyCommand[#1][#2]{#1Hello<br />
\ifsecondargument<br />
#2%<br />
\else<br />
World%<br />
\fi<br />
!}<br />
\MyCommand[\bf]\ %<br />
\MyCommand[\sc][Hans]<br />
</context><br />
<br />
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}} instead of {{cmd|dodoubleempty}}. 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 {{src|syst-aux.mkiv}} for the exact names.<br />
<br />
=== Examples ===<br />
<br />
To define <tt>\mycommand[.1.][.2.]{.3.}</tt>, i.e., with curly braces around a non-optional third argument, you just define<br />
<br />
<texcode><br />
\def\mycommand{\dodoubleempty\doMycommand}<br />
\def\doMycommand[#1][#2]#3{whatever}<br />
</texcode><br />
<br />
To define <code>\mycommand[optional]{text}</code>, do the following<br />
<texcode><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par<br />
\else<br />
No optional parameter\par<br />
\fi<br />
This is the mandatory text: {\em #2}<br />
}<br />
<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</texcode><br />
<br />
<context><br />
<br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par<br />
\else<br />
No optional parameter\par<br />
\fi<br />
This is the mandatory text: {\em #2}<br />
}<br />
<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
=== Pitfalls ===<br />
<br />
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:<br />
<br />
<context source="yes" text="produces"><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\startalignment[center]<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par<br />
\else<br />
No optional parameter\par<br />
\fi<br />
This is the mandatory text: {\em #2}<br />
\stopalignment<br />
}<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
Use <code>\doifsomethingelse</code> instead:<br />
<br />
<context source="yes" text="this time is correct:"><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\startalignment[center]<br />
\doifsomethingelse{#1}<br />
{There is an optional parameter: {\bf #1}\par}<br />
{No optional parameter\par}<br />
This is the mandatory text: {\em #2}<br />
\stopalignment<br />
}<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
<br />
On a final note, for comparative purposes: in LaTeX, a new command with an optional argument is defined with <code>\newcommand</code>.<br />
<br />
<texcode><br />
\newcommand{\MyCommand}[2][World]{{#2Hello #1!}}<br />
\MyCommand{\bfseries}<br />
\MyCommand[Hans]{\scshape}<br />
</texcode><br />
<br />
Reference: <br />
http://archive.contextgarden.net/message/20101215.225603.cc903e62.en.html<br />
<br />
[[Category:Inside ConTeXt]]</div>Jaystricthttps://wiki.contextgarden.net/index.php?title=Commands_with_optional_arguments&diff=22903Commands with optional arguments2015-06-22T08:05:15Z<p>Jaystrict: Typo</p>
<hr />
<div>< [[Inside ConTeXt]] | [[Commands with KeyVal arguments]] ><br />
<br />
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}} (from {{src|syst-aux.mkiv}} or {{src|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.<br />
<br />
<texcode><br />
\def\MyCommand{\dodoubleempty\doMyCommand}<br />
</texcode><br />
<br />
We then create the "private" macro (<tt>\doMacroName</tt> 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 {{cmd|ifsecondargument}} are used to determine whether the given argument was specified, as follows:<br />
<br />
<texcode><br />
\def\doMyCommand[#1][#2]{#1Hello<br />
\ifsecondargument<br />
#2%<br />
\else<br />
World%<br />
\fi<br />
!}<br />
</texcode><br />
<br />
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.<br />
<br />
(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>".)<br />
<br />
<texcode><br />
\MyCommand[\bf]\ %<br />
\MyCommand[\sc][Hans]<br />
</texcode><br />
<br />
<context><br />
\def\MyCommand{\dodoubleempty\doMyCommand}<br />
\def\doMyCommand[#1][#2]{#1Hello<br />
\ifsecondargument<br />
#2%<br />
\else<br />
World%<br />
\fi<br />
!}<br />
\MyCommand[\bf]\ %<br />
\MyCommand[\sc][Hans]<br />
</context><br />
<br />
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}} instead of {{cmd|dodoubleempty}}. 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 {{src|syst-aux.mkiv}} for the exact names.<br />
<br />
=== Examples ===<br />
<br />
To define <tt>\mycommand[.1.][.2.]{.3.}</tt>, i.e., with curly braces around a non-optional third argument, you just define<br />
<br />
<texcode><br />
\def\mycommand{\dodoubleempty\doMycommand}<br />
\def\doMycommand[#1][#2]#3{whatever}<br />
</texcode><br />
<br />
To define <code>\mycommand[optional]{text}</code>, do the following<br />
<texcode><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par<br />
\else<br />
No optional parameter\par<br />
\fi<br />
This is the mandatory text: {\em #2}<br />
}<br />
<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</texcode><br />
<br />
<context><br />
<br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par<br />
\else<br />
No optional parameter\par<br />
\fi<br />
This is the mandatory text: {\em #2}<br />
}<br />
<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
=== Pitfalls ===<br />
<br />
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:<br />
<br />
<context source="yes" text="produces"><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\startalignment[center]<br />
\iffirstargument<br />
There is an optional parameter: {\bf #1}\par<br />
\else<br />
No optional parameter\par<br />
\fi<br />
This is the mandatory text: {\em #2}<br />
\stopalignment<br />
}<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
Use <code>\doifsomethingelse</code> instead:<br />
<br />
<context source="yes" text="this time is correct:"><br />
\def\mynewcommand{\dosingleempty\doMyNewCommand}<br />
\def\doMyNewCommand[#1]#2{%<br />
\startalignment[center]<br />
\doifsomethingelse{#1}<br />
{There is an optional parameter: {\bf #1}\par}<br />
{No optional parameter\par}<br />
This is the mandatory text: {\em #2}<br />
\stopalignment<br />
}<br />
\starttext<br />
\mynewcommand[opt]{Hello People}<br />
\blank<br />
\mynewcommand{Hello People}<br />
\stoptext<br />
</context><br />
<br />
<br />
On a final note, for comparative purposes: in LaTeX, a new command with an optional argument is defined with <code>\newcommand</code>.<br />
<br />
<texcode><br />
\newcommand{\MyCommand}[2][World]{{#2Hello #1!}}<br />
\MyCommand{\bfseries}<br />
\MyCommand[Hans]{\scshape}<br />
</texcode><br />
<br />
Reference: <br />
http://archive.contextgarden.net/message/20101215.225603.cc903e62.en.html<br />
<br />
[[Category:Inside ConTeXt]]</div>Jaystrict