Difference between revisions of "Multiline equations"

From Wiki
Jump to navigation Jump to search
(Replaced content with "moved to Math/Multiline_equations")
Tag: Replaced
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
< [[Main Page]] | [[Math]] | [[Math with newmat]] | [[MathML]] >
+
moved to [[Math/Multiline_equations]]
 
 
Be sure to also read [http://dl.contextgarden.net/myway/mathalign.pdf Using \startalign and friends] written by Aditya Mahajan.
 
 
 
= Basic Alignment with {{cmd|startalign}} =
 
 
 
<context source=yes>
 
\startformula\startalign
 
\NC v \NC = u + at \NR
 
\NC h \NC = ut + \frac12 gt^2 \NR
 
\stopalign\stopformula
 
</context>
 
 
 
{{cmd|NC}} stands for new column, {{cmd|NR}} stands for new row.
 
 
 
= Changing the number of columns =
 
 
 
The above equations were aligned at <code>=</code>. Suppose you also want the <code>+</code> to align. Well, this is simple in context, simply specify the number of columns with <code>\startalign</code>
 
 
 
<context source=yes>
 
\startformula \startalign[n=3]
 
\NC v \NC = u  \NC + at \NR
 
\NC h \NC = ut \NC + \frac12 gt^2 \NR
 
\stopalign \stopformula
 
</context>
 
 
 
= Equation numbering with {{cmd|startplaceformula}} =
 
 
 
Aligned equations can be numbered by using {{cmd|startplaceformula}} (as usual), and by placing a tag after {{cmd|NR}}
 
 
 
<context source=yes>
 
\setuplayout[scale=0.8,width=13cm]
 
\startplaceformula
 
\startformula \startalign
 
\NC v \NC = u + at \NR[eq:v]
 
\NC h \NC = ut + \frac12 gt^2 \NR[eq:h]
 
\stopalign \stopformula
 
\stopplaceformula
 
Equation~(\in[eq:v]) tells the final velocity after time~$t$ and equation~(\in[eq:h]) tells the distance travelled in time~$t$.
 
</context>
 
 
 
== Sub-numbering with {{cmd|startsubformulas}} ==
 
 
 
The numbering can be changed to a subformula style by encapsulating {{cmd|startplaceformula}} with {{cmd|startsubformulas}} :
 
 
 
<context source=yes>
 
\setuplayout[scale=0.8,width=13cm]
 
\startsubformulas[eq:total]
 
\startplaceformula
 
\startformula \startalign
 
\NC v \NC = u + at \NR[eq:v]
 
\NC h \NC = ut + \frac12 gt^2 \NR[eq:h]
 
\stopalign \stopformula
 
\stopplaceformula
 
\stopsubformulas
 
 
 
In~(\in[eq:total]), equation~(\in[eq:v]) tells the final velocity after time~$t$ and equation (\in[eq:h]) tells the distance travelled in time~$t$.
 
</context>
 
 
 
= Specifying and defining alignment with {{cmd|definemathalignment}} =
 
 
 
If you want more control over the formatting, and want the middle column to be center aligned, you can do that by
 
 
 
<context source=yes>
 
\startformula \startalign[n=3,align={1:right,2:middle,3:left}]
 
\NC v \NC = u \NC+ at \NR
 
\NC h \NC= ut \NC+ \frac12 gt^2 \NR
 
\stopalign \stopformula
 
</context>
 
 
 
This mechanism allows fancier alignments like
 
 
 
<context source=yes>
 
\startformula \startalign[n=4,align={1:right,2:right,3:middle,4:left}]
 
\NC \text{We have} \quad \NC v \NC = u \NC+ at \NR
 
\NC \text{and}    \quad \NC h \NC= ut \NC+ \frac12 gt^2 \NR
 
\stopalign \stopformula
 
</context>
 
 
 
 
 
These kind of new alignment can be defined using {{cmd|definemathalignment}}.
 
 
 
<context source=yes>
 
\definemathalignment
 
    [demom]
 
    [n=4,align={1:right,2:right,3:middle,4:left}]
 
 
 
\startformula \startdemom
 
\NC \text{We have} \quad \NC v \NC = u \NC+ at \NR
 
\NC \text{and}    \quad \NC h \NC= ut \NC+ \frac12 gt^2 \NR
 
\stopdemom \stopformula
 
</context>
 
 
 
A second example, to emulate <code>gather</code> environment of amsmath, we can use
 
 
 
<context source=yes>
 
\definemathalignment
 
    [gather]
 
    [n=1,align={1:middle}]
 
 
 
\startformula \startgather
 
\NC  ax^2 + bx + c = 0 \NR
 
\NC \text{roots} = \frac{ -b \pm \sqrt{b^2 - 4ac}}{2a} \NR
 
\stopgather \stopformula
 
 
 
</context>
 
 
 
= Cases =
 
 
 
Context provides a {{cmd|startmathcases}} {{cmd|stopmathcases}} pair to make it easy to get cases.
 
 
 
<context source=yes text="gives">
 
\startformula
 
\delta_{ij} =
 
\startmathcases
 
\NC 1 \NC \text{if } i = j \NR
 
\NC 0 \NC \text{otherwise} \NR
 
\stopmathcases
 
\stopformula
 
</context>
 
 
 
The cases environment consists of two columns, separated by {{cmd|NC}}. The second column is by default in math mode. To typeset the second column in text mode, use {{cmd|TC}}
 
 
 
<texcode>
 
\startformula
 
\delta_{ij} =
 
\startmathcases
 
\NC 1 \TC if \m{i = j} \NR
 
\NC 0 \TC otherwise \NR
 
\stopmathcases
 
\stopformula
 
</texcode>
 
 
 
Each line must end with a {{cmd|NR}}.
 
 
 
 
 
There are several built-in commands for displaying mathematical formulas. For instance
 
 
 
<texcode>
 
\startcases, \stopcases
 
\startmathmatrix, \stopmathmatrix
 
\startalign,\stopalign
 
</texcode>
 
Each has its own advantage and can be customized: for a thorough understanding of these commands please
 
refer to Aditya Mohajan's excellent user's guide
 
[https://wiki.contextgarden.net/images/archive/b/b4/20100530075041!Mathalign.pdf Using \startalign and friends].
 
These commands can be found in [[source:core-mat.tex|core-mat.tex]] file, which contains
 
most of the core ConTeXt math macros.
 
 
<context source="yes">
 
This is an example of aligned equations:
 
\startformula
 
\startcases[align={right,left},distance=3pt]
 
  \NC 2x + 3y \NC{} = 10 \NR
 
  \NC 3x + 2y \NC{} = 5,  \NR
 
\stopcases
 
\stopformula
 
and this is another one:
 
\startformula
 
\startmatrix[align={right,left},distance=3pt,left={\left\{}, right={\right.}]
 
  \NC 2x + 3y \NC{} = 10 \NR
 
  \NC 3x + 2y \NC{} = 5  \NR
 
\stopmatrix
 
\stopformula
 
</context>
 
In these structures it is important (well, in the above example it is not important and the result is the same, but I have been told that it is necessary in some other situations...) to notice that the sign "=" is preceded by "\NC{}, since
 
otherwise that sign would be considered as a binary operator and the alignment would be
 
disturbed (at least this is what I guess…Otared K.).
 
 
 
One of the powerful structures in displaying mathematical formulas is the possibility of defining custom maths alignment.
 
For instance the following definition
 
 
 
<texcode>
 
\definemathmatrix[alignedcases]
 
[align={right,left,left},
 
distance=3pt,
 
left={\left\{},
 
right={\right.},
 
style=\displaystyle]
 
</texcode>
 
 
 
creates two new commands,
 
 
 
<texcode>
 
\startalignedcases, \stopalignedcases:
 
</texcode>
 
 
 
after which one may use them in a displayed formula as follows:
 
 
 
<texcode>
 
The following formula is displayed in two lines with an appropriate alignment of the equations:
 
 
 
\startformula
 
\startalignedcases
 
\NC -\Delta u + g(u) \NC{} = f \NC \quad\mbox{in } \Omega\NR
 
\NC {\partial u \over \partial {\bf n} } \NC{} = h(u) \NC \quad\mbox{on } \partial\Omega  \NR
 
\stopalignedcases
 
\stopformula
 
</texcode>
 
 
 
<context>
 
\definemathmatrix[alignedcases]
 
[align={right,left,left},
 
distance=3pt,
 
left={\left\{},
 
right={\right.},
 
style=\displaystyle]
 
 
 
The following formula is displayed in two lines with an appropriate alignment of the equations:
 
 
 
\startformula
 
\startalignedcases
 
\NC -\Delta u + g(u) \NC{} = f \NC \quad\mbox{in } \Omega\NR
 
\NC {\partial u \over \partial {\bf n} } \NC{} = h(u) \NC \quad\mbox{on } \partial\Omega  \NR
 
\stopalignedcases
 
\stopformula
 
</context>
 
 
 
[[Category:Math]]
 
 
 
= Numbered Cases =
 
 
 
[[Category:Math]]
 

Latest revision as of 10:40, 12 May 2024