next_inactive up previous


AMTH247 Lecture 4

LATEX -- Alignment

In this lecture we will look at various topics to do with alignment -- tables, matrices and aligning equations.


Reading: NSSI §2.11.5 and §3.5 User's Guide for the amsmath Package §3 and §4.1.



Contents

Tables

Simple Tables

Tables are created with the tabular environment.

Example:

  \begin{center}
    \begin{tabular}{lcl}
      Name & Date & Formula \\
      Newton   & 1687 & $F = m a$ \\
      Einstein & 1905 & $E = m c^2$ \\
    \end{tabular}
  \end{center}
Name Date Formula
Newton 1687 $ F = m a$
Einstein 1905 $ E = m c^2$
Notes:
  1. Tables are usually placed in the centre of the page, hence the center environment.
  2. Directly after the \begin{tabular} command, the number and alignment of the columns in the table is specified. The alignments are l - left, c - center, and r - right. In our example {lcl} specifies three columns with the indicated alignments.
  3. Within each line of the table columns are separated by an ampersand, &, and the line terminated by \\.

Adding Lines

  1. Vertical lines are indicated by a | between alignment specifiers.
  2. Horizontal lines are indicated by the command \hline at the appropriate position.

Example:

  \begin{center}
    \begin{tabular}{|l||cl|}
      \hline
      Name & Date & Formula \\
      \hline
      Newton   & 1687 & $F = m a$ \\
      Einstein & 1905 & $E = m c^2$ \\ 
      \hline
    \end{tabular}
  \end{center}
Name Date Formula
Newton 1687 $ F = m a$
Einstein 1905 $ E = m c^2$

Vertical Spacing

Vertical spacing of tables can be altered by using changing \arraystretch. In the example below this is altered within the center environment; if it were done outside the environment the change would affect the whole document.

Example

  \begin{center}
    \renewcommand{\arraystretch}{1.25}
    \begin{tabular}{|l||cl|}
      \hline
      Name & Date & Formula \\
      \hline
      Newton   & 1687 & $F = m a$ \\
      Einstein & 1905 & $E = m c^2$ \\ 
      \hline
    \end{tabular}
  \end{center}
Name Date Formula
Newton 1687 $ F = m a$
Einstein 1905 $ E = m c^2$

multicolumn

The \multicolumn command can be used to to spread items across columns of a table.

Example:

  \begin{center}
    \renewcommand{\arraystretch}{1.25}
    \begin{tabular}{|l||cl|}
      \hline
      \multicolumn{3}{|c|}{Physics Formulas} \\
      \hline
      Name & Date & Formula \\
      \hline
      Newton   & 1687 & $F = m a$ \\
      Einstein & 1905 & $E = m c^2$ \\ 
      \hline
    \end{tabular}
  \end{center}
Physics Formulas
Name Date Formula
Newton 1687 $ F = m a$
Einstein 1905 $ E = m c^2$
In this example
      \multicolumn{3}{|c|}{Physics Formulas} \\
indicates that the entry should span 3 columns. A \multicolumn line has its own vertical lines.

Example:

  \begin{center}
    \renewcommand{\arraystretch}{1.25}
    \begin{tabular}{|l||cl|}
      \multicolumn{3}{c}{Physics Formulas} \\
      \hline
      Name & Date & Formula \\
      \hline
      Newton   & 1687 & $F = m a$ \\
      Einstein & 1905 & $E = m c^2$ \\ 
      \hline
    \end{tabular}
  \end{center}
Physics Formulas
Name Date Formula
Newton 1687 $ F = m a$
Einstein 1905 $ E = m c^2$

Mathematical Arrays

Arrays

The array environment is used to align mathematical formulas and works in much the same way as the tabular environment.

Example:

  $$ \mathbf{A} = \left[ \begin{array}{cccc}
             a_{11} & a_{12} & \ldots & a_{1n} \\
             a_{21} & a_{22} & \ldots & a_{2n} \\
             \vdots & \vdots & \ddots & \vdots \\
             a_{m1} & a_{m2} & \ldots & a_{mn} 
        \end{array} \right] $$

$\displaystyle \mathbf{A} = \left[ \begin{array}{cccc}
a_{11} & a_{12} & \ldots...
...& \ddots & \vdots \\
a_{m1} & a_{m2} & \ldots & a_{mn}
\end{array} \right] $

  $$ y = \left\{ \begin{array}{rcc}
          -1 & \text{for} & x < 0 \\
           0 & \text{for} & x = 0 \\
           1 & \text{for} & x > 0 
        \end{array} \right. $$

$\displaystyle y = \left\{ \begin{array}{rcc}
-1 & \text{for} & x < 0 \\
0 & \text{for} & x = 0 \\
1 & \text{for} & x > 0
\end{array} \right. $

Note the use of \right. as an invisible right bracket and the use of right alignment of the first column. In the next two subsections we will see alternative ways of doing these examples using the amsmath package.

Matrices

The amsmath package provides a convenient way of formatting matrices. There are a number of different environments which enclose matrices in different types of braces:
Environment Braces
matrix None
pmatrix $ ( \; )$
bmatrix $ [ \; ]$
Bmatrix $ \{ \; \}$
vmatrix $ \vert \; \vert$
Vmatrix $ \vert \; \vert$
As for tables and arrays, the matrix elements are separated by & and the line terminated by \\. Unlike tables and arrays, matrices do not need alignment specifiers.

Example:

  $$ \mathbf{A} = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 
          \\ 7 & 8 & 9 \end{pmatrix} $$

$\displaystyle \mathbf{A} = \begin{pmatrix}1 & 2 & 3 \\ 4 & 5 & 6
\\ 7 & 8 & 9 \end{pmatrix} $

  $$ \mathbf{A} = \begin{bmatrix} 
             a_{11} & a_{12} & \ldots & a_{1n} \\
             a_{21} & a_{22} & \ldots & a_{2n} \\
             \vdots & \vdots & \ddots & \vdots \\
             a_{m1} & a_{m2} & \ldots & a_{mn} \\ 
         \end{bmatrix} $$

$\displaystyle \mathbf{A} = \begin{bmatrix}a_{11} & a_{12} & \ldots & a_{1n} \\ ...
...dots & \ddots & \vdots \\
a_{m1} & a_{m2} & \ldots & a_{mn} \\ \end{bmatrix} $

Cases

The following example shows how to use the cases environment. Note the use of \quad spacing.

Example:

  $$ y = \begin{cases}
          -1 &\text{for} \quad x < 0 \\
           \hfill 0 &\text{for} \quad x = 0 \\
           \hfill 1 &\text{for} \quad x > 0 \\
         \end{cases} $$

$\displaystyle y = \begin{cases}
-1 &\text{for} \quad x < 0 \\
\hfill 0 &\text{for} \quad x = 0 \\
\hfill 1 &\text{for} \quad x > 0 \\
\end{cases} $

Aligning Equations

Standard LATEX has a eqnarray environment for aligning equations, (see NSSI §3.5), but the amsmath align environment is more convenient. The align environment produces numbered equations, the examples below use align* which leaves equations unnumbered.

Example:

Our first example aligns the $ =$ symbols:
  \begin{align*}
     x &= r \cos \theta \\
     y &= r \sin \theta
  \end{align*}

$\displaystyle x$ $\displaystyle = r \cos \theta$    
$\displaystyle y$ $\displaystyle = r \sin \theta$    

Example:

The following structure is common:
  \begin{align*}
     I &= \int_{0}^{\pi} \sin t  dt \\
       &= \left[- \cos t \right]_{0}^{\pi} \\
       &= - \cos \pi + \cos 0 \\
       &= 2
  \end{align*}

$\displaystyle I$ $\displaystyle = \int_{0}^{\pi} \sin t dt$    
  $\displaystyle = \left[- \cos t \right]_{0}^{\pi}$    
  $\displaystyle = - \cos \pi + \cos 0$    
  $\displaystyle = 2$    

Example:

The \intertext command allows text to interspersed with equations while maintaining the alignment.
  \begin{align*}
     I &= \int_{0}^{\pi} \sin t  dt \\
     \intertext{which is easily integrated}
       &= \left[- \cos t \right]_{0}^{\pi} \\
       &= - \cos \pi + \cos 0 \\
       &= 2
  \end{align*}

$\displaystyle I$ $\displaystyle = \int_{0}^{\pi} \sin t dt$    

which is easily integrated


  $\displaystyle = \left[- \cos t \right]_{0}^{\pi}$    
  $\displaystyle = - \cos \pi + \cos 0$    
  $\displaystyle = 2$    

Example:

The \align environment can also create multiple aligned columns where the ampersand doubles as an alignment point and as a column separator. In this example the first and third ampersands on each line are alignment points while the second ampersand on each line is a column separator.
  \begin{align*}
    \frac{d}{dx} \sin x &= \cos x 
       & \frac{d}{dx} e^x &= e^x \\
    \frac{d}{dx} \cos x &= - \sin x 
       & \frac{d}{dx} \log x &= \frac{1}{x} \\
  \end{align*}

$\displaystyle \frac{d}{dx} \sin x$ $\displaystyle = \cos x$ $\displaystyle \frac{d}{dx} e^x$ $\displaystyle = e^x$    
$\displaystyle \frac{d}{dx} \cos x$ $\displaystyle = - \sin x$ $\displaystyle \frac{d}{dx} \log x$ $\displaystyle = \frac{1}{x}$    

About this document ...

AMTH247 Lecture 4

LATEX -- Alignment

This document was generated using the LaTeX2HTML translator Version 2K.1beta (1.61)

Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999, Ross Moore, Mathematics Department, Macquarie University, Sydney.

The command line arguments were:
latex2html -split +0 lecture.tex

The translation was initiated by amth247 on 2003-03-17


next_inactive up previous
amth247 2003-03-17