Open main menu

Difference between revisions of "Math/Display"

m (display math moved to Math/Display: Better organization)
 
(92 intermediate revisions by 10 users not shown)
Line 1: Line 1:
< [[Math]]|
+
= First {{cmd|startformula}} =
  
= Display Math =
+
Display math is enclosed in a {{cmd|startformula}} / {{cmd|stopformula}} pair.
 +
Thus
  
Display math is enclosed in a <cmd>startformula</cmd> / <cmd>stopformula</cmd> pair. Thus
+
<context source="yes" text="This, when typeset, produces the following:">
 
 
<table width="100%" cols="2"><tr valign="top"><td width="50%">
 
<texcode>
 
 
The famous result (once more) is given by
 
The famous result (once more) is given by
 
\startformula
 
\startformula
c^2 = a^2 + b^2.
+
c^2 = a^2 + b^2
 
\stopformula
 
\stopformula
</texcode>
+
</context>
</td><td>
 
This, when typeset, produces the following:
 
  
<context>
+
= Numbering Formulae {{cmd|startformula}} =
\setuplayout[scale=0.8,width=8cm]
+
 
 +
ConTeXt provides an easy way to number the display maths equations. Simply, insert the {{cmd|startformula}} in the {{cmd|startplaceformula}} environment, and you will get numbered equations.
 +
 
 +
<context source="yes" text="This, when typeset, produces the following:">
 
The famous result (once more) is given by
 
The famous result (once more) is given by
\startformula
+
\startplaceformula
c^2 = a^2 + b^2.
+
\startformula  
 +
c^2 = a^2 + b^2  
 
\stopformula
 
\stopformula
 +
\stopplaceformula
 
</context>
 
</context>
</td></tr></table>
 
  
= Numbering Formulae =
+
The {{cmd|startplaceformula}} command is optional, and produces the equation number; leaving it off produces an unnumbered equation.
  
ConTeXt provides an easy way to number the display maths equations. Simply, put <cmd>placeformula</cmd> before  <cmd>startformula</cmd> / <cmd>stopformula</cmd> pair and you will get numbered equations. Thus,
+
= Referencing formulae {{cmd|in}} and {{cmd|at}}=
<table width="100%" cols="2"><tr valign="top"><td width="50%">
+
 
<texcode>
+
Equations can be referred to by simply adding a label to {{cmd|startplaceformula}} and using command for referencing :
 +
* {{cmd|ref}} (note that it expects two arguments, therefore you need the brackets twice),
 +
* {{cmd|in}} (a kind of <tt>\ref[number][TheRef]</tt>,
 +
* {{cmd|at}} (a kind of <tt>\ref[page][TheRef]</tt>.
 +
 
 +
<context source="yes" text="This, when typeset, produces the following:">
 +
\setupinteraction[state=start]
 
The famous result (once more) is given by
 
The famous result (once more) is given by
\placeformula
 
\startformula
 
c^2 = a^2 + b^2.
 
\stopformula
 
</texcode>
 
</td><td>
 
This, when typeset, produces the following:
 
  
<context>
+
\startplaceformula[eq:myref]                  %  <===== here
\setuplayout[scale=0.8,width=8cm]
+
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
 +
 
 +
And now we can refer to its number by several ways,
 +
\startitemize
 +
\item with \tex{in} : formula \in[eq:myref] or \in{formula}{}[eq:myref]
 +
\item with \tex{at} : formula \in[eq:myref] \at{page}[eq:myref]
 +
\item with \tex{ref} : formula \ref[number] [eq:myref] page \ref[number] [eq:myref]
 +
\stopitemize
 +
</context>
 +
 
 +
By default, only the formula number appears as a reference. This can be changed by using {{cmd|definereferenceformat}}. For example, to create a command <code>\eqref</code> which shows the formula number in brackets, use
 +
 
 +
<context source="yes" text="This, when typeset, produces the following:">
 +
\setupinteraction[state=start]
 +
 
 +
\definereferenceformat[eqref][left=(,right=)]
 +
 
 
The famous result (once more) is given by
 
The famous result (once more) is given by
\placeformula
+
\startplaceformula[eq:myref]
\startformula
+
\startformula c^2 = a^2 + b^2 \stopformula
c^2 = a^2 + b^2.
+
\stopplaceformula
\stopformula
+
 
 +
And now we can refer to its number by several ways,
 +
\startitemize
 +
\item with \tex{in} : formula \in[eq:myref] or \in{formula}{}[eq:myref],
 +
\item with \tex{at} : formula \in[eq:myref] \at{page}[eq:myref],
 +
\item with \tex{ref} : formula \ref[number] [eq:myref] page \ref[number] [eq:myref],
 +
\item with \tex{eqref} : formula \eqref[eq:myref].
 +
\stopitemize
 
</context>
 
</context>
</td></tr></table>
 
  
The <cmd>placeformula</cmd> command is optional, and produces the equation number; leaving it off produces an unnumbered equation.
 
  
== Changing format of numbers ==
+
= Sub-Formulae with {{cmd|startsubformulas}} and {{cmd|startalign}} =
You can use <cmd>setupformulas</cmd> to change the format of numbers. For example to get bold numbers inside square brackets use
 
  
<table width="100%" cols="2" cellpadding="5"><tr valign="top"><td width="50%">
+
To use subformula numbering, you have to use {{cmd|startsubformulas}}/{{cmd|stopsubformulas}} and {{cmd|startalign}}. For example
<texcode>
 
\setupformulas[left={[},right={]},numberstyle=bold]
 
</texcode>
 
</td>
 
  
<td>which gives
+
<context source="yes" text="This, when typeset, produces the following:">
 +
The famous result (once more) is given by
  
<context>
+
Examples:
\setuplayout[scale=0.8,width=8cm]
+
\definereferenceformat[eqref][left=(,right=)]
\setupformulas[left={[},right={]},numberstyle=bold]
+
 
The famous result (once more) is given by
+
\startsubformulas[eq:total]
 
\placeformula
 
\placeformula
 
\startformula
 
\startformula
c^2 = a^2 + b^2.
+
\startalign
 +
\NC a^2 + b^2 \NC = c^2 \NR[eq:first]
 +
\NC a^2 + b^2 \NC = c^2 \NR[eq:second]
 +
\stopalign
 
\stopformula
 
\stopformula
</context>
+
\stopsubformulas
</td></tr></table>
+
 
 +
Formula \eqref[eq:total] states the Pythagora's Theorem twice,
 +
once in \eqref[eq:first] and again in \eqref[eq:second].
  
To get alphabets instead of numbers, use
+
You can compare without \tex{startsubformulas}:
<table width="100%" cols="2" cellpadding="5"><tr valign="top"><td width="50%">
 
<texcode>
 
\setupformulas[conversion=Character]
 
</texcode>
 
</td>
 
<td>which gives
 
<context>
 
\setuplayout[scale=0.8,width=8cm]
 
\setupformulas[conversion=Character]
 
 
\placeformula
 
\placeformula
 
\startformula
 
\startformula
c^2 = a^2 + b^2
+
\startalign
 +
\NC a^2 + b^2 \NC = c^2 \NR[eq2:first]
 +
\NC a^2 + b^2 \NC = c^2 \NR[eq2:second]
 +
\stopalign
 
\stopformula
 
\stopformula
 +
 +
We now have references for \eqref[eq2:first] and \eqref[eq2:second].
 
</context>
 
</context>
</td></tr></table>
 
  
== Changing Formula alignment ==
+
= List of Formulae with {{cmd|placelist}} =
  
Normally a formula is centered, but in case you want to align it left or right, you can set up formulas to behave
+
Since MKIV there are two types of formulas: numbered and named. Numbered formulas have the number as a reference and caption next to the formula and are listed only with the number and pagenumber in the list of formulas. Named formulas use the name as the caption instead of a number and are referenced with the name and pagenumber in the list of formulas.
that way. Normally a formula will adapt its left indentation to the environment:
+
 
 +
<context source=yes>
 +
2 examples:
 +
 
 +
% no reference        ==> no entry in the list of formulas
 +
\startplaceformula
 +
\startformula e=mc^2 \stopformula
 +
\stopplaceformula
 +
 
 +
% a reference        ==> entry in the list of formulas
 +
\startplaceformula
 +
  [reference=formula:mass1]              %  <====== here
 +
\startformula e=mc^2 \stopformula
 +
\stopplaceformula
 +
 
 +
This is the list:
  
<context>
+
\placelist[formula][criterium=all,alternative=c]
\setuplayout[textwidth=8cm]
 
This is a bit of text for purpose of example.\epar
 
\startitemize
 
\item This is some other example text.\epar
 
      \placeformula \startformula c^2 = a^2 + b^2 \stopformula
 
\item This is yet other example text.\epar
 
\stopitemize
 
This is a bit more text for other purpose of example.\epar
 
 
</context>
 
</context>
  
In the next examples we explicitly align formulas to the left (<cmd>raggedleft</cmd>), center and right (<cmd>raggedright</cmd>):
+
{{cmd|startplaceformula}} comes with the following key options: bookmark, list, reference, suffix, title. The title option changes the formula to a named formula (no number).
 +
 
 +
<context source=yes>
 +
3 examples:
  
<context source="yes" text="Or in print:">
+
% a reference + list  ==> entry with number + additional text in the list
\setuplayout[textwidth=8cm]
+
\startplaceformula
\setupformulas[align=left]
+
  [reference=formula:mass2,
\startformula c^2 = a^2 + b^2 \stopformula
+
  list={Mass-energy equivalence from list option}]
\setupformulas[align=middle]
+
\startformula e=mc^2 \stopformula
\startformula c^2 = a^2 + b^2 \stopformula
+
\stopplaceformula
\setupformulas[align=right]
 
\startformula c^2 = a^2 + b^2 \stopformula
 
</context>
 
  
With formula numbers the code is:
+
% a reference + title  ==> entry with title text, no number
 +
\startplaceformula
 +
  [title={Mass-energy equivalence},    %  <====== here
 +
  reference=formula:mass3]
 +
\startformula e=mc^2 \stopformula
 +
\stopplaceformula
  
<context source="yes" text="And the formulas look like:">
+
% a reference + title + list ==> entry with list text, no number
\setuplayout[textwidth=8cm]
+
\startplaceformula
\setupformulas[align=left]
+
  [title={Mass-energy equivalence},    %  <====== here
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
  reference=formula:mass4,
\setupformulas[align=middle]
+
  list={Mass-energy equivalence from list option}]
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
\startformula e=mc^2 \stopformula
\setupformulas[align=right]
+
\stopplaceformula
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
 
</context>
 
  
When tracing is turned on (<cmd>tracemathtrue</cmd>) you can visualize the bounding box of the formula,
+
This is the list:
  
<context>
+
\placelist[formula][criterium=all,alternative=c]
\setuplayout[textwidth=8cm]
 
\tracemathtrue
 
\setupformulas[align=left]
 
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
 
\setupformulas[align=middle]
 
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
 
\setupformulas[align=right]
 
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
 
 
</context>
 
</context>
  
As you can see, the dimensions are the natural ones, but if needed you can force a normalized line:
+
= Formating with {{cmd|setupformulas}} =
  
<context source="yes" text="This time we get a more spacy result. [Ed. Note: For this example equation, there appears to be no visible change.]">
+
== Format of numbers ==
\setuplayout[textwidth=8cm]
+
You can use {{cmd|setupformulas}} to change the format of numbers. For example to get bold numbers inside square brackets use
\setupformulas[align=middle,strut=yes]
 
\tracemathtrue
 
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
 
</context>
 
  
We will now show a couple of more settings and combinations of settings. In centered formulas, the number takes no space
+
<context source="yes" text="This, when typeset, produces the following:">
 +
The famous result (once more) is given by
 +
\setupformulas
 +
  [left={[},
 +
  right={]},
 +
  numberstyle=bold]
  
<context source="yes">
+
\startplaceformula
\setuplayout[textwidth=8cm]
 
\tracemathtrue
 
\setupformulas[align=middle]
 
 
\startformula c^2 = a^2 + b^2 \stopformula
 
\startformula c^2 = a^2 + b^2 \stopformula
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
\stopplaceformula
 
</context>
 
</context>
  
You can influence the placement of the whole box with the parameters <tt>leftmargin</tt> and <tt>rightmargin</tt>.
+
To get equations also numbered by section,
 +
use the command {{cmd|setupnumbering}} <code>\setupnumbering[way=bysection]</code> in the preamble of your document.
 +
This means that the section number preceeds the formula number.
 +
Add also {{cmd|setupnumber}} <code>\setupnumber[formula][way=bysection]</code> to reset with each new section.
 +
 
 +
<context source="yes" text="This, when typeset, produces the following:">
 +
The famous result (once more) is given by
 +
\setupformulas
 +
  [left={[},
 +
  right={]},
 +
  numberstyle=bold]
 +
 
 +
\setupnumbering        [way=bysection] % to add the section number to the formula number
 +
\setupnumber [formula] [way=bysection] % to reset with each new section
  
<context source="yes">
+
\startsection[title=First title]
\setuplayout[textwidth=8cm]
+
\startplaceformula
Some example text, again, to show where the right and left margins of the text block are.
 
\tracemathtrue
 
\setupformulas[align=right,leftmargin=3em]
 
 
\startformula c^2 = a^2 + b^2 \stopformula
 
\startformula c^2 = a^2 + b^2 \stopformula
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
\stopplaceformula
 +
\stopsection
  
\setupformulas[align=left,rightmargin=1em]
+
\startsection[title=Second title]
 +
\startplaceformula
 
\startformula c^2 = a^2 + b^2 \stopformula
 
\startformula c^2 = a^2 + b^2 \stopformula
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
\stopplaceformula
 +
\stopsection
 
</context>
 
</context>
  
You can also inherit the margin from the environment.
+
To get alphabets instead of numbers, use again {{cmd|setupformulas}}
 +
 
 +
<context source="yes" text="This, when typeset, produces the following:">
 +
The famous result (once more) is given by
 +
\setupformulas[numberconversion=Character]
  
<context source="yes">
+
\startplaceformula
\setuplayout[textwidth=8cm]
 
Some example text, again, to show where the right and left margins of the text block are.
 
\tracemathtrue
 
\setupformulas[align=right,margin=standard]
 
 
\startformula c^2 = a^2 + b^2 \stopformula
 
\startformula c^2 = a^2 + b^2 \stopformula
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
\stopplaceformula
</context>
 
  
The distance between the formula and the number is only applied when the formula is left or right aligned.
+
\setupformulas[numberconversion=character]
  
<context source="yes">
+
\startplaceformula
\setuplayout[textwidth=8cm]
 
\tracemathtrue
 
\setupformulas[align=left,distance=2em]
 
 
\startformula c^2 = a^2 + b^2 \stopformula
 
\startformula c^2 = a^2 + b^2 \stopformula
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
\stopplaceformula
 
</context>
 
</context>
  
= Referencing formulae =
+
== Formula alignment ==
Equations can be referred to by simply adding a label to <cmd>placeformula</cmd> and using <cmd>ref</cmd> to create the reference:
 
  
<table width="100%" cols="2" cellpadding="5"><tr valign="top"><td width="50%">
+
Normally a formula is centered, but in case you want to align it left or right, you can set up formulas to behave
<texcode>
+
that way. Normally a formula will adapt its left indentation to the environment:
The famous result (and again) is given by
 
\placeformula[formulalabel]
 
\startformula
 
c^2 = a^2 + b^2.
 
\stopformula
 
And now we can refer to formula \ref[formulalabel].
 
</texcode>
 
</td><td>
 
This, when typeset, produces the following:
 
  
<context>
+
<context source="yes">  
\setuplayout[scale=0.8,width=8cm]
+
This is a bit of text for purpose of example.
The famous result (once more) is given by
+
\startitemize
\placeformula[formulalabel]
+
\item This is some other example text.
\startformula
+
      \startplaceformula \startformula c^2 = a^2 + b^2 \stopformula \stopplaceformula
c^2 = a^2 + b^2.
+
\item This is yet other example text.
\stopformula
+
\stopitemize
% number is added manually because otherwise only '??' appears:
+
This is a bit more text for other purpose of example.
And now we can refer to formula 1.
 
 
</context>
 
</context>
</td></tr></table>
 
  
By default, only the formula number appears as a reference. This can be changed by using <cmd>definereferenceformat</cmd>. For example, to create a command <code>\eqref</code> which shows the formula number in brackets, use
 
<texcode>
 
\definereferenceformat[eqref][left=(,right=)]
 
</texcode>
 
See [[References]] for more examples of <cmd>definereferenceformat</cmd>.
 
  
=Sub-Formula Numbering=
+
In the next examples we explicitly align formulas with {{cmd|setupformulas}}, to the left (<code>raggedleft</code>), center (<code>middle</code>, and right (<code>raggedright</code>):
 +
 
 +
<context source="yes">
 +
\setuppapersize[A5]
 +
\setuplayout[textwidth=8cm]
 +
\setupformulas[align=left]
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\setupformulas[align=middle]
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\setupformulas[align=right]
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
</context>
  
== Automatic Sub-Formula Numbering ==
+
With formula numbers the code is:
  
To use subformula numbering, you can use <cmd>startsubformulas</cmd>/<cmd>stopsubformulas</cmd>. For example
+
<context source="yes">
 +
\setuppapersize[A7,landscape]
 +
\setuplayout[textwidth=8cm]
 +
\starttext
 +
\setupformulas[align=flushleft]
 +
\startplaceformula
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
  
<table width="100%" cols="2"><tr valign="top"><td width="50%">
+
\setupformulas[align=middle]
<texcode>
+
\startplaceformula
Examples:
+
\startformula c^2 = a^2 + b^2 \stopformula
\startsubformulas[eq:1]
+
\stopplaceformula
\placeformula[eq:first]
 
\startformula
 
  c^2 = a^2 + b^2
 
\stopformula
 
  
\placeformula[eq:second]
+
\setupformulas[align=flushright]
\startformula
+
\startplaceformula
c^2 = a^2 + b^2
+
\startformula c^2 = a^2 + b^2 \stopformula
\stopformula
+
\stopplaceformula
\stopsubformulas
 
Formula (\in[eq:1]) states
 
the Pythagora's Theorem twice,
 
once in (\in[eq:first]) and
 
again in (\in[eq:second]).
 
</texcode>
 
</td><td>
 
<context>
 
\setuplayout[scale=0.8,width=8cm]
 
Examples:
 
\startsubformulas[eq:1]
 
\placeformula[eq:first]
 
\startformula
 
  c^2 = a^2 + b^2
 
\stopformula
 
  
\placeformula[eq:second]
+
\stoptext
\startformula
 
c^2 = a^2 + b^2
 
\stopformula
 
\stopsubformulas
 
Formula (\in[eq:1]) states the Pythagora's Theorem twice, once in (\in[eq:first]) and again in (\in[eq:second]).
 
 
</context>
 
</context>
</td></tr></table>
 
  
 +
When tracing is turned on ({{cmd|showboxes}}) you can visualize the bounding box of the formula,
  
== The Manual Method ==
+
<context source="yes" text="This, when typeset, produces the following:">
 
+
The famous result (once more) is given by
Sometimes, you need more fine grained control over numbering of subformulas. In that case one can make use of the optional agument of <cmd>placeformula</cmd> command and the related <cmd>placesubformula</cmd> commands which can be used to produce sub-formula numbering.  For example:
+
\showboxes
 
+
\startplaceformula
<table width="100%" cols="2"><tr valign="top"><td width="50%">
 
<texcode>
 
Examples:
 
\placeformula{a}
 
 
\startformula
 
\startformula
 
c^2 = a^2 + b^2
 
c^2 = a^2 + b^2
 
\stopformula
 
\stopformula
 +
\stopplaceformula
 +
</context>
  
\placesubformula{b}
+
As you can see, the dimensions are the natural ones, but if needed you can force a normalized line:
\startformula
 
c^2 = a^2 + b^2
 
\stopformula
 
</texcode>
 
</td><td>
 
<context>
 
\setuplayout[scale=0.8,width=8cm]
 
Examples:
 
\placeformula{a}
 
\startformula
 
c^2 = a^2 + b^2
 
\stopformula
 
  
\placesubformula{b}
+
<context source="yes"  text="This time we get a more spacy result.  [Ed. Note: For this example equation, there appears to be no visible change.]">
 +
The famous result (once more) is given by
 +
\showboxes
 +
\setupformulas[align=middle,strut=yes]
 +
\startplaceformula
 
\startformula
 
\startformula
 
c^2 = a^2 + b^2
 
c^2 = a^2 + b^2
 
\stopformula
 
\stopformula
 +
\stopplaceformula
 
</context>
 
</context>
</td></tr></table>
 
  
What's going on here is simpler than it might appear at first glance.  Both <cmd>placeformula</cmd> and <cmd>placesubformula</cmd> produce equation numbers with the optional tag added at the end; the sole difference is that the former increments the equation number first, while the latter does not (and thus can be used for the second and subsequent formulas that use the same formula number but presumably have different tags).
+
You can influence the placement of the whole box with the parameters <tt>leftmargin</tt> and <tt>rightmargin</tt>.
 +
 
 +
<context source="yes">
 +
The famous result (once more) is given by
 +
\showboxes
 +
\setupformulas[align=right,leftmargin=1em]
 +
\startplaceformula
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
  
This is sufficient for cases where the standard ConTeXt equation numbers suffice, and where only one equation number is needed per formula.  However, there are many cases where this is insufficient, and <cmd>placeformula</cmd> defines <cmd>formulanumber</cmd> and <cmd>subformulanumber</cmd> commands, which provide hooks to allow the use of ConTeXt-managed formula numbers with plain TeX equation numbering.  These, when used within a formula, simply return the formula number in properly formatted form, as can be seen in this simple example with plain TeX's <cmd>eqno</cmd>.  Note that the optional tag is inherited from <cmd>placeformula</cmd>.
+
\setupformulas[align=right,leftmargin=3em]
 +
\startplaceformula
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
  
<table width="100%" cols="2"><tr valign="top"><td width="50%">
+
\setupformulas[align=left,rightmargin=1em]
<texcode>
+
\startplaceformula
More examples:
+
\startformula c^2 = a^2 + b^2 \stopformula
\placeformula{c}
+
\stopplaceformula
\startformula
 
\let\doplaceformulanumber\empty
 
c^2 = a^2 + b^2  \eqno{\formulanumber}
 
\stopformula
 
</texcode>
 
</td><td>
 
<context>
 
\setuplayout[scale=0.8,width=8cm]
 
More examples:
 
\placeformula{c}
 
\startformula
 
\let\doplaceformulanumber\empty
 
c^2 = a^2 + b^2   \eqno{\formulanumber}
 
\stopformula
 
 
</context>
 
</context>
</td></tr></table>
 
  
In order for this to work properly, we need to turn off ConTeXt's automatic formula number placement; thus the <cmd>let</cmd> command to empty <cmd>doplaceformulanumber</cmd>, which must be placed <em>after</em> the start of the formula.  In many practical examples, however, this is not necessary; ConTeXt redefines <cmd>displaylines</cmd> and <cmd>eqalignno</cmd> to do this automatically.
+
You can also inherit the margin from the environment.
  
For more control over sub-formula numbering, <cmd>formulanumber</cmd> and <cmd>subformulanumber</cmd> have an optional argument parallel to that of <cmd>placeformula</cmd>, as demonstrated in this use of plain TeX's <cmd>eqalignno</cmd>, which places multiple equation numbers within one formula.
+
<context source="yes">
 +
Some example text, again, to show where the right and left margins of the text block are.
 +
\showboxes
  
<table width="100%" cols="2"><tr valign="top"><td width="50%">
+
\setupformulas[align=right]
<texcode>
+
\startplaceformula
\placeformula
+
\startformula c^2 = a^2 + b^2 \stopformula
\startformula
+
\stopplaceformula
\eqalignno{
 
  c^2 &= a^2 + b^2  &\formulanumber{a} \cr
 
  c &= \left(a^2 + b^2\right)^{\vfrac{1}{2}} &\subformulanumber{b}\cr
 
  a^2 + b^2 &= c^2  &\subformulanumber{c} \cr
 
  d^2 &= e^2        &\formulanumber\cr}
 
\stopformula
 
</texcode>
 
</td><td>
 
<context>
 
\setuplayout[scale=0.8,width=8cm]
 
\placeformula
 
\startformula
 
\eqalignno{
 
  c^2 &= a^2 + b^2 &\formulanumber{a} \cr
 
  c &= \left(a^2 + b^2\right)^{\vfrac{1}{2}} &\subformulanumber{b}\cr
 
  a^2 + b^2 &= c^2  &\subformulanumber{c} \cr
 
  d^2 &= e^2        &\formulanumber\cr}
 
\stopformula
 
</context>
 
</td></tr></table>
 
  
Note that both <cmd>formulanumber</cmd> and <cmd>subformulanumber</cmd> can be used within the same formula, and the formula number is incremented as expected.  Also, if an optional argument is specified in both <cmd>placefigure</cmd> and <cmd>formulanumber</cmd>, the latter takes precedence.
+
\setupformulas[align=right,margin=standard]
 +
\startplaceformula
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
  
<table width="100%" cols="2"><tr valign="top"><td width="50%">
 
<texcode>
 
More examples for left-located equation number:
 
\setupformulas[location=left]
 
\placeformula{d}
 
\startformula
 
\let\doplaceformulanumber\empty
 
c^2 = a^2 + b^2  \leqno{\formulanumber}
 
\stopformula
 
and
 
\placeformula
 
\startformula
 
\leqalignno{c^2 &= a^2 + b^2  &\formulanumber{a} \cr
 
          a^2 + b^2 &= c^2  &\subformulanumber{b} \cr
 
          d^2 &= e^2        &\formulanumber\cr}
 
\stopformula
 
</texcode>
 
</td><td>
 
<context>
 
\setuplayout[scale=0.8,width=8cm]
 
More examples for left-located equation no.:
 
\setupformulas[location=left]
 
\placeformula{d}
 
\startformula
 
\let\doplaceformulanumber\empty
 
c^2 = a^2 + b^2  \leqno{\formulanumber}
 
\stopformula
 
and
 
\placeformula
 
\startformula
 
\leqalignno{c^2 &= a^2 + b^2  &\formulanumber{a} \cr
 
          a^2 + b^2 &= c^2  &\subformulanumber{b} \cr
 
          d^2 &= e^2        &\formulanumber\cr}
 
\stopformula
 
 
</context>
 
</context>
</td></tr></table>
 
  
-- 23:46, 15 Aug 2005 (CEST) Prinse Wang
+
The distance between the formula and the number is only applied when the formula is left or right aligned.
  
=List of Formulas=
+
<context source="yes">
 +
Some example text, again, to show where the right and left margins of the text block are.
 +
\showboxes
 +
\setupformulas[align=left,distance=2em]
 +
\startplaceformula
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
 +
</context>
  
You can have a list of the formulas contained in a document by using <cmd>placenamedformula</cmd> instead of <cmd>placeformula</cmd>. Only the formulas written with <cmd>placenamedformula</cmd> are not put in the list, so that you can control precisely the content of the list.
+
== Shaded background for part of a displayed equation ==
 +
(see also [[Framed]])
  
<cmd>placenamedformula</cmd> takes as first parameter the name of the formula put in the list. The other <cmd>placeformula</cmd> features are still available. The list can be formatted like any other list.
+
One may define a specific framed command, for example <tt>coloredmath<tt>, with {{cmd|definemathframed}}.
  
Example:
+
<context source="yes">
<texcode>
+
\setuppapersize[A7,landscape]
\subsubject{List of Formulas}
+
\definemathframed[coloredmath]
\placelist[formula][criterium=text,alternative=c]
+
[
+
frame=off,
\subsubject{Formulas}
+
location=mathematics,
\placenamedformula[one]{First listed Formula}
+
background=color,
\startformula a = 1 \stopformula \endgraf
+
backgroundcolor=lightcyan,
 
+
backgroundoffset=1pt
\placeformula
+
]
\startformula a = 2 \stopformula \endgraf
+
\starttext
 
+
Since for $|x| < 1$ we have
\placenamedformula{Second listed Formula}{b}
+
\startformula
\startformula a = 3 \stopformula \endgraf
+
\log(1+x) = \coloredmath{x- \displaystyle{x^2\over2}} + {x^3 \over 3} + \cdots
</texcode>
+
\stopformula
 
+
we may write $\log(1+x) = x + O(x^2)$.
Gives:
+
\stoptext
 
+
</context>
<context>
 
\setuplayout[scale=0.8,width=13cm]
 
\subsubject{List of Formulas}
 
\placelist[formula][criterium=text,alternative=c]
 
 
\subsubject{Formulas}
 
\placenamedformula[one]{First listed Formula}
 
\startformula a = 1 \stopformula \endgraf
 
 
 
\placeformula
 
\startformula a = 2 \stopformula \endgraf
 
  
\placenamedformula{Second listed Formula}{b}
+
[[Category:Math]]
\startformula a = 3 \stopformula \endgraf
 
</context>
 

Latest revision as of 08:46, 12 May 2024

First \startformula

Display math is enclosed in a \startformula / \stopformula pair. Thus

The famous result (once more) is given by
\startformula
c^2 = a^2 + b^2
\stopformula

This, when typeset, produces the following:

 

Numbering Formulae \startformula

ConTeXt provides an easy way to number the display maths equations. Simply, insert the \startformula in the \startplaceformula environment, and you will get numbered equations.

The famous result (once more) is given by
\startplaceformula
\startformula 
c^2 = a^2 + b^2 
\stopformula
\stopplaceformula

This, when typeset, produces the following:

 

The \startplaceformula command is optional, and produces the equation number; leaving it off produces an unnumbered equation.

Referencing formulae \in and \at

Equations can be referred to by simply adding a label to \startplaceformula and using command for referencing :

  • \ref (note that it expects two arguments, therefore you need the brackets twice),
  • \in (a kind of \ref[number][TheRef],
  • \at (a kind of \ref[page][TheRef].
\setupinteraction[state=start]
The famous result (once more) is given by

\startplaceformula[eq:myref]                  %   <===== here
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

And now we can refer to its number by several ways,
\startitemize
\item with \tex{in} : formula \in[eq:myref] or \in{formula}{}[eq:myref] 
\item with \tex{at} : formula \in[eq:myref] \at{page}[eq:myref]
\item with \tex{ref} : formula \ref[number] [eq:myref] page \ref[number] [eq:myref]
\stopitemize

This, when typeset, produces the following:

 

By default, only the formula number appears as a reference. This can be changed by using \definereferenceformat. For example, to create a command \eqref which shows the formula number in brackets, use

\setupinteraction[state=start]

\definereferenceformat[eqref][left=(,right=)]

The famous result (once more) is given by
\startplaceformula[eq:myref]
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

And now we can refer to its number by several ways,
\startitemize
\item with \tex{in} : formula \in[eq:myref] or \in{formula}{}[eq:myref],
\item with \tex{at} : formula \in[eq:myref] \at{page}[eq:myref],
\item with \tex{ref} : formula \ref[number] [eq:myref] page \ref[number] [eq:myref],
\item with \tex{eqref} : formula \eqref[eq:myref].
\stopitemize

This, when typeset, produces the following:

 


Sub-Formulae with \startsubformulas and \startalign

To use subformula numbering, you have to use \startsubformulas/\stopsubformulas and \startalign. For example

The famous result (once more) is given by

Examples:
\definereferenceformat[eqref][left=(,right=)]

\startsubformulas[eq:total]
\placeformula
\startformula
\startalign
\NC a^2 + b^2 \NC = c^2 \NR[eq:first]
\NC a^2 + b^2 \NC = c^2 \NR[eq:second]
\stopalign
\stopformula
\stopsubformulas

Formula \eqref[eq:total] states the Pythagora's Theorem twice,
once in \eqref[eq:first] and again in \eqref[eq:second].

You can compare without \tex{startsubformulas}:
\placeformula
\startformula
\startalign
\NC a^2 + b^2 \NC = c^2 \NR[eq2:first]
\NC a^2 + b^2 \NC = c^2 \NR[eq2:second]
\stopalign
\stopformula

We now have references for \eqref[eq2:first] and \eqref[eq2:second].

This, when typeset, produces the following:

 

List of Formulae with \placelist

Since MKIV there are two types of formulas: numbered and named. Numbered formulas have the number as a reference and caption next to the formula and are listed only with the number and pagenumber in the list of formulas. Named formulas use the name as the caption instead of a number and are referenced with the name and pagenumber in the list of formulas.

2 examples:

% no reference        ==> no entry in the list of formulas
\startplaceformula
\startformula e=mc^2 \stopformula 
\stopplaceformula

% a reference         ==> entry in the list of formulas
\startplaceformula
  [reference=formula:mass1]              %   <====== here
\startformula e=mc^2 \stopformula 
\stopplaceformula

This is the list:

\placelist[formula][criterium=all,alternative=c]

 

\startplaceformula comes with the following key options: bookmark, list, reference, suffix, title. The title option changes the formula to a named formula (no number).

3 examples:

% a reference + list  ==> entry with number + additional text in the list
\startplaceformula
  [reference=formula:mass2,
   list={Mass-energy equivalence from list option}]
\startformula e=mc^2 \stopformula
\stopplaceformula

% a reference + title  ==> entry with title text, no number
\startplaceformula
  [title={Mass-energy equivalence},     %   <====== here
   reference=formula:mass3]
\startformula e=mc^2 \stopformula
\stopplaceformula

% a reference + title + list ==> entry with list text, no number
\startplaceformula
  [title={Mass-energy equivalence},     %   <====== here
   reference=formula:mass4,
   list={Mass-energy equivalence from list option}]
\startformula e=mc^2 \stopformula
\stopplaceformula

This is the list:

\placelist[formula][criterium=all,alternative=c]

 

Formating with \setupformulas

Format of numbers

You can use \setupformulas to change the format of numbers. For example to get bold numbers inside square brackets use

The famous result (once more) is given by
\setupformulas
  [left={[},
   right={]},
   numberstyle=bold]

\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

This, when typeset, produces the following:

 

To get equations also numbered by section, use the command \setupnumbering \setupnumbering[way=bysection] in the preamble of your document. This means that the section number preceeds the formula number. Add also \setupnumber \setupnumber[formula][way=bysection] to reset with each new section.

The famous result (once more) is given by
\setupformulas
  [left={[},
   right={]},
   numberstyle=bold]

\setupnumbering        [way=bysection] % to add the section number to the formula number
\setupnumber [formula] [way=bysection] % to reset with each new section

\startsection[title=First title]
\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula
\stopsection

\startsection[title=Second title]
\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula
\stopsection

This, when typeset, produces the following:

 

To get alphabets instead of numbers, use again \setupformulas

The famous result (once more) is given by
\setupformulas[numberconversion=Character]

\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

\setupformulas[numberconversion=character]

\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

This, when typeset, produces the following:

 

Formula alignment

Normally a formula is centered, but in case you want to align it left or right, you can set up formulas to behave that way. Normally a formula will adapt its left indentation to the environment:

 
This is a bit of text for purpose of example.
\startitemize
\item This is some other example text.
      \startplaceformula \startformula c^2 = a^2 + b^2 \stopformula \stopplaceformula
\item This is yet other example text.
\stopitemize
This is a bit more text for other purpose of example.

 


In the next examples we explicitly align formulas with \setupformulas, to the left (raggedleft), center (middle, and right (raggedright):

\setuppapersize[A5]
\setuplayout[textwidth=8cm]
\setupformulas[align=left]
\startformula c^2 = a^2 + b^2 \stopformula
\setupformulas[align=middle]
\startformula c^2 = a^2 + b^2 \stopformula
\setupformulas[align=right]
\startformula c^2 = a^2 + b^2 \stopformula

 

With formula numbers the code is:

\setuppapersize[A7,landscape]
\setuplayout[textwidth=8cm]
\starttext
\setupformulas[align=flushleft]
\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

\setupformulas[align=middle]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula 
\stopplaceformula 

\setupformulas[align=flushright]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula 
\stopplaceformula

\stoptext

 

When tracing is turned on (\showboxes) you can visualize the bounding box of the formula,

The famous result (once more) is given by
\showboxes
\startplaceformula 
\startformula
c^2 = a^2 + b^2
\stopformula
\stopplaceformula

This, when typeset, produces the following:

 

As you can see, the dimensions are the natural ones, but if needed you can force a normalized line:

The famous result (once more) is given by
\showboxes
\setupformulas[align=middle,strut=yes]
\startplaceformula 
\startformula
c^2 = a^2 + b^2
\stopformula
\stopplaceformula

This time we get a more spacy result. [Ed. Note: For this example equation, there appears to be no visible change.]

 

You can influence the placement of the whole box with the parameters leftmargin and rightmargin.

The famous result (once more) is given by
\showboxes
\setupformulas[align=right,leftmargin=1em]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

\setupformulas[align=right,leftmargin=3em]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

\setupformulas[align=left,rightmargin=1em]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

 

You can also inherit the margin from the environment.

Some example text, again, to show where the right and left margins of the text block are.
\showboxes

\setupformulas[align=right]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

\setupformulas[align=right,margin=standard]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

 

The distance between the formula and the number is only applied when the formula is left or right aligned.

Some example text, again, to show where the right and left margins of the text block are.
\showboxes
\setupformulas[align=left,distance=2em]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

 

Shaded background for part of a displayed equation

(see also Framed)

One may define a specific framed command, for example coloredmath, with \definemathframed.

\setuppapersize[A7,landscape]
\definemathframed[coloredmath]
	[
	frame=off,
	location=mathematics,
	background=color,
	backgroundcolor=lightcyan,
	backgroundoffset=1pt
	]
\starttext
Since for $|x| < 1$ we have
\startformula
\log(1+x) = \coloredmath{x- \displaystyle{x^2\over2}} + {x^3 \over 3} + \cdots
\stopformula
we may write $\log(1+x) = x + O(x^2)$.
\stoptext