Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

LaTeX Math Formulas Advanced

LaTeX Math Formulas Advanced

The previous part solved single-line formulas; this part handles the "big chunks": matrices, equation systems, multi-line derivations, and formula numbering.

They all come from the amsmath package - which is why every math document's preamble should have a line \usepackage{amsmath}.


Matrices: The matrix Family

Matrices are represented by environments: & separates columns, \\ changes lines, and the environment name determines the outer bracket style.

Environment Outer Style Typical use
matrix No brackets Concatenating data blocks
pmatrix Parentheses ( ) Ordinary matrix
bmatrix Square brackets [ ] Coefficient matrix, linear algebra
Bmatrix Braces { } Set notation
vmatrix Single vertical bar | | Determinant
Vmatrix Double vertical bar || || Norm

pmatrix (parenthesized matrix)

bmatrix (bracketed matrix)

vmatrix (determinant)

Example: An n-th Order Matrix with Ellipses

[

  \begin{pmatrix}        

  a_{11} & \cdots & a_{1n} \
  \vdots & \ddots & \vdots \
  a_{n1} & \cdots & a_{nn}
\end{pmatrix}
\

]

The three ellipses each have their role: \cdots horizontal, \vdots vertical, \ddots diagonal. When a matrix has many elements, always use ellipses instead of typing in dozens of elements.

When a large matrix does not fit in an inline formula, you can use the smallmatrix environment, which compresses line spacing and font size.


Multi-Line Derivation: The align Environment

align is the main environment for math derivations: multiple formula lines align at the position marked by &, and each line is automatically numbered.

Example: A Two-Line Derivation

\begin

{

align}
  (a+b)^2 &= a^2 + 2ab + b^2 \
          &= a^2 + b^2 + 2ab
\end{align

}

There are only two rules: & is placed at the "alignment anchor" (usually before the equal sign), which splits the line into two columns; \\ changes lines. Each line automatically gets a number.

Example: Definite Integral Calculation

\begin

{

align}
  \int_0^1 x^2 ,\mathrm{d}x
    &= \left[ \frac{x^3}{3} \right]_0^1 \notag \[2pt]
    &= \frac{1}{3}
\end{align

}

Here two new notations appear: \notag makes a line unnumbered (use it for intermediate derivation lines that need no number); the [2pt] after \\ fine-tunes line spacing and can be omitted.

When no numbering is needed at all, use the starred align* environment - this is consistent with the section command \section*: the asterisk means "no number, not in the table of contents".


Equation Systems and Piecewise Functions: The cases Environment

The cases environment specifically generates a grouped structure with a large brace on the left; it is the standard way to write systems of equations and piecewise functions.

Example: A System of Two Linear Equations in Two Variables

[

  \begin{cases}        

  x + y = 5 \
  2x - y = 1
\end{cases}
\

]

Example: Piecewise Function

[

f(x) =
\begin{cases}
  -x,  & x < 0 \
  x,   & x \geq 0
\end{cases}
\

]

In a piecewise function, the role of & changes: it splits "function value" and "condition" into two columns, and the condition in the second column is automatically right-aligned.


Formula Numbering and Referencing

Display formulas with [...] are not numbered; for formulas that need to be referenced, use the equation environment, which numbers them automatically.

Example: Numbering and Referencing

\

documentclass

{

ctexart}
\usepackage{amsmath

}

               

% align, equation, and cases all need it

\begin

{

document}
From equation \eqref{eq:pyth}, the hypotenuse of a right triangle satisfies
\begin{equation} \label{eq:pyth

}

   

% \label assigns a tag for \eqref to reference

  a^2 + b^2 = c^2,

\end

{

equation}
and the sum of the first $n$ terms of an arithmetic progression is
\begin{equation}
  S_n = \frac{n(a_1 + a_n)}{2}.
\end{equation}
\end{document

}

Compilation result of equation auto-numbering and referencing

Remember three steps: equation handles numbering, \label{key} assigns a tag, \eqref{key} references it (automatically with parentheses).

It is recommended to name tags "type:content", such as eq:pyth, eq:sum - once you have many formulas, names like eq:3 will be unrecognizable even to yourself.

If a reference shows as (??), it means .aux has not been updated; compile once more (an old friend by now).


Breaking Long Formulas: The split Environment

When a formula is too long to fit the text width, put it in equation and break lines with split - the whole segment keeps one number.

Example: Breaking a Long Formula

\begin

{

equation}
\begin{split}
  \sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})
    &= \sum_{i=1}^{n} x_i y_i - n\bar{x}\bar{y} \
    &= \frac{1}{n}\sum_{i=1}^{n} x_i y_i - \bar{x}\bar{y}
\end{split}
\end{equation

}

The difference between split and align: align numbers each of "multiple formulas" separately; split breaks "one formula" into several lines sharing one number. The nesting also shows the point - split must live inside equation (or [...]).


Troubleshooting Common Formula Errors

Math-mode errors account for more than half of beginners' errors; the high-frequency ones are as follows.

Incorrect Writing Problem Correct Writing
Using \alpha directly in text Math commands can only be used in math mode $\alpha$
$a_1 + b_$ _ or ^ is missing content after it Fix it: $a_1 + b_2$
[ x = \frac{1}{2} ] Using \dfrac etc. without loading amsmath In the preamble: \usepackage{amsmath}
[ a & b \ c & d ] Using & in a normal display formula & belongs only to environments like matrix and align
Mixing Chinese explanations inside a formula Font and line breaking are all messed up Put explanations outside $ or use \text{Chinese}
Forgetting \\ in align Multiple lines crammed into one Add \\ at the end of each line (except the last)

\text{...} (provided by amsmath) deserves a mention: occasionally you need to embed a short text inside a formula (such as a condition "when x > 0" or a unit); wrapping it with \text keeps upright font and normal spacing.


Summary

Requirements Environment / Command
Matrix pmatrix / bmatrix / vmatrix (& separates columns, \\ changes lines)
Multi-line derivation (each line numbered) align, starred version not numbered
System of equations / piecewise function cases
Single numbered formula equation + \label
Referencing a formula number \eqref{key}
Breaking a long formula equation with embedded split