Difference between revisions of "Multiline equations"

From Wiki
Jump to navigation Jump to search
Line 106: Line 106:
 
</context>
 
</context>
  
= Cases with {{cmd|startmathcases}} (two columns)=
+
= Cases =
 +
 
 +
== with {{cmd|startmathcases}} (two columns) ==
  
 
Context provides a {{cmd|startmathcases}} {{cmd|stopmathcases}} pair to make it easy to get cases.  
 
Context provides a {{cmd|startmathcases}} {{cmd|stopmathcases}} pair to make it easy to get cases.  
Line 143: Line 145:
 
</context>
 
</context>
  
= More complex =
+
== with {{cmd|definemathmatrix}} ==
  
 
One of the powerful structures in displaying mathematical formulas is the possibility of defining custom maths alignment.
 
One of the powerful structures in displaying mathematical formulas is the possibility of defining custom maths alignment.
 
For instance the following definition  
 
For instance the following definition  
  
<texcode>
+
<context source="yes">
\definemathmatrix[alignedcases]
+
\definemathmatrix
[align={right,left,left},
+
  [alignedcases]           % creates 2 new commands \startalignedcases \stopalignedcases
distance=3pt,
+
   [align={right,left,left},
left={\left\{},
+
  distance=3pt,
right={\right.},
+
  left={\left\{},
style=\displaystyle]
+
  right={\right.},
</texcode>
+
  style=\displaystyle]
 
 
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:
 
The following formula is displayed in two lines with an appropriate alignment of the equations:
Line 193: Line 168:
 
\stopformula
 
\stopformula
 
</context>
 
</context>
 
[[Category:Math]]
 
  
 
= Numbered Cases =
 
= Numbered Cases =
  
 
[[Category:Math]]
 
[[Category:Math]]

Revision as of 09:33, 12 May 2024

< Main Page | Math | Math with newmat | MathML >

Be sure to also read 2010 - Using \startalign and friends written by Aditya Mahajan (My Way article).

Basic Alignment with \startalign

\startformula\startalign
 \NC v \NC = u + at \NR
 \NC h \NC = ut + \frac12 gt^2 \NR
\stopalign\stopformula

\NC stands for new column, \NR stands for new row.

Changing the number of columns

The above equations were aligned at =. Suppose you also want the + to align. Well, this is simple in context, simply specify the number of columns with \startalign

\startformula \startalign[n=3]
 \NC v \NC = u  \NC + at \NR
 \NC h \NC = ut \NC + \frac12 gt^2 \NR
\stopalign \stopformula

Equation numbering with \startplaceformula

Aligned equations can be numbered by using \startplaceformula (as usual), and by placing a tag after \NR

\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$.

Sub-numbering with \startsubformulas

The numbering can be changed to a subformula style by encapsulating \startplaceformula with \startsubformulas :

\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$.

Specifying and defining alignment with \definemathalignment

If you want more control over the formatting, and want the middle column to be center aligned, you can do that by

\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

This mechanism allows fancier alignments like

\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


These kind of new alignment can be defined using \definemathalignment.

\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

A second example, to emulate gather environment of amsmath, we can use

\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

Cases

with \startmathcases (two columns)

Context provides a \startmathcases \stopmathcases pair to make it easy to get cases. The cases environment consists of two columns, separated by \NC. Each line must end with a \NR.

\startformula
 \startcases[align={right,left},distance=3pt]
   \NC 2x + 3y \NC = 10 \NR
   \NC 3x + 2y \NC = 5  \NR
 \stopcases
\stopformula

and

\startformula
\delta_{ij} =
 \startmathcases
 \NC 1 \NC \text{if } i = j \NR
 \NC 0 \NC \text{otherwise} \NR
 \stopmathcases
\stopformula

gives

The second column is by default in math mode. To typeset the second column in text mode, use \TC:

\startformula
\delta_{ij} =
 \startmathcases
 \NC 1 \TC if \m{i = j} \NR
 \NC 0 \TC otherwise    \NR
 \stopmathcases
\stopformula

gives

with \definemathmatrix

One of the powerful structures in displaying mathematical formulas is the possibility of defining custom maths alignment. For instance the following definition

\definemathmatrix
  [alignedcases]            % creates 2 new commands \startalignedcases \stopalignedcases
  [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

Numbered Cases