Difference between revisions of "Math/Display"
Line 507:  Line 507:  
In mkiv the code is slightly different: one may define {{cmdgraymath}} directly using {{cmddefinemathframed}}  In mkiv the code is slightly different: one may define {{cmdgraymath}} directly using {{cmddefinemathframed}}  
−  <  +  <texcode> 
−  
\setuppapersize[A5]  \setuppapersize[A5]  
\definemathframed[graymath]  \definemathframed[graymath]  
Line 518:  Line 517:  
backgroundoffset=2pt  backgroundoffset=2pt  
]  ]  
−  +  \starttext  
Since for $x < 1$ we have  Since for $x < 1$ we have  
\startformula  \startformula  
Line 525:  Line 524:  
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…).  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…). 
Revision as of 08:20, 23 October 2013
< Math
If you consider using ConTeXt for a paper with a proper formula numbering please wait until it is implemented in mkiv. At the moment only a very limited formula numbering is possilbe:
 number formula(s) with a plain number
 number formula(s) with a subnumber
Not possible is at the moment:
 Table of formulas
 Named subformulas
 Mixure of formulas and subformulas in one block
 Reference on a (sub)formula without having it listed in the Table of formulas
For more details and actual state see mailing list and Summary of formula numbering problem.
Contents
Display Math
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
ConTeXt provides an easy way to number the display maths equations. Simply, put \placeformula before \startformula and you will get numbered equations. Thus,
The famous result (once more) is given by \placeformula \startformula c^2 = a^2 + b^2. \stopformula 
This, when typeset, produces the following:

The \placeformula command is optional, and produces the equation number; leaving it off produces an unnumbered equation.
Changing format of numbers
You can use \setupformulas to change the format of numbers. For example to get bold numbers inside square brackets use
\setupformulas[left={[},right={]},numberstyle=bold] 
which gives

To get equations also numbered by section, add the command:
\setupnumber[formula][way=bysection]
To the start of your document.
To get alphabets instead of numbers, use
\setupformulas[conversion=Character] 
which gives

Changing 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:
In the next examples we explicitly align formulas to the left (\raggedleft), center 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
Or in print:
With formula numbers the code is:
\setuppapersize[A5] \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
And the formulas look like:
When tracing is turned on (\tracemathtrue) you can visualize the bounding box of the formula,
As you can see, the dimensions are the natural ones, but if needed you can force a normalized line:
\setuppapersize[A5] \setuplayout[textwidth=8cm] \setupformulas[align=middle,strut=yes] \tracemathtrue \placeformula \startformula c^2 = a^2 + b^2 \stopformula
This time we get a more spacy result. [Ed. Note: For this example equation, there appears to be no visible change.]
We will now show a couple of more settings and combinations of settings. In centered formulas, the number takes no space
\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
You can influence the placement of the whole box with the parameters leftmargin and rightmargin.
\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] \startformula c^2 = a^2 + b^2 \stopformula \placeformula \startformula c^2 = a^2 + b^2 \stopformula
You can also inherit the margin from the environment.
\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,margin=standard] \startformula c^2 = a^2 + b^2 \stopformula \placeformula \startformula c^2 = a^2 + b^2 \stopformula
The distance between the formula and the number is only applied when the formula is left or right aligned.
\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
Referencing formulae
Equations can be referred to by simply adding a label to \placeformula and using \ref to create the reference:
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]. 
This, when typeset, produces the following:

Note, that \ref expects two arguments, therefore you need the brackets twice.
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
\definereferenceformat[eqref][left=(,right=)]
See References for more examples of \definereferenceformat.
SubFormula Numbering
Automatic SubFormula Numbering
To use subformula numbering, you can use \startsubformulas/\stopsubformulas. For example
Examples: \startsubformulas[eq:1] \placeformula[eq:first] \startformula c^2 = a^2 + b^2 \stopformula \placeformula[eq:second] \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]). 

The Manual Method
Sometimes, you need more fine grained control over numbering of subformulas. In that case one can make use of the optional agument of \placeformula command and the related \placesubformula commands which can be used to produce subformula numbering. For example:
Examples: \placeformula{a} \startformula c^2 = a^2 + b^2 \stopformula \placesubformula{b} \startformula c^2 = a^2 + b^2 \stopformula 

What's going on here is simpler than it might appear at first glance. Both \placeformula and \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).
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 \placeformula defines \formulanumber and \subformulanumber commands, which provide hooks to allow the use of ConTeXtmanaged 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 \eqno. Note that the optional tag is inherited from \placeformula.
More examples: \placeformula{c} \startformula \let\doplaceformulanumber\empty c^2 = a^2 + b^2 \eqno{\formulanumber} \stopformula 

In order for this to work properly, we need to turn off ConTeXt's automatic formula number placement; thus the \let command to empty \doplaceformulanumber, which must be placed after the start of the formula. In many practical examples, however, this is not necessary; ConTeXt redefines \displaylines and \eqalignno to do this automatically.
For more control over subformula numbering, \formulanumber and \subformulanumber have an optional argument parallel to that of \placeformula, as demonstrated in this use of plain TeX's \eqalignno, which places multiple equation numbers within one formula.
\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 

Note that both \formulanumber and \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 \placefigure and \formulanumber, the latter takes precedence.
More examples for leftlocated 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 

 23:46, 15 Aug 2005 (CEST) Prinse Wang
List of Formulas
You can have a list of the formulas contained in a document by using \placenamedformula instead of \placeformula. Only the formulas written with \placenamedformula are not put in the list, so that you can control precisely the content of the list.
\placenamedformula takes as first parameter the name of the formula put in the list. The other \placeformula features are still available. The list can be formatted like any other list.
Example:
\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} \startformula a = 3 \stopformula \endgraf
Gives:
Shaded background for part of a displayed equation
(see also Framed)
To highlight part of a formula, you can give it a gray background using \mframed: the following is the code you can use in mkii (see below what one has to do in mkiv):
\setuppapersize[A5] \setupcolors[state=start] \def\graymath{\mframed[frame=off, background=color, backgroundcolor=gray, backgroundoffset=3pt]} \startformula \ln (1+x) =\, \graymath{x  {x^2\over2}} \,+ {x^3\over3}\cdots. \stopformula
In mkiv the code is slightly different: one may define \graymath directly using \definemathframed
\setuppapersize[A5] \definemathframed[graymath] [ frame=off, location=mathematics, background=color, backgroundcolor=lightgray, backgroundoffset=2pt ] \starttext 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)$. \stoptext
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…).