Scientific Computing 372
LATEX: Section 4
W. H. K. Bester
Department of Mathematical Sciences, Computer Science Division
Last updated: 12 February 2015
Admin
Schedule
1 Introduction and setting text
2 Setting mathematics
3 Standard environments
4 Tables and figures
5 Boxes and new environments
6 AMS-LATEX
7 Beamer and PGF
Arrays
Example (Matrices in mathematics mode)
Use the array environment
Separate columns with &
End rows with \\
The matrix \[ The matrix
\begin{array}{clcr}
a+b+c & uv & x-y & 27 \\ a + b + c uv x−y 27
a+b & u-v & z & 134 \\ a+b u−v z 134
a & 3u\times vw & xyz a 3u × vw xyz 2.978
& 2.978
\end{array} \] is easy to produce.
is easy to produce.
Nested arrays
Example (More with arrays)
Watch this:
\[\frac{
\left[\begin{array}{cc} Watch this:
\left|\begin{array}{cc}
a & b \\ c & d a b
c d 1
\end{array}\right| & 1 \\
Pn 2
2 & \sum_{i=1}^{n}x_{i}^{2} 2 i=1 xi
\end{array}\right]} 1 2 3
{\left| 4 5 6
\begin{array}{ccc} 7 8 9
1 & 2 & 3 \\
4 & 5 & 6 \\ Beautiful, yes?
7 & 8 & 9
\end{array}\right|}\]
Beautiful, yes?
Arrays
Example (Something else with arrays)
Note that \left and \right have to be balanced
Thus, we have
\begin{eqnarray}
Thus, we have
x & = & \left\{
\begin{array}{ll}
3z, z≥0
3z, & z\geq 0 \\ x = z + 3, 0 > z ≥ −5
z+3, & 0>z\geq -5 \\
2z + 8, z < −5
2z+8, & z<-5 \end{array}
\right. \nonumber \\ z2
y = . (1)
y & = & sin z
\frac{z^{2}}{\sin z}. Now, take (1), and . . .
\label{y}
\end{eqnarray}
Now, take (\ref{y}), and
Tables
Example (Tabular data)
Use the tabular environment
\begin{tabular}{|p{2.6cm}||c|r|}
\hline
Subject & Prac & Tut \\ Subject Prac Tut
\hline\hline
Physics 7 3
Physics & 7 & 3 \\
Maths None 10
\hline
Computer Science, 5 5
Maths & None & 10 \\
the best subject in
\cline{1-1}\cline{3-3}
the whole universe,
Computer Science, the best
but then again, I
subject in the whole universe,
am biased
but then again, I am
biased & 5 & 5 \\
\hline
\end{tabular}
Tables
Example (Multicolumn tabular data)
Use \multicolumn{hni}{haligni}{hentryi}, where hni is the
number of columns to spread over
Also note the mischief I get up to with the @{htexti} column
specifier
\begin{center}
\begin{tabular}{|l|l|r@{.}l|}
\hline
\multicolumn{2}{|c}{Item} &
\multicolumn{2}{|c|}{Price} \\ Item Price
\hline Apples & (per dozen) Apples (per dozen) 12.99
& 12 & 99 \\ Onions (each) 0.75
Onions & (each) & 0 & 75 \\
\hline
\end{tabular}
\end{center}
Tables
Column specifiers
l Left-aligned items
r Right-aligned items
c Centred items
@{htexti} Inserts htexti in every row; in math mode when in
an array, left-to-right when in tabular
p{hwi} Produces a justified paragraph box of width hwi
Horizontal lines
\hline draws a line the full width of the environment
\cline{hc1i-hc2i} draws a line from column hc1i to hc2i
The columns are numbered 1, 2, . . .
Tabs
Example (Arbitrarily aligned items)
Use the tabbing environment
Set tabs with \=
Move to next tab with \>
End lines with \\
The environment starts on The environment starts on a new
a new line, as follows. line, as follows.
\begin{tabbing}
When \= it rains, \= then \\ When it rains, then
the road is very,
\> the \> road is very, \\
very wet!
\> \> very wet! \\
\> Don’t slip\ldots. Don’t slip. . . .
\end{tabbing} Afterwards, normal text continues
Afterwards, normal text on a new line.
continues on a new line.
Floating bodies
Figures and other floating bodies
TEX will happily break sentences over pages
Things such as pictures cannot be split
They must be “floated” to convenient places, like the top of a
page, to prevent half-empty pages
Use the figure environment for figures
Use the table environment for tables
Inside a floating environment, use \caption{htexti} for an
automatically numbered caption
The optional arguments h (here), t (top of page), b (bottom of
page) , and p (separate floats page) specifies where the float
may be put
Floating bodies
Rules to determine where a float is put
Placed in the earliest place that does not violate subsequent
rules, except that h takes precedence over t
Will not be printed on an earlier page than the environment
appears
No figure will be printed before an earlier figure; no table
before an earlier table
It may appear only at a position allowed by the optional
arguments; tbp is assumed if the argument is missing
Placement of a float cannot produce an overfull page
Tables
Example (Floating table)
Note that the \label command must go inside or after the
\caption command
Where may the following be put?1
\begin{table}[tb]
\begin{center}
\begin{tabular}{|c|c|}
\hline A silly & little table \\
\hline to illustrate & the point \\
\hline \end{tabular}\end{center}
\caption[Nice table]{A nice little table}
\label{t:nice}
\end{table}
1
top of page, bottom of page
Loose ends
A Table of . . .
\tableofcontents, \listoffigures, \listoftables
The optional argument to \caption gives the text of the entry
in the listof commands
Graphics
Include the package graphicx
Use the \includegraphics command
Text in the margins
Use the \marginpar[hright texti]{hleft texti} command