Difference between revisions of "Math/Display"

From Wiki
Jump to navigation Jump to search
(add {{cmd|startformulas}})
 
(67 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Display math =
+
= First {{cmd|startformula}} =
  
 
Display math is enclosed in a {{cmd|startformula}} / {{cmd|stopformula}} pair.
 
Display math is enclosed in a {{cmd|startformula}} / {{cmd|stopformula}} pair.
Line 11: Line 11:
 
</context>
 
</context>
  
== Numbering Formulae ==
+
= Numbering Formulae {{cmd|startformula}} =
  
 
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 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.  
Line 18: Line 18:
 
The famous result (once more) is given by
 
The famous result (once more) is given by
 
\startplaceformula
 
\startplaceformula
\startformula
+
\startformula  
c^2 = a^2 + b^2
+
c^2 = a^2 + b^2  
 
\stopformula
 
\stopformula
 
\stopplaceformula
 
\stopplaceformula
 
</context>
 
</context>
  
The {{cmd|placeformula}} command is optional, and produces the equation number; leaving it off produces an unnumbered equation.
+
The {{cmd|startplaceformula}} command is optional, and produces the equation number; leaving it off produces an unnumbered equation.
  
== Changing format of numbers ==
+
= Referencing formulae {{cmd|in}} and {{cmd|at}}=
You can use {{cmd|setupformulas}} to change the format of numbers. For example to get bold numbers inside square brackets use
+
 
 +
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:">
 
<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
\setupformulas[left={[},right={]},numberstyle=bold]
+
 
\startplaceformula
+
\startplaceformula[eq:myref]                  %  <===== here
\startformula
+
\startformula c^2 = a^2 + b^2 \stopformula
c^2 = a^2 + b^2
 
\stopformula
 
 
\stopplaceformula
 
\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>
 
</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]
  
To get equations also numbered by section, add the command:
+
\definereferenceformat[eqref][left=(,right=)]
<texcode>
 
\setupnumber[formula][way=bysection]  
 
</texcode>
 
To the start of your document.
 
  
To get alphabets instead of numbers, use
+
The famous result (once more) is given by
<table width="100%" cols="2" cellpadding="5"><tr valign="top"><td width="50%">
+
\startplaceformula[eq:myref]
<texcode>
+
\startformula c^2 = a^2 + b^2 \stopformula
\setupformulas[numberconversion=Character]
+
\stopplaceformula
</texcode>
 
</td>
 
<td>which gives
 
<context>
 
\setuppapersize[A5]
 
\setuplayout[width=8cm]
 
\setupformulas[numberconversion=Character]
 
\placeformula
 
\startformula
 
c^2 = a^2 + b^2
 
\stopformula
 
</context>
 
</td></tr></table>
 
  
== Changing Formula alignment ==
+
And now we can refer to its number by several ways,
 
 
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:
 
 
 
<context>
 
\setuppapersize[A5]
 
\setuppapersize[A5]
 
\setuplayout[textwidth=8cm]
 
This is a bit of text for purpose of example.\epar
 
 
\startitemize
 
\startitemize
\item This is some other example text.\epar
+
\item with \tex{in} : formula \in[eq:myref] or \in{formula}{}[eq:myref],
      \placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
\item with \tex{at} : formula \in[eq:myref] \at{page}[eq:myref],
\item This is yet other example text.\epar
+
\item with \tex{ref} : formula \ref[number] [eq:myref] page \ref[number] [eq:myref],
 +
\item with \tex{eqref} : formula \eqref[eq:myref].
 
\stopitemize
 
\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}}), center and right ({{cmd|raggedright}}):
 
  
<context source="yes" text="Or in print:">
+
= Sub-Formulae with {{cmd|startsubformulas}} and {{cmd|startalign}} =
\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>
 
  
With formula numbers the code is:
+
To use subformula numbering, you have to use {{cmd|startsubformulas}}/{{cmd|stopsubformulas}} and {{cmd|startalign}}. For example
  
<context source="yes" text="And the formulas look like:">
+
<context source="yes" text="This, when typeset, produces the following:">
\setuppapersize[A5]
+
The famous result (once more) is given by
\setuplayout[textwidth=8cm]
 
\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>
 
  
When tracing is turned on ({{cmd|tracemathtrue}}) you can visualize the bounding box of the formula,
+
Examples:
 +
\definereferenceformat[eqref][left=(,right=)]
  
<context>
+
\startsubformulas[eq:total]
\setuppapersize[A5]
+
\startplaceformula
\setuplayout[textwidth=8cm]
+
\startformula
\tracemathtrue
+
\startalign
\setupformulas[align=left]
+
\NC a^2 + b^2 \NC = c^2 \NR[eq:first]
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
\NC a^2 + b^2 \NC = c^2 \NR[eq:second]
\setupformulas[align=middle]
+
\stopalign
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
\stopformula
\setupformulas[align=right]
+
\stopsubformulas
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
\stopplaceformula
</context>
 
  
As you can see, the dimensions are the natural ones, but if needed you can force a normalized line:
+
Formula \eqref[eq:total] states the Pythagora's Theorem twice,
 +
once in \eqref[eq:first] and again in \eqref[eq:second].
  
<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.]">
+
You can compare without \tex{startsubformulas}:
\setuppapersize[A5]
+
\startplaceformula
\setuplayout[textwidth=8cm]
+
\startformula
\setupformulas[align=middle,strut=yes]
+
\startalign
\tracemathtrue
+
\NC a^2 + b^2 \NC = c^2 \NR[eq2:first]
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
+
\NC a^2 + b^2 \NC = c^2 \NR[eq2:second]
</context>
+
\stopalign
 
+
\stopformula
We will now show a couple of more settings and combinations of settings. In centered formulas, the number takes no space
+
\stopplaceformula
  
<context source="yes">
+
We now have references for \eqref[eq2:first] and \eqref[eq2:second].
\setuppapersize[A5]
 
\setuplayout[textwidth=8cm]
 
\tracemathtrue
 
\setupformulas[align=middle]
 
\startformula c^2 = a^2 + b^2 \stopformula
 
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
 
 
</context>
 
</context>
  
You can influence the placement of the whole box with the parameters <tt>leftmargin</tt> and <tt>rightmargin</tt>.
 
  
<context source="yes">
+
= Combination of formulae with {{cmd|startformulas}} =
\setuppapersize[A5]
 
\setuplayout[textwidth=8cm]
 
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
 
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
 
  
\setupformulas[align=left,rightmargin=1em]
+
To use subformula numbering, you have to use {{cmd|startsubformulas}}/{{cmd|stopsubformulas}} and {{cmd|startalign}}. For example
\startformula c^2 = a^2 + b^2 \stopformula
 
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
 
</context>
 
  
You can also inherit the margin from the environment.
+
<context source="yes" text="This, when typeset, produces the following:">
 +
\definereferenceformat[eqref][left=(,right=)]
  
<context source="yes">
+
The famous result (once more) is given by
\setuppapersize[A5]
+
\startsubformulas[eq:total]
\setuplayout[textwidth=8cm]
+
\startplaceformula
Some example text, again, to show where the right and left margins of the text block are.
+
\startformulas
\tracemathtrue
 
\setupformulas[align=right,margin=standard]
 
\startformula c^2 = a^2 + b^2 \stopformula
 
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
 
</context>
 
  
The distance between the formula and the number is only applied when the formula is left or right aligned.
+
\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
  
<context source="yes">
 
\setuppapersize[A5]
 
\setuplayout[textwidth=8cm]
 
\tracemathtrue
 
\setupformulas[align=left,distance=2em]
 
\startformula c^2 = a^2 + b^2 \stopformula
 
\placeformula \startformula c^2 = a^2 + b^2 \stopformula
 
</context>
 
 
= Referencing formulae =
 
Equations can be referred to by simply adding a label to {{cmd|placeformula}} and using {{cmd|ref}} to create the reference:
 
  
<table width="100%" cols="2" cellpadding="5"><tr valign="top"><td width="50%">
 
<texcode>
 
The famous result (and again) is given by
 
\placeformula[formulalabel]
 
 
\startformula
 
\startformula
c^2 = a^2 + b^2.
+
\startalign
 +
\NC a^2 + b^2 \NC = c^2 \NR[eq:third]
 +
\NC a^2 + b^2 \NC = c^2 \NR[eq:forth]
 +
\stopalign
 
\stopformula
 
\stopformula
And now we can refer to formula \ref[][formulalabel].
 
</texcode>
 
</td><td>
 
This, when typeset, produces the following:
 
  
<context>
+
\stopformulas
\setuppapersize[A5]
+
\stopplaceformula
\setuplayout[width=8cm]
+
\stopsubformulas
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].
+
We now have references for \eqref[eq:forth] for example.
 
</context>
 
</context>
</td></tr></table>
 
  
Note, that {{cmd|ref}} expects two arguments, therefore you need the brackets twice.
+
= List of Formulae with {{cmd|placelist}} =
  
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
+
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.
<texcode>
 
\definereferenceformat[eqref][left=(,right=)]
 
</texcode>
 
See [[References]] for more examples of {{cmd|definereferenceformat}}.
 
  
=Sub-Formula Numbering=
+
<context source=yes>
 +
2 examples:
  
== Automatic Sub-Formula Numbering ==
+
% no reference        ==> no entry in the list of formulas
 +
\startplaceformula
 +
\startformula e=mc^2 \stopformula
 +
\stopplaceformula
  
To use subformula numbering, you can use {{cmd|startsubformulas}}/{{cmd|stopsubformulas}}. For example
+
% a reference        ==> entry in the list of formulas
 +
\startplaceformula
 +
  [reference=formula:mass1]              %  <====== here
 +
\startformula e=mc^2 \stopformula
 +
\stopplaceformula
  
<table width="100%" cols="2"><tr valign="top"><td width="50%">
+
This is the list:
<texcode>
 
Examples:
 
\startsubformulas[eq:1]
 
\placeformula[eq:first]
 
\startformula
 
  c^2 = a^2 + b^2
 
\stopformula
 
  
\placeformula[eq:second]
+
\placelist[formula][criterium=all,alternative=c]
\startformula
+
</context>
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]).
 
</texcode>
 
</td><td>
 
<context>
 
\setuppapersize[A5]
 
\setuplayout[width=8cm]
 
Examples:
 
\startsubformulas[eq:1]
 
\placeformula[eq:first]
 
\startformula
 
  c^2 = a^2 + b^2
 
\stopformula
 
  
\placeformula[eq:second]
+
{{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).
\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>
 
</td></tr></table>
 
  
 +
<context source=yes>
 +
3 examples:
  
== The Manual Method ==
+
% 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
  
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}} command and the related {{cmd|placesubformula}} commands which can be used to produce sub-formula numbering.  For example:
+
% a reference + title  ==> entry with title text, no number
 +
\startplaceformula
 +
  [title={Mass-energy equivalence},    %  <====== here
 +
  reference=formula:mass3]
 +
\startformula e=mc^2 \stopformula
 +
\stopplaceformula
  
<table width="100%" cols="2"><tr valign="top"><td width="50%">
+
% a reference + title + list ==> entry with list text, no number
<texcode>
+
\startplaceformula
Examples:
+
  [title={Mass-energy equivalence},    %   <====== here
\placeformula{a}
+
  reference=formula:mass4,
\startformula
+
  list={Mass-energy equivalence from list option}]
c^2 = a^2 + b^2
+
\startformula e=mc^2 \stopformula
\stopformula
+
\stopplaceformula
  
\placesubformula{b}
+
This is the list:
\startformula
 
c^2 = a^2 + b^2
 
\stopformula
 
</texcode>
 
</td><td>
 
<context>
 
\setuppapersize[A5]
 
\setuplayout[width=8cm]
 
Examples:
 
\placeformula{a}
 
\startformula
 
c^2 = a^2 + b^2
 
\stopformula
 
  
\placesubformula{b}
+
\placelist[formula][criterium=all,alternative=c]
\startformula
 
c^2 = a^2 + b^2
 
\stopformula
 
 
</context>
 
</context>
</td></tr></table>
 
  
What's going on here is simpler than it might appear at first glance.  Both {{cmd|placeformula}} and {{cmd|placesubformula}} 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).
+
= Formating with {{cmd|setupformulas}} =
  
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}} defines {{cmd|formulanumber}} and {{cmd|subformulanumber}} 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}}.  Note that the optional tag is inherited from {{cmd|placeformula}}.
+
== Format of numbers ==
 +
You can use {{cmd|setupformulas}} to change the format of numbers. For example to get bold numbers inside square brackets use
  
<table width="100%" cols="2"><tr valign="top"><td width="50%">
+
<context source="yes" text="This, when typeset, produces the following:">
<texcode>
+
The famous result (once more) is given by
More examples:
+
\setupformulas
\placeformula{c}
+
  [left={[},
\startformula
+
  right={]},
\let\doplaceformulanumber\empty
+
  numberstyle=bold]
c^2 = a^2 + b^2  \eqno{\formulanumber}
+
 
\stopformula
+
\startplaceformula
</texcode>
+
\startformula c^2 = a^2 + b^2 \stopformula
</td><td>
+
\stopplaceformula
<context>
 
\setuppapersize[A5]
 
\setuplayout[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}} command to empty {{cmd|doplaceformulanumber}}, 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}} and {{cmd|eqalignno}} to do this automatically.
+
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.
  
For more control over sub-formula numbering, {{cmd|formulanumber}} and {{cmd|subformulanumber}} have an optional argument parallel to that of {{cmd|placeformula}}, as demonstrated in this use of plain TeX's {{cmd|eqalignno}}, which places multiple equation numbers within one formula.
+
<context source="yes" text="This, when typeset, produces the following:">
 +
The famous result (once more) is given by
 +
\setupformulas
 +
  [left={[},
 +
  right={]},
 +
  numberstyle=bold]
  
<table width="100%" cols="2"><tr valign="top"><td width="50%">
+
\setupnumbering        [way=bysection] % to add the section number to the formula number
<texcode>
+
\setupnumber [formula] [way=bysection] % to reset with each new section
\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
 
</texcode>
 
</td><td>
 
<context>
 
\setuppapersize[A5]
 
\setuplayout[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}} and {{cmd|subformulanumber}} 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}} and {{cmd|formulanumber}}, the latter takes precedence.
+
\startsection[title=First title]
 +
\startplaceformula
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
 +
\stopsection
  
<table width="100%" cols="2"><tr valign="top"><td width="50%">
+
\startsection[title=Second title]
<texcode>
+
\startplaceformula
More examples for left-located equation number:
+
\startformula c^2 = a^2 + b^2 \stopformula
\setupformulas[location=left]
+
\stopplaceformula
\placeformula{d}
+
\stopsection
\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>
 
\setuppapersize[A5]
 
\setuplayout[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
+
To get alphabets instead of numbers, use again {{cmd|setupformulas}}
  
=List of Formulas=
+
<context source="yes" text="This, when typeset, produces the following:">
 +
The famous result (once more) is given by
 +
\setupformulas[numberconversion=Character]
  
You can have a list of the formulas contained in a document by using {{cmd|placenamedformula}} instead of {{cmd|placeformula}}. Only the formulas written with {{cmd|placenamedformula}} are not put in the list, so that you can control precisely the content of the list.
+
\startplaceformula
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
  
{{cmd|placenamedformula}} takes as first parameter the name of the formula put in the list. The other {{cmd|placeformula}} features are still available. The list can be formatted like any other list.
+
\setupformulas[numberconversion=character]
  
Example:
+
\startplaceformula
<texcode>
+
\startformula c^2 = a^2 + b^2 \stopformula
\subsubject{List of Formulas}
+
\stopplaceformula
\placelist[formula][criterium=text,alternative=c]
+
</context>
 
\subsubject{Formulas}
 
\placenamedformula[one]{First listed Formula}
 
\startformula a = 1 \stopformula \endgraf
 
  
\placeformula
+
== Formula alignment ==
\startformula a = 2 \stopformula \endgraf
 
  
\placenamedformula{Second listed Formula}{b}
+
Normally a formula is centered, but in case you want to align it left or right, you can set up formulas to behave
\startformula a = 3 \stopformula \endgraf
+
that way. Normally a formula will adapt its left indentation to the environment:
</texcode>
 
  
Gives:
+
<context source="yes">
 +
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.
 +
</context>
  
<context>
 
\setuppapersize[A5]
 
\setuplayout[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
+
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>):
\startformula a = 2 \stopformula \endgraf
 
  
\placenamedformula{Second listed Formula}{b}
+
<context source="yes">
\startformula a = 3 \stopformula \endgraf
+
\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>
 
</context>
  
== List of Formulas with MKIV/LMTX ==
+
With formula numbers the code is:
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.
 
  
<context source=yes>
+
<context source="yes">
% equivalent to \placenamedformula[formula:mass]{Mass-energy equivalence}
+
\setuppapersize[A7,landscape]
\startplaceformula[title={Mass-energy equivalence},reference=formula:mass] \startformula e=mc^2 \stopformula\stopplaceformula
+
\setuplayout[textwidth=8cm]
 +
\starttext
 +
\setupformulas[align=flushleft]
 +
\startplaceformula
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula  
  
% equivalent to \placeformula[formula:mass] (note: placeformula is listed in the list of formulas if it has a reference)
+
\setupformulas[align=middle]
\startplaceformula[reference=formula:mass] \startformula e=mc^2 \stopformula \stopplaceformula
+
\startplaceformula  
 +
\startformula c^2 = a^2 + b^2 \stopformula  
 +
\stopplaceformula  
  
% equivalent to \placeformula (with no entry in the list of formulas)
+
\setupformulas[align=flushright]
\startplaceformula \startformula e=mc^2 \stopformula \stopplaceformula
+
\startplaceformula  
 +
\startformula c^2 = a^2 + b^2 \stopformula  
 +
\stopplaceformula
  
\placelist[formula][criterium=text,alternative=c]
+
\stoptext
 
</context>
 
</context>
  
{{cmd|startplaceformula}} comes with the following key options: bookmark, list, reference, suffix, title.
+
When tracing is turned on ({{cmd|showboxes}}) you can visualize the bounding box of the formula,
  
To name a numbered formula in the list of formulas, one can use the list option:
+
<context source="yes" text="This, when typeset, produces the following:">
<context source=yes>
+
The famous result (once more) is given by
% named formula
+
\showboxes
\startplaceformula[reference=formula:mass,title={Mass-energy equivalence}] \startformula e=mc^2 \stopformula
+
\startplaceformula  
 +
\startformula
 +
c^2 = a^2 + b^2
 +
\stopformula
 
\stopplaceformula
 
\stopplaceformula
 +
</context>
  
% numbered formula with list text, note without a reference option the formula is just a placeformula that is not listed in the list of formulas -- needs ConTeXt latest version 2021-03-21 or later
+
As you can see, the dimensions are the natural ones, but if needed you can force a normalized line:
\startplaceformula[reference=formula:mass,list={Mass-energy equivalence}] \startformula e=mc^2 \stopformula \stopplaceformula
 
  
\placelist[formula][criterium=text,alternative=c]
+
<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
 +
c^2 = a^2 + b^2
 +
\stopformula
 +
\stopplaceformula
 
</context>
 
</context>
  
Note: The title option changes the formula to a named formula and will use that name in the list of formulas regardless of the list option.
+
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
 +
 
 +
\setupformulas[align=right,leftmargin=3em]
 +
\startplaceformula
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
  
= Shaded background for part of a displayed equation =
+
\setupformulas[align=left,rightmargin=1em]
(see also [[Framed]])
+
\startplaceformula
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
 +
</context>
  
To highlight part of a formula, you can give it a gray background using {{cmd|mframed}}: the following is the code you can use in mkii (see below what one has to do in mkiv):
+
You can also inherit the margin from the environment.
  
 
<context source="yes">
 
<context source="yes">
\setuppapersize[A5]
+
Some example text, again, to show where the right and left margins of the text block are.
\setupcolors[state=start]
+
\showboxes
\def\graymath{\mframed[frame=off,
+
 
    background=color,
+
\setupformulas[align=right]
    backgroundcolor=gray,
+
\startplaceformula
    backgroundoffset=3pt]}
+
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
 +
 
 +
\setupformulas[align=right,margin=standard]
 +
\startplaceformula
 +
\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.
  
\startformula
+
<context source="yes">
  \ln (1+x) =\, \graymath{x - {x^2\over2}} \,+ {x^3\over3}-\cdots.
+
Some example text, again, to show where the right and left margins of the text block are.
\stopformula
+
\showboxes
 +
\setupformulas[align=left,distance=2em]
 +
\startplaceformula
 +
\startformula c^2 = a^2 + b^2 \stopformula
 +
\stopplaceformula
 
</context>
 
</context>
  
In mkiv the code is slightly different: one may define {{cmd|graymath}} directly using {{cmd|definemathframed}}
+
== Shaded background for part of a displayed equation ==
 +
(see also [[Framed]])
  
<texcode>
+
One may define a specific framed command, for example <tt>coloredmath<tt>, with {{cmd|definemathframed}}.
\setuppapersize[A5]
+
 
\definemathframed[graymath]
+
<context source="yes">
 +
\setuppapersize[A7,landscape]
 +
\definemathframed[coloredmath]
 
[
 
[
 
frame=off,
 
frame=off,
 
location=mathematics,
 
location=mathematics,
 
background=color,
 
background=color,
backgroundcolor=lightgray,
+
backgroundcolor=lightcyan,
backgroundoffset=2pt
+
backgroundoffset=1pt
 
]
 
]
 
\starttext
 
\starttext
 
Since for $|x| < 1$ we have
 
Since for $|x| < 1$ we have
 
\startformula
 
\startformula
\log(1+x) = \graymath{x- \displaystyle{x^2\over2}} + {x^3 \over 3} + \cdots
+
\log(1+x) = \coloredmath{x- \displaystyle{x^2\over2}} + {x^3 \over 3} + \cdots
 
\stopformula
 
\stopformula
 
we may write $\log(1+x) = x + O(x^2)$.
 
we may write $\log(1+x) = x + O(x^2)$.
 
\stoptext
 
\stoptext
</texcode>
 
 
The result is shown below (possibly the framed part of the formula is not aligned correctly with the remainder of the formula because the mkiv engine on Context Garden is not up to date…).
 
 
<context mode=mkiv>
 
\setuppapersize[A5]
 
\definemathframed[graymath]
 
[
 
frame=off,
 
location=mathematics,
 
background=color,
 
backgroundcolor=lightgray,
 
backgroundoffset=2pt
 
]
 
 
Since for $|x| < 1$ we have
 
\startformula
 
\log(1+x) = \graymath{x- \displaystyle{x^2\over2}} + {x^3 \over 3} + \cdots
 
\stopformula
 
we may write $\log(1+x) = x + O(x^2)$.
 
 
</context>
 
</context>
  
 
[[Category:Math]]
 
[[Category:Math]]

Latest revision as of 13:14, 18 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]
\startplaceformula
\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
\stopplaceformula

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}:
\startplaceformula
\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
\stopplaceformula

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

This, when typeset, produces the following:


Combination of formulae with \startformulas

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

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

The famous result (once more) is given by
\startsubformulas[eq:total]
\startplaceformula
\startformulas

\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


\startformula
\startalign
\NC a^2 + b^2 \NC = c^2 \NR[eq:third]
\NC a^2 + b^2 \NC = c^2 \NR[eq:forth]
\stopalign
\stopformula

\stopformulas
\stopplaceformula
\stopsubformulas

We now have references for \eqref[eq:forth] for example.

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