Difference between revisions of "Multiline equations"

From Wiki
Jump to navigation Jump to search
Line 3: Line 3:
 
Be sure to also read [http://dl.contextgarden.net/myway/mathalign.pdf Using \startalign and friends] written by Aditya Mahajan.
 
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}} ==
+
= Basic Alignment with {{cmd|startalign}} =
  
 
<context source=yes>
 
<context source=yes>
Line 14: Line 14:
 
{{cmd|NC}} stands for new column, {{cmd|NR}} stands for new row.
 
{{cmd|NC}} stands for new column, {{cmd|NR}} stands for new row.
  
== Changing the number of columns ==
+
= 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>
 
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>
Line 25: Line 25:
 
</context>
 
</context>
  
== Specify the alignment mode of each column ==
+
= Specify the alignment mode of each column =
 
If you want more control over the formatting, and want the middle column to be center aligned, you can do that by  
 
If you want more control over the formatting, and want the middle column to be center aligned, you can do that by  
  
Line 44: Line 44:
 
</context>
 
</context>
  
== Equation numbering {{cmd|startplaceformula}} ==
+
= Equation numbering {{cmd|startplaceformula}} =
  
 
Aligned equations can be numbered by using {{cmd|startplaceformula}} (as usual), and by placing a tag after {{cmd|NR}}
 
Aligned equations can be numbered by using {{cmd|startplaceformula}} (as usual), and by placing a tag after {{cmd|NR}}
Line 59: Line 59:
 
</context>
 
</context>
  
=== Sub-numbering {{cmd|startsubformulas}} ===
+
== Sub-numbering {{cmd|startsubformulas}} ==
  
 
The numbering can be changed to a subformula style by encapsulating {{cmd|startplaceformula}} with {{cmd|startsubformulas}} :
 
The numbering can be changed to a subformula style by encapsulating {{cmd|startplaceformula}} with {{cmd|startsubformulas}} :
Line 93: Line 93:
 
</context>
 
</context>
  
== Defining new alignment structures ==
+
= Defining new alignment structures =
  
 
New alignment can be defined using {{cmd|definemathalignment}}. For example, to emulate <code>gather</code> environment of amsmath, we can use
 
New alignment can be defined using {{cmd|definemathalignment}}. For example, to emulate <code>gather</code> environment of amsmath, we can use
Line 109: Line 109:
 
</context>
 
</context>
  
== Cases ==
+
= Cases =
  
 
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 137: Line 137:
 
Each line must end with a {{cmd|NR}}.
 
Each line must end with a {{cmd|NR}}.
  
== Numbered Cases ==
+
= Numbered Cases =
  
 
[[Category:Math]]
 
[[Category:Math]]

Revision as of 09:03, 12 May 2024

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

Be sure to also read Using \startalign and friends written by Aditya Mahajan.

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

Specify the alignment mode of each column

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

Equation numbering \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 \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 

Equation~(\in[eq:v]) tells the final velocity after time~$t$ and equation (\in[eq:h]) tells the distance travelled in time~$t$.

instead of \placeformula and numbering the subformulas using \NR[+][a], \NR[+][b], … instead:

The numbering can be changed to a subformula style using \placesubformula instead of \placeformula and numbering the subformulas using \NR[+][a], \NR[+][b], … instead:


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

Defining new alignment structures

New alignment can be defined using \definemathalignment. For 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

Context provides a \startmathcases \stopmathcases pair to make it easy to get cases.

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

gives

The cases environment consists of two columns, separated by \NC. 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

Each line must end with a \NR.

Numbered Cases