L TEX Maths and Graphics: A Tim Love May 26, 2004
L TEX Maths and Graphics: A Tim Love May 26, 2004
Tim Love
May 26, 2004
This handout assumes that you have already read the Advanced
LaTeX1 document handout, so if you’re unsure about ‘environments’,
read no further.
Note that there’s an alternative way of producing maths in LATEX -
AMS-LATEX. See the online manual2 for details.
If you want to more more about graphics, see Using Imported Graph-
ics in LaTeX2e Documents3 by Keith Reckdahl.
Comments and bug reports to Tim Love ([email protected]).
Contents
1 Maths 2
1.1 Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 Greek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.2 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.3 Arrows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.4 Calligraphic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.5 Character Modifiers . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.6 Common functions . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Subscripts and superscripts . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Overlining, underlining and bold characters . . . . . . . . . . . . . . 6
1.5 Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Fractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.7 Delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.8 Numbering and labelling . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.9 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.10 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.11 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.12 Fine tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.13 Maths and Postscript fonts . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.14 Matlab and LaTeX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.15 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1 http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/latex advanced/latex advanced.html
2 http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/amslatex.dvi
3 http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/epslatex.ps
c
Copyright
2004 by T.P. Love. This document may be copied freely for the pur-
poses of education and non-commercial research. Cambridge University Engineer-
ing Department, Cambridge CB2 1PZ, England.
1
2 Graphics 12
2.1 Postscript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.1.1 psfrag: adding maths to postscript files . . . . . . . . . . . . 13
2.1.2 Postscript from PCs/Macs . . . . . . . . . . . . . . . . . . . . . 14
2.2 Scaling, rotation, clipping, wrap-around and shadows . . . . . . . . . 14
2.3 GIF and jpeg files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1 Maths
There’s more to maths typesetting than meets the eye. Many conventions used in
the typesetting of plain text are inappropriate to maths. LATEX goes a long way to
help you along with the style. For example, in a LATEX maths environment, letters
come out in italics, ‘-’ as ‘−’ (minus) instead of the usual ‘-’ (dash), ‘*’ becomes ∗,
’ becomes 0 and spacing is changed (less around ‘/’, more around ‘+’).
Many of the usual LATEX constructions can still be used in maths environments
but their effect may be slightly different; eg \textbf{ } only affects letters and
numbers. ‘{’ and ‘}’ are still special characters; they’re used to group characters.
As usual in LATEX you can override the defaults, but think before doing it: maths
support in LATEX has been carefully thought out and is quite logical though the
LATEX source text may not be very readable. It’s a good idea to write out the formu-
lae on paper before you start LATEXing, and try not to overdo the use of the ‘\frac’
construction; use ‘/’ instead.
1.1 Environments
There are 2 environments to display one-line equations.
\begin{equation}
x + iy
\end{equation}
x + iy (1)
\begin{displaymath}
x + iy
\end{displaymath}
x + iy
Never leave a blank line before these equations; it starts a new paragraph and
looks ugly. ’\displaystyle’ is the font type used to print maths in these display
environments. Other relevant environments are:-
math:- For use in text. \( and \) can be used to delimit the environment, as can
the TEX constructions $ and $ . For example, $x=yˆ2$ gives x = y 2 .
eqnarray:- This is like a 3 column tabular environment. Each line by default is
numbered. You can use the eqnarray* variant to suppress numbering alto-
gether.
2
\begin{eqnarray}
a1 & = & b1 + c1\nonumber\\
a2 & = & b2 - c2
\end{eqnarray}
a1 = b1 + c1
a2 = b2 − c2 (2)
Maths in these 2 sorts of environments have different default sizes for some
characters and other behavioural differences so that a line of maths won’t impinge
on text lines below or above. If you want to put some non-maths text in amongst
maths then enclose it in an \mbox{...}.
1.2.1 Greek
1.2.2 Miscellaneous
..
... \ldots ··· \cdots . \vdots
..
. \ddots ± \pm ∓ \mp
× \times ÷ \div ∗ \ast
? \star ◦ \circ •T \bullet
· \cdot ∩
S \cap \bigcap
∪
U \cup \bigcup ] \uplus
F \biguplus u \sqcap t
W \sqcup
\bigsqcup ∨
V \vee \bigvee
∧ \wedge \bigwedge \ \setminus
o \wr \diamond 4 \bigtriangleup
5 \bigtriangledown /L \triangleleft . \triangleright
⊕ \oplus N \bigoplus \ominus
⊗ \otimes J \bigotimes \oslash
\odot \bigodot
\bigcirc
q \amalg ≤ \leq ≺ \prec
\preceq \ll ⊂ \subset
⊆ \subseteq v \sqsubseteq ∈ \in
` \vdash ≥ \geq \succ
\succeq \gg ⊃ \supset
4 http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/symbols.ps
3
⊇ \supseteq w \sqsupseteq 3 \ni
a \dashv ≡ \equiv ∼ \sim
' \simeq \asymp ≈ \approx
∼ .
= \cong 6= \neq = \doteq
∝ \propto |= \models ⊥ \perp
| \mid k \parallel ./ \bowtie
^ \smile _ \frown ℵ \aleph
h̄ \hbar ı \imath \jmath
` \ell ℘ \wp < \Re
= \Im 0√ \prime \empty
∇ \nabla \surd > \top
⊥ \bot k \| 6 \angle
∀ \forall ∃ \exists ¬ \neg
[ \flat \ \natural ] \sharp
\ \backslash ∂
P \partial ∞
Q \infty
4
` \triangle R \sum H \prod
\coprod \int \oint
1.2.3 Arrows
1.2.4 Calligraphic
These characters are available if you use the \cal control sequence.
${\cal A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}$
\hat{e} ê \widehat{easy} d
easy
\tilde{e} ẽ \widetilde{easy} g
easy
\check{e} ě \breve{e} ĕ
\acute{e} é \grave{e} è
\bar{e} ē \vec{e} ~e
\dot{e} ė \ddot{e} ë
\not e 6e
Note that the wide versions of hat and tilde cannot produce very wide alterna-
tives. The ‘\not’ operator hasn’t properly cut the following letter. The Fine Tuning
4
section on page 9 describes how to adjust this.
If you want to place one character above another, you can use \stackrel,
which prints its first argument in small type immediately above the second
$ a \stackrel{def}{=} b + c $
def
gives a = b + c
See the Macros section for how to stack characters using atop.
$F_2ˆ3$
produces ‘F23 ’. Note that the sub- and superscripts aren’t in line. To make them so,
P an invisible character after the ‘F’. $F{}_2ˆ3$ produces F 2 .
you can add 3
$\sum_{i=0}ˆ2 $
P2
produces i=0 (text style) but
\[\sum_{i=0}ˆ2 \]
i=0
This default behaviour can be overridden, if you really need to. For example in
text mode,
$\sum\limits_{i=0}ˆ2$
P
2
produces
i=0
5
1.4 Overlining, underlining and bold characters
$\underline{one} \overline{two}$
produces onetwo. This is not a useful facility if it’s used more than once on a line.
The lines are produced so that they don’t quite overlap the text; lines over or under
different words won’t in general be at the same height.
To be able to reproduce bold maths, it’s best to use the bm package. $E = \bm{mcˆ2}$
produces E = mc2 .
Alternatively, you can use \mathbf{} to create bold characters - $\mathbf{F}_2ˆ3$
produces F32 . or you can use the following idea
\usepackage{amsbsy} % This loads amstext too
\begin{document}
$\omega + \boldsymbol{\omega}$
1.5 Roots
$\sqrt{4} + \sqrt[3]{x + y}$
√ √
gives 4 + 3 x + y.
1.6 Fractions
Three constructions for putting expressions above others are
frac:- $\frac{1}{(x + 3)}$ produces (x+3) .
1
choose:- ${n + 1 \choose 3}$ produces n+1
3 .
atop:- ${x \atop y}$ produces xy .
These constructions can be used with ones described earlier. E.g.,
\[ \sum_{-1\le i \le 1 \atop 0 < j < \infty} f(i,j)\]
gives X
f (i, j)
−1≤i≤1
0<j<∞
1.7 Delimiters
these are made by these and these are made by these
( ( ) )
[ [ ] ]
{ \{ } \}
b \lfloor c \rfloor
d \lceil e \rceil
h \langle i \rangle
/ / \ \backslash
| | k \|
↑ \uparrow ⇑ \Uparrow
↓ \downarrow ⇓ \Downarrow
l \updownarrow m \Updownarrow
6
This table shows the standard sizes. To get bigger sizes, use these prefices
(for left delimiters) (for right delimiters) magnification
\bigl \bigr a bit bigger, but won’t overlap lines
\Bigl \Bigr 150% times big
\biggl \biggr 200% times big
\Biggl \Biggr 250% times big
For example,
$\Biggl\{2\Bigl(x(3+y)\Bigr)\Biggr\}$
( )
gives 2 x(3 + y) . If you’re not using the default text size these commands
might not work correctly. In that case try the exscale package.
It’s preferable to let LATEX choose the delimiter size for you by using \left
and \right. These will produce delimiters just big enough for the formulae inbe-
tween.
\[ z = \left\{
\begin{array}{ll}
1 & (x>0)\\
0 & (x<0)
\end{array}
\right.
\]
produces
1 (x > 0)
z=
0 (x < 0)
7
\begin{equation}
W_{\bf S}(t,\omega) = \int\limits_{-\infty}ˆ{\infty} {
{\cal R}_{\bf S}(t,\tau) eˆ{-j\omega\tau} \,d \tau }
\label{LABELLING}
\end{equation}
Z∞
WS (t, ω) = RS (t, τ )e−jωτ dτ (3)
−∞
Now the following text
refers back to equation \ref{LABELLING}
refers back to equation 3 by number, and
refers back to the equation on page \pageref{LABELLING}
refers back to the equation on page 8.
A file will have to be LATEX’ed twice before the references, both forwards and
backwards, will be correctly produced.
1.9 Matrices
The array environment is like LATEX’s tabular environment except that each el-
ement is in math mode. The number and alignment of columns is controlled by
the arguments - use l, c or r to represent each column with either left, center or
right alignment. The default font style used is \textstyle but you can override
this by changing the \displaystyle.
\begin{math}
\begin{array}{clrr} %
a+b+c & uv & x-y & 27 \\
x+y & w & +z & 363
\end{array}
\end{math}
a + b + c uv x − y 27
produces
x+y w +z 363
The rows are arranged so that their centres are aligned. You can align their tops
or bottoms instead by using a further argument when you create the array.
\begin{array}{clrr}[t]
would produce top-aligned lines, and ‘[b]’ would produce bottom-aligned ones.
The Delimiters section of this document shows how to bracket matrices.
TEX has a few maths facilities not mentioned in the LATEX book. The following
TEX construction might be useful.
\begin{math}
\bordermatrix{&a_1&a_2&...&a_n\cr
b_1 & 1.2 & 3.3 & 5.1 & 2.8 \cr
c_1 & 4.7 & 7.8 & 2.4 & 1.9 \cr
... & ... & ... & ... & ... \cr
z_1 & 8.0 & 9.9 & 0.9 & 9.99 \cr}
\end{math}
a a2 ... an
1
b1 1.2 3.3 5.1 2.8
c1 4.7 7.8 2.4 1.9
... ... ... ... ...
z1 8.0 9.9 0.9 9.99
8
1.10 Macros
These aid readability, save on repetitive typing and offer ways of producing stylis-
tic variations on standard LATEX formats.
\def\bydefn{\stackrel{def}{=}}
\def\convf{\hbox{\space \raise-2mm\hbox{$\textstyle
\bigotimes \atop \scriptstyle \omega$} \space}}
def N
produce = and when $\bydefn$ and $\convf$ are typed.
ω
1.11 Packages
The following packages may be of help
• easybmat5 - easy block matrices
9
to give
y = a+b
+k (4)
but the spacing around the ‘+’ on the 2nd line is wrong because LATEX thinks it’s a
unary operator. You can fool LATEX into treating it as a binary operator by inserting
a hidden character.
y & = & a + b \nonumber \\
& & \mbox{} + k
gives
y = a+b
+k (5)
You can use the \lefteqn construction to format long expressions so that con-
tinuation lines are differently indented.
\begin{eqnarray}
\lefteqn{x+ iy=}\\
& & a + b + c + d + e + f + g + h + i + j + k +\nonumber\\
& & l + m \nonumber
\end{eqnarray}
x + iy = (6)
a+b+c+d+e+f +g+h+i+j+k+
l+m
If you want more vertical spacing around a line you can create an invisible
vertical ”struct” in LaTeX. \rule[-.3cm]{0cm}{1cm} creates a box of width 0,
height 1cm which starts .3cm below the usual line base. By adjusting these values
you should be able to create as much extra space below/above the maths as you
like. “ B
A
and” is created by
$A \over B$ \rule[-.3cm]{0cm}{1cm}{and}
10
latex(’(sin(x)+2*x+3*xˆ2)/(5*x+6*xˆ2)’,’math.tex’)
puts the LaTeX representation of the expression into a file called math.tex. Type
“help latex" inside matlab for details.
1.15 Examples
• \begin{equation}
\hat{\theta}_{w_i} = \hat{\theta}(s(t,{\cal U}_{w_i})).
\end{equation}
gives
θ̂wi = θ̂(s(t, Uwi )). (7)
• \begin{eqnarray}
{\cal M}ˆ2(\hat{\theta},\theta) &=& E[(\hat{\theta} - \theta)ˆ2]
\nonumber \\
{\cal M}ˆ2(\hat{\theta},\theta) &=& {\rm var}ˆ2(\hat{\theta}) +
{\cal B}ˆ2(\hat{\theta}).
\end{eqnarray}
gives
• \begin{equation}
\hat{W}_{s}(t,\omega;\phi) \bydefn
\int\limits_{-\infty}ˆ{\infty}
{\hat{\cal R}_s(t,\tau;\psi) eˆ{-j\omega \tau}
\, d \tau }
\end{equation}
gives
Z∞
def
Ŵs (t, ω; φ) = R̂s (t, τ ; ψ)e−jωτ dτ (9)
−∞
• \begin{eqnarray}
{\cal B}(t,\omega) & \approx &
{1 \over 4\pi}
{\cal D}_tˆ2 W_{\bf S}(t, \omega)
{{{\scriptstyle \infty} \atop
{\displaystyle \int \! \int \!
}}\atop {\scriptstyle -\infty}}
t_1ˆ2
\phi(t_1,\omega_1) \, dt_1 d\omega_1
\nonumber \\
&& +
{1 \over 4\pi}
{\cal D}_\omegaˆ2 W_{\bf S}(t, \omega)
{{{\scriptstyle \infty} \atop
{\displaystyle \int \! \int \!
}}\atop {\scriptstyle -\infty}}
\omega_1ˆ2
\phi(t_1,\omega_1) \, dt_1 \, d\omega_1.
11
\label{F4}
\end{eqnarray}
gives
Z∞Z
1 2
B(t, ω) ≈ D WS (t, ω) t21 φ(t1 , ω1 ) dt1 dω1
4π t −∞
Z∞Z
1 2
+ D WS (t, ω) ω12 φ(t1 , ω1 ) dt1 dω1 . (10)
4π ω −∞
• \newsavebox{\DERIVBOXZLM}
\savebox{\DERIVBOXZLM}[2.5em]{$\Longrightarrow\hspace{-1.5em}
\raisebox{.2ex}{*}
\hspace{-.7em}\raisebox{-.8ex}{\scriptsize lm}\hspace{.7em}$}
\newcommand{\Deriveszlm}{\usebox{\DERIVBOXZLM}}
\Deriveszlm
gives
*
=⇒
lm
2 Graphics
LATEX has a picture environment in which pictures can be drawn, but you’ll find
graph paper handy. xfig can create code for the picture environment but the
resulting graphics still suffer several limitations: only certain slopes and circles
can be reproduced. The best method presently available on Unix is to use xfig to
produce postscript files, which have no such limitations, but require a postscript
printer or equivalent.
Whatever graphics you want to add, you should use the figure environment
so that LATEX can cope sensibly with situations where, for example, you attempt to
insert near the bottom of a page a graphic that’s half a page high. The figure
environment will float the graphic to the top or bottom of the page, or on the next
page, with preferences that you can provide.
h here
t top of page
b bottom of page
p on a page with no text
Putting ! as the first argument in the square brackets will encourage LATEX to
do what you say, even if the result’s sub-optimal. See the online hints about floats
in LaTeX14 for further details.
\begin{figure}[htbp]
\vspace{0.5in}
\caption{0.5 inch of space}
\end{figure}
It’s possible to have more than one graphic in a figure. See the example later on.
14 http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/float hint.html
12
Figure 1: 0.5 inch of space
2.1 Postscript
pdflatex supports JPEG, PNG andPDF images - but not postscript. latex sup-
ports Postscript files as long as they have a proper bounding box comment; i.e.
LATEX requires full Encapsulated Postscript15 as produced by (for example) xv and
xfig on Unix. If the file hasn’t got a BoundingBox line near the top, you can use
ps2epsi to generate one. Wherever the postscript file comes from, simply use
\documentclass[dvips]{article}
\usepackage{graphicx}
13
would display the file with MATHS replaced by x2 . See the online documentation18
for details.
Figure 2: Tigers
18 http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/psfrag.ps
14
\centering
\begin{figure}[hbtp]
\includegraphics[height=40mm]{/export/ghostfonts/tiger.eps}
\includegraphics[angle=120, height=20mm]{/export/ghostfonts/tiger.eps}
\caption{Tigers}
\end{figure}
Figure 3: 3 crests
To clip the postscript image use the viewpoint argument. The following frag-
ment would display only part of the image. The viewport coordinates are in the
same units as the bounding box.
\begin{figure}[htbp]
\includegraphics[viewport=200 400 400 600,width=5cm,clip]%
{CUniv3.eps}
\end{figure}
The floatflt19 package lets you insert a graphic and have the text wrap
around it. You can provide 2 arguments to the floatingfigure command: the
first (l or r) selects whether you want the graphic to be on the left or right of the
19 http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/floatflt.dvi
15
page. The 2nd argument gives the width of the graphic. Not all text will flow per-
fectly around (for example, verbatim text fails, as illustrated below) so check the
final output carefully.
Using the fancybox package gives you access to
\shadowbox, \ovalbox, \Ovalbox and \doublebox
commands, which can be used with text or with graph-
ics. For example, \shadowbox{shadow package}
shadow package
produces and
\ovalbox{\includegraphics[height=10mm]{CUni3.eps}}
Figure 4: Using floating-
figure
produces . Unfortu-
nately, the fancybox package as supplied suppresses
the table of contents. The locally produced contents-
fancybox solves this, but may introduce graphics
problems.
16