Difference between revisions of "Multiline equations"

From Wiki
Jump to navigation Jump to search
m (Added definemathalign)
m (Add ties (non-breaking spaces) before equation numbers and variable names.)
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
< [[Main Page]] | [[Math]] | [[Math with newmat]] | [[MathML]] >
 
< [[Main Page]] | [[Math]] | [[Math with newmat]] | [[MathML]] >
 +
 +
Be sure to also read [http://dl.contextgarden.net/myway/mathalign.pdf Using \startalign and friends] written by Aditya Mahajan.
  
 
== Basic Alignment ==
 
== Basic Alignment ==
Line 7: Line 9:
  
 
* Latex style
 
* Latex style
<texcode>
 
\startformula \startalign
 
v &= u + at \\
 
h &= ut + \frac12 gt^2 \\
 
\stopalign \stopformula
 
</texcode>
 
 
Note the \\ in the last line, above.
 
  
This appears as follows:
+
<context source=yes text="Note the \\ in the last line, above. This appears as follows:">
 
 
<context>
 
 
\startformula \startalign
 
\startformula \startalign
 
  v &= u + at \\
 
  v &= u + at \\
Line 27: Line 19:
 
* Context Style
 
* Context Style
  
<texcode>
+
<context source=yes>
\startformula \startalign
 
\NC v \NC = u + at \NR
 
\NC h \NC= ut + \frac12 gt^2 \NR
 
\stopalign \stopformula
 
</texcode>
 
 
 
<context>
 
 
\startformula \startalign
 
\startformula \startalign
 
  \NC v \NC = u + at \NR
 
  \NC v \NC = u + at \NR
Line 47: Line 32:
 
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>
  
 
+
<context source=yes>
<texcode>
 
\startformula \startalign[n=3]
 
\NC v \NC = u \NC+ at \NR
 
\NC h \NC= ut \NC+ \frac12 gt^2 \NR
 
\stopalign \stopformula
 
</texcode>
 
 
 
<context>
 
 
\startformula \startalign[n=3]
 
\startformula \startalign[n=3]
 
  \NC v \NC = u \NC+ at \NR
 
  \NC v \NC = u \NC+ at \NR
Line 64: Line 41:
 
== Alignment of each column ==
 
== Alignment 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  
<texcode>
 
\startformula \startalign[n=3,align={right,middle,left}]
 
\NC v \NC = u \NC+ at \NR
 
\NC h \NC= ut \NC+ \frac12 gt^2 \NR
 
\stopalign \stopformula
 
</texcode>
 
  
<context>
+
<context source=yes>
 
\startformula \startalign[n=3,align={right,middle,left}]
 
\startformula \startalign[n=3,align={right,middle,left}]
 
  \NC v \NC = u \NC+ at \NR
 
  \NC v \NC = u \NC+ at \NR
Line 80: Line 51:
 
This mechanism allows fancier alignments like
 
This mechanism allows fancier alignments like
  
<texcode>
+
<context source=yes>
\startformula \startalign[n=4,align={left,right,middle,left}]
 
\NC \text{We have} \quad \NC v \NC = u \NC+ at \NR
 
\NC \text{and}          \NC h \NC= ut \NC+ \frac12 gt^2 \NR
 
\stopalign \stopformula
 
</texcode>
 
 
 
 
 
<context>
 
 
\startformula \startalign[n=4,align={left,right,middle,left}]
 
\startformula \startalign[n=4,align={left,right,middle,left}]
 
  \NC \text{We have} \quad \NC v \NC = u \NC+ at \NR
 
  \NC \text{We have} \quad \NC v \NC = u \NC+ at \NR
Line 97: Line 60:
 
== Working with equation numbering ==
 
== Working with equation numbering ==
  
aligned equations can be numbered by placing a tag after <cmd>NR</cmd>
+
aligned equations can be numbered by placing a tag after {{cmd|NR}}
  
<texcode>
+
<context source=yes>
 +
\setuplayout[scale=0.8,width=13cm]
 
\placeformula \startformula \startalign
 
\placeformula \startformula \startalign
 
  \NC v \NC = u + at \NR[eq:v]
 
  \NC v \NC = u + at \NR[eq:v]
 
  \NC h \NC = ut + \frac12 gt^2 \NR[eq:h]
 
  \NC h \NC = ut + \frac12 gt^2 \NR[eq:h]
\stopalign \stopformula
+
\stopalign \stopformula  
Equation (\in[eq:v]) tells the final velocity after  
+
Equation~(\in[eq:v]) tells the final velocity after time~$t$ and equation~(\in[eq:h]) tells the distance travelled in time~$t$.
time $t$ and equation (\in[eq:h]) tells the distance  
+
</context>
travelled in time $t$.
+
 
</texcode>
+
The numbering can be changed to a subformula style using {{cmd|placesubformula}} instead of {{cmd|placeformula}} and numbering the subformulas using {{cmd|NR|2=[+][a]}},  {{cmd|NR|2=[+][b]}}, … instead:
  
<context>
+
<context source=yes>
 
\setuplayout[scale=0.8,width=13cm]
 
\setuplayout[scale=0.8,width=13cm]
\placeformula \startformula \startalign
+
\placesubformula \startformula \startalign
  \NC v \NC = u + at \NR[eq:v]
+
  \NC v \NC = u + at \NR[+][eq:v]
  \NC h \NC = ut + \frac12 gt^2 \NR[eq:h]
+
  \NC h \NC = ut + \frac12 gt^2 \NR[+][eq:h]
 
\stopalign \stopformula  
 
\stopalign \stopformula  
Equation (\in[eq:v]) tells the final velocity after time $t$ and equation (\in[eq:h]) tells the distance travelled in time $t$.
+
Equation~(\in[eq:v]) tells the final velocity after time~$t$ and equation (\in[eq:h]) tells the distance travelled in time~$t$.
 
</context>
 
</context>
  
Line 122: Line 86:
 
== Defining new alignment structures ==
 
== Defining new alignment structures ==
  
New alignment can be defined using <cmd>definemathalignment</cmd>. 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
<texcode>
 
\definemathalignment
 
    [gather]
 
    [n=1,align={middle}]
 
  
\startformula \startgather
+
<context source=yes>
\NC  ax^2 + bx + c = 0 \NR
 
\NC \text{roots} = \frac{ -b \pm \sqrt{b^2 - 4ac}}{2a} \NR
 
\stopgather \stopformula
 
</texcode>
 
 
 
<context>
 
 
\definemathalignment
 
\definemathalignment
 
     [gather]
 
     [gather]
Line 149: Line 103:
 
== Cases ==
 
== Cases ==
  
Context provides a <cmd>startmathcases</cmd> <cmd>stopmathcases</cmd> pair to make it easy get cases.  
+
Context provides a {{cmd|startmathcases}} {{cmd|stopmathcases}} pair to make it easy to get cases.  
<texcode>
+
 
 +
<context source=yes text="gives">
 
\startformula
 
\startformula
 
  f(x) = \startmathcases
 
  f(x) = \startmathcases
Line 157: Line 112:
 
\stopmathcases
 
\stopmathcases
 
\stopformula
 
\stopformula
</texcode>
+
</context>
  
gives
+
The cases environment consists of two columns, separated by {{cmd|NC}}. The second column is by default in text mode. An alternative way of getting the same result is to define the second column as a mathcolumn with {{cmd|MC}} like so:
  
<context>
+
<texcode>
 
\startformula
 
\startformula
 
  f(x) = \startmathcases
 
  f(x) = \startmathcases
   \NC x, \NC if $0 \le x \le \frac12$ \NR
+
   \NC x, \MC \text{if } 0 \le x \le \frac12 \NR
   \NC 1-x ,\NC if $\frac12 \le x \le 1$ \NR
+
   \NC 1-x ,\MC \text{if } \frac12 \le x \le 1 \NR
 
\stopmathcases
 
\stopmathcases
 
\stopformula
 
\stopformula
</context>
+
</texcode>
  
The cases environment consists of two columns, separated by <cmd>NC</cmd>. The second column is by default in text mode. Each line must end with a <cmd>NR</cmd>.
+
Each line must end with a {{cmd|NR}}.
  
 
== Numbered Cases ==
 
== Numbered Cases ==
 +
 +
[[Category:Math]]

Revision as of 09:14, 22 June 2015

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

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

Basic Alignment

Two modes of input

  • Latex style
\startformula \startalign
 v &= u + at \\
 h &= ut + \frac12 gt^2 \\
\stopalign \stopformula

Note the \\ in the last line, above. This appears as follows:

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

(The examples here will focus on the context style, having two styles can be confusing -- Aditya )

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

Alignment 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={right,middle,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={left,right,middle,left}]
 \NC \text{We have} \quad \NC v \NC = u \NC+ at \NR
 \NC \text{and}           \NC h \NC= ut \NC+ \frac12 gt^2 \NR
\stopalign \stopformula

Working with equation numbering

aligned equations can be numbered by placing a tag after \NR

\setuplayout[scale=0.8,width=13cm]
\placeformula \startformula \startalign
 \NC v \NC = u + at \NR[eq:v]
 \NC h \NC = ut + \frac12 gt^2 \NR[eq:h]
\stopalign \stopformula 
Equation~(\in[eq:v]) tells the final velocity after time~$t$ and equation~(\in[eq:h]) tells the distance travelled in time~$t$.

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]
\placesubformula \startformula \startalign
 \NC v \NC = u + at \NR[+][eq:v]
 \NC h \NC = ut + \frac12 gt^2 \NR[+][eq:h]
\stopalign \stopformula 
Equation~(\in[eq:v]) tells the final velocity after time~$t$ and equation (\in[eq:h]) tells the distance travelled in time~$t$.

Changing the number of columns

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={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
 f(x) = \startmathcases
   \NC x, \NC if $0 \le x \le \frac12$ \NR
   \NC 1-x ,\NC if $\frac12 \le x \le 1$ \NR
\stopmathcases
\stopformula

gives

The cases environment consists of two columns, separated by \NC. The second column is by default in text mode. An alternative way of getting the same result is to define the second column as a mathcolumn with \MC like so:

\startformula
 f(x) = \startmathcases
   \NC x, \MC \text{if } 0 \le x \le \frac12 \NR
   \NC 1-x ,\MC \text{if } \frac12 \le x \le 1 \NR
\stopmathcases
\stopformula

Each line must end with a \NR.

Numbered Cases