A Gentle Introduction to LATEX - Ludwig-Maximilians

advertisement
Ludwig-Maximilians-Universität München
Department of Statistics
A Gentle Introduction to LATEX
(mathematics)
Wolfgang Pößnecker
September 24, 2014
Math Mode
We used it already, but here it is officially:
By far the most common way to enter math mode is
$ ...$
I
The $ command is used for an in-text formulas.
I
The $. . . $ structure is equivalent to \(. . . \) and to
\begin{math}. . . \end{math} environments, e.g.
n2 p + n3 + 7
n2 p + n3 + 7
n2 p + n3 + 7
$ n^2p + n^3 + 7 $
\( n^2p + n^3 + 7 \)
\begin{math} n^2p + n^3 + 7 \end{math}
Displayed Math Mode
Displayed formulas are produced by:
\[ . . . \]
Note the difference between the $ . . . $ and the \[ . . . \]
environments in the example below:
Are determined by:
fm (x)
=
fm−1 (x) + ρ h(x) where ρ is a step
size.
Are determined by:
fm (x) = fm−1 (x) + ρ h(x)
where ρ is a step size.
Are determined by:
$f_m(x) = f_{m-1}(x) + \rho\, h(x)$
where $\rho$ is a step size.
Are determined by:
\[f_m(x) = f_{m-1}(x) + \rho\, h(x)\]
where $\rho$ is a step size.
Displayed Math Mode
Displayed formulas are produced by:
\[ . . . \]
but also by the equivalent environments $$. . . $$ and
\begin{displaymath}. . . \end{displaymath}. The two code
chunks below are equivalent. $$. . . $$ should be avoided as it
produces slightly inconsistent spacing.
Are determined by:
fm (x) = fm−1 (x) + ρ h(x)
where ρ is a step size.
Are determined by:
\[ f_m(x) = f_{m-1}(x) + \rho\, h(x) \]
where $\rho$ is a step size.
Are determined
\begin{displaymath}
f_m(x) = f_{m-1}(x) + \rho\, h(x)
\end{displaymath}
where $\rho$ is a step size.
Numbered Math Mode
Numbered displayed formulas are produced by the environment:
\begin{equation} . . . \end{equation}
Are determined by
fm (x) = fm−1 (x) + ρ h(x)
(1)
where ρ is a step size.
1
Provided by the amsmath package.
Are determined
\begin{equation}
f_m(x) = f_{m-1}(x) + \rho\, h(x)
\end{equation}
where $\rho$ is a step size.
Numbered Math Mode
Numbered displayed formulas are produced by the environment:
\begin{equation} . . . \end{equation}
Are determined by
fm (x) = fm−1 (x) + ρ h(x)
(1)
where ρ is a step size.
Are determined
\begin{equation}
f_m(x) = f_{m-1}(x) + \rho\, h(x)
\label{mytag}
\end{equation}
where $\rho$ is a step size.
I If you want to refer to that equation’s number, use \label to assign a name
and \eqref1 command to refer to that name, e.g. type \eqref{mytag} and it
appears like this: (1)
I \eqref produces the parentheses in (1) as well, which is the recommended
behaviour for equation reference. Use \ref if no parentheses are desired.
I \equation* environment is the same as \equation except that it does not
generate equation number.
1
Provided by the amsmath package.
Numbered Math Mode
Numbered displayed formulas are produced by the environment:
\begin{equation} . . . \end{equation}
Are determined by
fm (x) = fm−1 (x) + ρ h(x)
(1)
where ρ is a step size.
Are determined
\begin{equation}
f_m(x) = f_{m-1}(x) + \rho\, h(x)
\label{mytag}
\end{equation}
where $\rho$ is a step size.
I If you want to refer to that equation’s number, use \label to assign a name
and \eqref1 command to refer to that name, e.g. type \eqref{mytag} and it
appears like this: (1)
amsmath
I \eqref produces the parentheses in (1) as well, which is the recommended
behaviour for equation reference. Use \ref if no parentheses are desired.
I \equation* environment is the same as \equation except that it does not
generate equation number.
1
Provided by the amsmath package.
amsfonts
amsthm
amssymb
The Three Modes
LATEX processes your input in one of the three modes:
I
Paragraph (Text) Mode (Absatzmodus) – LATEX’s normal
mode for ordinary text processing. It regards your input as a
sequence of words and sentences. Automatically breaks
sentences and pages.
I
Math Mode (Mathematischer Modus) – LATEX is in math mode
when it is generating a mathematical formula. When in math
mode, it considers letters as being mathematical symbols.
Therefore, the input $ a l e $ is considered as the product
of a, l and e, and ignores any space characters between them:
ale.
LR Mode (Links-Rechts Modus) – LATEX considers your text to
be a string of words from left to right on a single (infinite
long) unbreakable line:
I
This line is in LR-Mode and this is way it goes beyond the page boundaries and even further. . .
The Three Modes
Notes of caution:
I
You should always be aware in which mode you are in. There
are mode-specific commands, e.g. \alpha in math mode looks
like α, whereas in paragraph mode you get
! Missing $ inserted.
<inserted text>
$
l.7
in text mode \alpha
I
Different modes can be nested within one another. You can
insert text in math mode, for example with \text or \mbox.
I
Declarations are not allowed in math mode, e.g.
${\bfseries 1 + 1 }$ would lead to
! LaTeX Error: Command \bfseries invalid in math mode
I
{\bfseries $1 + 1$ } is not an error, however, with this
example nothing changes.
The Three Modes - Example
Different modes can be nested within one another. You can insert
text in math mode with \text.
That is $\mathbb{E}\,(y_t) = \mu \quad \text{for all $t$,}\quad \mu < \infty$
That is E (yt ) = µ for all t, µ < ∞
1. That is is in paragraph mode.
2.
$\mathbb{E}\,(y_t) = \mu \quad }
is in math mode.
3.
\text{for all
,}
is in LR mode.
$t$
\quad \mu < \infty$
Common Structures
I
Subscripts and Superscripts: _ and ^
x 2a
a
x2
a
x2
! Double superscript.
x 2a
$x^{2a}$
$x^{2^a}$
$x^{2^{a}}$
$x^2^a$
$x^{2_a}$
I NB: In text mode x^{2} leads to
! Missing $ inserted.
<inserted text>
$
l.40 x^
{2}
?
x2a
x2a
! Double subscript.
xa2
xa2
$x_{2a}$
$x_{2_a}$
$x_2_a$
$x^2_a$
$x^{2}_{a}$
Fractions
I
\frac{numerator}{denominator}
(n + p)/m
$(n+p)/m$
n+p
m
$\frac{n+p}{m}$
n+p
m
n+p
1 + x+z
y
n+p
x +z
1+
y
n+p
1+
I
I
x +z
$\dfrac{n+p}{m}$
$\dfrac{n+p}{1+\frac{x+z}{y}}$
$\dfrac{n+p}{1+\dfrac{x+z}{y}}$
$\cfrac{n+p}{1+\cfrac{x+z}{y}}$
y
\dfrac - displaystyle fraction as in \[. . . \] or in $$. . . $$
\cfrac - continued fraction
Square Roots & Integrals
I
\sqrt{number}
√
a+b
√
n
a+b
I
$\sqrt{a+b}$
$\sqrt[n]{a+b}$
\int_{a}^{b}
R∞
a
R∞
2x dx
2x dx
a
$\int_{a}^\infty 2x\,dx$
$\int\limits_{a}^\infty 2x\,dx$
∞
Z
2x dx
$\displaystyle\int_{a}^\infty 2x\,dx$
2x dx
\[\int_{a}^\infty 2x\,dx\]
a
∞
Z
a
Z∞
2x dx
a
\[\int\limits_{a}^\infty 2x\,dx\]
Sums & Products
Note the difference when in displaystyle.
I
\sum_{a}^{b}
\prod_{a}^{b}
Pn
i=1
n
X
Xi
$\sum_{i=1}^n X_i $
Xi
i=1
n
P
\[\sum_{i=1}^n X_i \]
Xi
i=1
X
Xi,j
0<i<m
0<j<n
Qn
i=1
n
Q
i=1
Yi
Yi
$\sum\limits_{i=1}^n X_i$
\[
\sum_{\substack{0<i<m\\ 0<j<n}} X_{i,j}
\]
$\prod_{i=1}^n Y_i$
$\prod\limits_{i=1}^n Y_i$
Spacing in Math Mode
Relative Amount
Command
Description
||
||
||
||
||
| |
|
|
\,
\!
\:
\;
\
\quad
\qquad
thin space
negative thin space
medium space
thick space
interword space
large space
even larger space
I LATEX
does not understand what ydx means. If you want y
times the differential dx you need to type $y\,dx$ in order to
obtain y dx. Simply typing $y dx$ gives you ydx.
Common Structures
I
Over- and Underlining2
7
z }| {
x1 + x2 + · · · + xn−1 + xn
| {z }
$\underbrace{x_1 + x_2}_{7} + \cdots +
\overbrace{x_{n-1} + x_n}^{7}$
7
x1 + x2 + · · · + xn−1 + xn
$\underline{x_1 + x_2} + \cdots +
\overline{x_{n-1} + x_n}$
I
Use \cdots (· · · ) between operators like +,−, and =.
Use \ldots (. . .) between juxtaposed symbols like a . . . z.
.
.
Use \vdots ( .. ) and \ddots ( . . ) in matrices.
I
Stacking symbols and accents
a
∼
def
~
x = (x1 , . . . , xn )
\
1
− x = ŷ
ā
ã
2
$\stackrel{a}{\sim}$
$\vec{x} \stackrel{\text{def}}{=} (x_1,\ldots,x_n)$
$\widehat{1-x} = \hat{y}$
$\bar{a}$
$\tilde{a}$
\underline may be used in text mode too.
Exercise
3
Please find the file 03maths.pdf on the homepage. Try to reproduce the document.
Hints:
1. Begin your document with:
\documentclass[a4paper,12pt]{article}
2. Do not forget to include
\usepackage{amsmath}
\usepackage{amssymb} (required for E)
3. The different sections are produced with \enumerate. Put this in your preamble:
\renewcommand{\labelenumi}{( \alph{enumi} )}
4. Symbols that were not shown on the slides:
\beta
β
\theta
θ
\vartheta
ϑ
\epsilon
\varepsilon
ε
\nu
ν
\Sigma
Σ
\to
→
\lim
lim
\in
∈
\otimes
\partial
’
\neq
\sim
\Rightarrow
\le \ge
\mathbb{E}
\top
⊗
∂
0
6=
∼
⇒
≤≥
E
>
Multiline Formulas
Use \align3 environment for multiline Formulas. Note that this environment is
in math mode, therefore, you do not need to specify $...$ explicitly.
y = β0 + 3x + 7
= 2.5 + 3x + 7
y = β0 + 3x + 7
= 2.5 + 3x + 7
(2)
\begin{align}
y & = \beta_0 + 3x + 7 \\
& = 2.5 + 3x + 7 \notag
\end{align}
\begin{align*}
y & = \beta_0 + 3x +
& = 2.5 + 3x + 7
\end{align*}
7 \\
I Consecutive rows are separated by \\.
I Note the \notag command in the first example. It suppresses the equation
number.
I \align* environment is the same as \align except that it does not generate
equation numbers at all.
3
\align comes with the amsmath package.
Multiline Formulas
You can use whichever symbol you like as a reference point
y = β0 + 3x + 7
(3)
= 2.5 + 3x + 7
(franz)
and then (franz) is shown here as
well.
\begin{align}
y & = \beta_0 + 3x + 7 \\
& = 2.5 + 3x + 7 \label{funlab}
\tag{franz}
\end{align}
and then \eqref{funlab} is shown
here as well.
But remember that \tag{franz} expects franz to be in LR-Mode.
Never leave a blank line before the \end{align}.
\begin{align}
y & = \beta_0 + 3x + 7 \\
Runaway argument?
& = 2.5 + 3x + 7 \label{mybullet}
y & = ... {funlab} \tag {fr\ETC.
\tag{\textbullet}
! Paragraph ended before \align was complete.
\end{align}
What’s wrong with \eqnarray?
Most textbooks recommend \eqnarray for multiline formulas.
Do not use \eqnarray, use \align instead!
Problem 1: Spacing inconsistency
=
whereas
=
whereas
=
=
\begin{minipage}{4cm}
\begin{minipage}{4cm}
\[
\[
\framebox[1cm]{} = \framebox[1cm]{}
\framebox[1cm]{} = \framebox[1cm]{}
\]
\]
whereas
whereas
\begin{align*}
\begin{eqnarray*}
\framebox[1cm]{} &= \framebox[2cm]{}
\framebox[1cm]{} &=& \framebox[2cm]{}
\end{align*}
\end{eqnarray*}
\end{minipage}
\end{minipage}
What’s wrong with \eqnarray?
Most textbooks recommend \eqnarray for multiline formulas.
Do not use \eqnarray, use \align instead!
Problem 2: Overwriting equation numbers4
=
=
(5)
(4)
\begin{minipage}{4cm}
\begin{minipage}{4cm}
\begin{align}
\begin{eqnarray}
\framebox[1cm]{} &= \framebox[2cm]{}
\framebox[1cm]{} &=& \framebox[2cm]{}
\end{align}
\end{eqnarray}
\end{minipage}
\end{minipage}
4
Consider the article by Lars Madsen (Madsen, 2006) for further details.
Array
The \array environment has a single argument that specifies the number of columns
and the alignment of items within these columns: c - center, l - flush left and r flush right.
a+b+c
a+b
a
xy
x +y
x
y
100
72
1
\[
\begin{array}{clr}
a + b +c & xy
a + b
& x + y
a
& \frac{x}{y}
\end{array}
\]
& 100 \\
& 72 \\
& 1
I Adjacent columns are separated by &.
There must be no & after the last item in a row.
I Adjacent rows are separated by \\. The last row is not followed by a \\,
however, it is not a problem if there is one.
I Keep your source code well arranged - increases readability.
I If you do not provide enough alignment-letters, then you’ll see something like
! Extra alignment tab has been changed to ...
Matrix
I Delimiters are parentheses which are big enough to fit around expressions. Put
delimiters around your \array and you get a matrix!
\[
\left(
\begin{array}{ccc}


a + b + c & xy
& 100\\
a+b+c
xy
100


a + b
& x + y
& 72\\
x + y 72 
 a+b
a
& \frac{x}{y}
& 1
x
1
a
y
\end{array}
\right)
\]
I The matrix environment acts like a centered array. You do not bother
providing even the single argument {ccc} of \array{ccc}.

a+b+c

 a+b
a
xy
x +y
x
y

100

72 
1
\[
\left(
\begin{matrix}
a + b + c & xy
a + b
& x + y
a
& \frac{x}{y}
\end{matrix}
\right)
\]
& 100\\
& 72\\
& 1
Further Delimiters
"
#
a+b+c
a+b
xy
x +y
a + b + c
a+b
xy x + y
(
a+b+c
a+b
(
a+b+c
a+b
xy
x +y
\[
\left[
\begin{matrix}
a + b + c & xy
\\
a + b
& x + y \\
\end{matrix}
\right]
\]
% 1
\left|
\right|
% 2
\left\{ ...
\right\}
% 3
\left\{ ...
\right.
% 4
)
xy
x +y
...
I Note the third example. We used \left\{ ...\right\} and not
\left{ ...\right} since {...} belong to the ten special characters!
I Note also the fourth example. When a big left (or right) delimiter is required
with no matching one, the \left and \right command still have to match.
Therefore, type a . after the matching \left or \right!
Changing Style in Math Mode
Change the style only of letters, numbers, and uppercase Greek
letters. Nothing else is affected.
italic + 2 3 α + φ
roman + 23α + φ
bold + 23α + φ
bold + 23α + φ 5
sans serif + 23α + φ
typewriter + 23α + φ
UPPERCASE ON LY
R, N, Q, . . . 6
$\mathit{italic + 2^{3\alpha} + \phi}$
$\mathrm{roman + 2^{3\alpha} + \phi}$
$\mathbf{bold + 2^{3\alpha} + \phi}$
$\mathbf{bold + 2^{3\alpha} + \boldsymbol\phi}$\\
$\mathsf{sans\ serif + 2^{3\alpha} + \phi}$
$\mathtt{typewriter + 2^{3\alpha} + \phi}$
$\mathcal{UPPERCASE\ ONLY}$
$\mathds{R,N,Q,\ldots}$
The \boldmath declaration causes everything in a formula to be bold
boldmath + 23α + φ
5
6
\boldmath{$boldmath + 2^{3\alpha} + \phi$}
You need amsbsy package for the \boldsymbol command.
You need the Doublestroke Font package dsfont for R, N, Q, . . . symbols.
Defining Commands Important!
LATEX provides plenty of commands. But still we do need our own.
I By repeated structures, for example, it is convenient to have our own definitions
through \newcommand.
Let y be a Rn vector and X
be a Rn×m matrix.
\newcommand{\R}{\mathds{R}} % in preamble
...
Let $y$ be a $\R^n$ vector and $X$
be a $\R^{n\times m}$ matrix.
I Common problem: define it in the right mode?
\newcommand{\R}{\mathds{R}} or \newcommand{\R}{$\mathds{R}$}
Solution: \ensuremath
n
Let y be a R vector and X
be a Rn×m matrix and R in
text mode.
\newcommand{\R}{\ensuremath{\mathds{R}}}
...
Let $y$ be a $\R^n$ vector and $X$
be a $\R^{n\times m}$ matrix and
\R in text mode.
Defining Commands Important!
I
We can define arguments as well:
1
exp − 12
2πσ 2
1
√
exp − 12
2πσ 2
√
I
x−µ 2
σ
y −µ 2
σ
\newcommand{\dnorm}[1]
{\dfrac{1}{\sqrt{2\pi\sigma^2}}
\exp\left(-\frac{1}{2}
\left(\frac{#1-\mu}{\sigma}\right)^2
\right)}
$\dnorm{x}$
$\dnorm{y}$
Another common problem: using \newcommand to define a
command that already exists produces an error.
! LaTeX Error: Command \R already defined.
I
If you are absolutely sure that you want this name, use
\renewcommand instead. Do not redefine an existing
command unless you know what you are doing. Otherwise use
another name.
A note of caution: all LATEX commands should contain letters
only, i.e. no numbers or special characters are allowed!
Common Symbols
Art Of Problem Solving:
http://www.artofproblemsolving.com/LaTeX/AoPS_L_GuideSym.php
The Comprehensive LATEX Symbol List:
http://www.ctan.org/tex-archive/info/symbols/comprehensive/symbols-a4.pdf
Find out the name of your symbol:
http://detexify.kirelabs.org/classify.html
Exercise 4
Please find the file 04maths-multiline.pdf on the homepage.
Try to reproduce the document. Hints:
1. Begin your document as in the last exercise.
2. The different sections are produced with \enumerate.
this in your preamble:
\renewcommand{\labelenumi}{\Roman{enumi}}
\times
3. Symbols that were not shown on the slides: \star
\tau
Put
×
?
τ
4. \input is extremely helpful to organize the files of large
projects, use it heavily. See more on:
http://www.weinelt.de/latex/input.html
References
Madsen, L. (2006). Avoid eqnarray!, PracTeX Journal .
URL: http://home.imf.au.dk/daleif/
Download