LaTeX/Advanced Mathematics

From Wikibooks, the open-content textbooks collection

< LaTeX
Jump to: navigation, search

This page outlines some more advanced uses of mathematics markup using LaTeX. In particular it makes heavy use of the AMS-LaTeX packages supplied by the American Mathematical Society.

Contents

[edit] Equation numbering

The equation environment automatically numbers your equation.

\begin{equation} 
 f(x)=(x+a)(x+b)
\end{equation}
 {f(x)}=(x+a)(x+b) {\color{White} ww} (1) \,

You can also use the \label and \ref commands to label and reference equations respectively.

To have the enumeration follow from your section or subsection heading, you must use the amsmath package or use AMS class documents. Then enter

\numberwithin{equation}{section}

to the preamble to get enumeration at the section level or

\numberwithin{equation}{subsection}

to have the enumeration go to the subsection level.

\documentclass[12pt]{article}
\usepackage{amsmath}
 \numberwithin{equation}{subsection}
 \begin{document}
 \section{First Section}
 
\subsection{A subsection}
\begin{equation}
 L' = {L}{\sqrt{1-\frac{v^2}{c^2}}}
\end{equation}
\end{document}
 L' = {L}{\sqrt{1-\frac{v^2}{c^2}}} {\color{White} ww} (1.1.1) \,

[edit] Vertically aligning displayed mathematics

An often encountered problem with displayed environments (displaymath and equation) is the lack of any ability to span multiple lines. While it is possible to define lines individually, these will not be aligned.

[edit] Above and below

The \overset and \underset commands[1] typeset symbols above and below expressions. This can be particularly useful for creating new binary relations:

\[
 A \overset{!}{=} B
\]

 A \overset{!}{=} B
\,

If the purpose is to make comments on particular parts of an equation, the \overbrace and \underbrace commands may be more useful, however they have a different syntax:

\[
 z = \overbrace{
   \underbrace{x}_\text{real} + 
   \underbrace{iy}_\text{imaginary}
  }^\text{complex number}
\]

 z = \overbrace{
   \underbrace{x}_\text{real} + 
   \underbrace{iy}_\text{imaginary}
  }^\text{complex number}
\,

The \xleftarrow and \xrightarrow commands[1] produce arrows which extend to the length of the text. Yet again, the syntax is different: the optional argument (using [ & ]) specifies the subscript, and the mandatory argument (using { & }) specifies the superscript (this can be left empty).

\[
 A \xleftarrow{\text{this way}} B 
  \xrightarrow[\text{or that way}]{} C
\]

A \xleftarrow{\text{this way}} B \xrightarrow[\text{or that way}]{} C
\,

[edit] align and align*

The align and align* environments[1] are used for arranging equations of multiple lines. As with matrices and tables, \\ specifies a line break, and & is used to indicate the point at which the lines should be aligned.

The align* environment is used like the displaymath or equation*environment:

\begin{align*}
 f(x) &= (x+a)(x+b) \\
 &= x^2 + (a+b)x + ab
\end{align*}
\begin{align}
f(x) &= (x+a)(x+b) \\
&= x^2 + (a+b)x + ab
\end{align}\,

To force numbering on a specific line, use the \tag{...} command before the linebreak.

The align is similar, but automatically numbers each line like the equation environment. Individual lines may be referred to by placing a \label{...} before the linebreak. The \nonumber or \notag command can be used to suppress the number for a given line:

\begin{align}
 f(x) = x^4 &+ 7x^3 +2x^2+ \nonumber \\
 & +10x + 12
\end{align}
\begin{align}
f(x) = x^4 &+ 7x^3 +2x^2+ \\
&  +10x + 12 \qquad \qquad (3)
\end{align}

Additional &'s on a single line will specify multiple "equation columns", each of which is aligned.

[edit] The cases environment

The cases environment[1] allows the writing of piecewise functions:

\[
 u(x) = 
  \begin{cases} 
   \exp{x} & \text{if } x \geq 0 \\
   1 & \text{if } x < 0
  \end{cases}
\]

u(x) = 
\begin{cases}
\exp{x} & \text{if } x \geq 0 \\
1 & \text{if } x < 0
\end{cases}

Just like before, you don't have to take care of definition or alignment of columns, LaTeX will do it for you.

[edit] Other environments

Although align and align* are the most useful, there are several other environments which may also be of interest:

Environment name Description Notes
eqnarray and eqnarray* Similar to align and align* Not recommended since spacing is inconsistent
split[1] Similar to align*, but used inside another displayed mathematics environment Combined with equation, all lines will only be assigned one equation number.
multline and multline*[1] First line left aligned, last line right aligned Equation number aligned vertically with first line and not centered as with other other environments.
gather and gather*[1] Consecutive equations without alignment
flalign and flalign*[1] Similar to align, but left aligns first equation column, and right aligns last column
alignat and alignat*[1] Takes an argument specifying number of columns

[edit] Boxed Equations

For a single equation, with the tag outside the box, use \boxed{}:

\begin{equation}
\boxed{x^2+y^2 = z^2}
\end{equation}

If you want the entire line or several equations to be boxed, use a minipage inside an fbox:

\fbox{
\addtolength{\linewidth}{-2\fboxsep}%
\addtolength{\linewidth}{-2\fboxrule}%
\begin{minipage}{\linewidth}
\begin{equation}
x^2+y^2=z^2
\end{equation}
\end{minipage}
}

[edit] Custom operators

Although many common operators are available in LaTeX, sometimes you will need to write your own, for example to typeset the argmax operator. The \operatorname and \operatorname* commands[1] display a custom operators, the * version sets the underscored option underneath like the \lim operator:

\[
\operatorname{arg\,max}_a f(a) 
= \operatorname*{arg\,max}_b f(b)
\]
\operatorname{arg\,max}_a f(a) = \underset{b}\operatorname{arg\,max} f(b)

However if the operator is frequently used, it is preferable to keep within the LaTeX ideal of markup to define a new operator. The \DeclareMathOperator and \DeclareMathOperator* commands[1] are specified in the header of the document:

\DeclareMathOperator*{\argmax}{arg\,max}

This defines a new command which may be referred to in the body:

\[
\argmax_c f(c)
\]
\underset{c}{\operatorname{arg\,max}} f(c)

[edit] Changing font size

Probably a rare event, but there may be a time when you would prefer to have some control of the size. For example, using text-mode maths, by default a simple fraction will look like this: \textstyle \frac{a}{b} where as you may prefer to have it displayed larger, like when in display mode, but still keeping it in-line, like this: \displaystyle \frac{a}{b} .

A simple approach is to utilize the predefined sizes for maths elements:

\displaystyle Size for equations in display mode
\textstyle Size for equations in text mode
\scriptstyle Size for first sub/superscripts
\scriptscriptstyle Size for subsequent sub/superscripts

A classic example to see this in use is typesetting continued fractions. The following code provides an example.

\begin{equation}
  x = a_0 + \frac{1}{a_1 + \frac{1}{a_2 + \frac{1}{a_3 + a_4}}}
\end{equation}

x = a_0 + \frac{1}{a_1 + \frac{1}{a_2 + \frac{1}{a_3 + a_4}}}

As you can see, as the fractions continue, they get smaller (although they will not get any smaller as in this example, they have reached the \scriptstyle limit. If you wanted to keep the size consistent, you could declare each fraction to use the display style instead, e.g.:

\begin{equation}
  x = a_0 + \frac{1}{\displaystyle a_1 
          + \frac{1}{\displaystyle a_2 
          + \frac{1}{\displaystyle a_3 + a_4}}}
\end{equation}

   x = a_0 + \frac{1}{\displaystyle a_1 
           + \frac{1}{\displaystyle a_2 
           + \frac{1}{\displaystyle a_3 + a_4}}}

Another approach is to use the \DeclareMathSizes command to select your preferred sizes. You can only define sizes for \displaystyle, \textstyle, etc. One potential downside is that this command sets the global maths sizes, as it can only be used in the document preamble.

However, it's fairly easy to use: \DeclareMathSizes{ds}{ts}{ss}{sss}, where ds is the display size, ts is the text size, etc. The values you input are assumed to be point (pt) size.

NB the changes only take place if the value in the first argument matches the current document text size. It is therefore common to see a set of declarations in the preamble, in the event of the main font being changed. E.g.,

\DeclareMathSizes{10}{18}{12}{8}   % For size 10 text
\DeclareMathSizes{11}{19}{13}{9}   % For size 11 text
\DeclareMathSizes{12}{20}{14}{10}  % For size 12 text

[edit] Notes

  1. a b c d e f g h i j k Requires the amsmath package


Previous: Mathematics Index Next: Theorems