IntroToLaTeXpart1-version1 5 3
IntroToLaTeXpart1-version1 5 3
Version 1.5.3
David Goulette
April, 23 2014
Abstract
This is where a brief summary of your paper would go. You should write something that
is direct and to the point so that an expert in the field could read it in about a minute and
know exactly what the article covers and what the key results are. When I say “expert,”
I mean, say, a university professor with research level experience in the area. Abstracts
are often hard to fully understand if you are not an expert. But this abstract that you are
reading now is the worst ever! It has nothing to do with this document... o.k. I mostly
just threw this in to show you how to make an abstract in LATEX. It uses the abstract
environment, which does all of the formatting for you automatically! By-the-way, if you
don’t know what an “environment” is, don’t worry. I will explain what a LATEX environment
is in section 4. After you finish that section, come back and look at the code that created
this abstract in the .tex file again. It will make sense.
Contents
1 Before you start reading! 3
4 Environments 8
5 Packages 9
1
6.8.1 Unexplained Math Examples . . . . . . . . . . . . . . . . . . . . . . . . . 16
6.9 Issues with common math symbols, scripts, and math variations. . . . . . . . . . 18
6.10 Examples of some basic math script styles and symbols . . . . . . . . . . . . . . 19
8 More mathematics 32
8.1 Variable sized math grouping symbols . . . . . . . . . . . . . . . . . . . . . . . . 32
8.1.1 \left and \right . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
8.1.2 \big, \Big, \bigg, and \Bigg . . . . . . . . . . . . . . . . . . . . . . . . . 34
8.2 Fun ways to use invisible stuff with {} or \phantom{} . . . . . . . . . . . . . . . 36
8.3 Inserting text inside of math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
8.4 Under-stuff and over-stuff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
8.5 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2
Notice that LATEX automatically indented this paragraph and it isn’t in the abstract envi-
ronment any more. That is the default behavior for normal paragraphs. But you will see below
that the first line after a section heading is not indented. That is just common style in technical
articles so that is the default behavior when you choose \documentclass{article}. There are
always ways to override these defaults if you really want to do that.
http://tobi.oetiker.ch/lshort/lshort-letter.pdf
which is extremely well written and I highly recommend you read. And my humble introduction
to LATEX will never be as comprehensive as the wikibook,
http://en.wikibooks.org/wiki/Latex
which will be a fantastic resource for you once you know the basics. But my introduction differs
from others in one important way. I have essentially written this as two parallel introductions
to LATEX. I have made available the .tex file AND the .pdf file that it generates so you can read
them side-by-side. In fact, if you don’t have both, you will miss out on a lot of what I am doing
here. I have heavily commented the .tex file with explanations which you won’t see in the final
document. And I have done some things in the .tex file that will look confusing until you see the
final .pdf document and then it will make sense. And I have done a few fancy things that you
might have to look back and forth a few times to figure it out. I feel the best way to learn LATEX
(and any programming language for that matter) is to see good examples and start coding your
own stuff as soon as possible. So if you are currently reading the .pdf version of this right now
and you don’t have the .tex file that created this, stop right now and go get it!1
http://www.sjsu.edu/people/david.goulette/courses/latex/
Also double check that the version number of both files is the same (if not, re-download both
of them). I will update the version number if I make any changes. I suggest you watch my first
three video lessons on YouTube that explain what LATEX is, how compiling works and also what
files are created. So here goes!
O.k... so you don’t need “Hello World!” You can type whatever you want. And also there
are other choices other than article. The function \documentclass{article} declares what
type of a document it is. The pair of functions \begin{document} and \end{document} are the
beginning and end of the document. Now that you have the most basic document lets discuss
what you can put in the document.
1 If you are reading an old version of this and the link to my website is dead, then I may have moved on to a
new place in life. Just Google my name, throw in the word LaTeX, and see what happens.
3
3 Basic stuff in normal text mode
3.1 Comments on commenting
If you read the .tex file you will see many lines in the code that begin with a percent character,
%. Any text you type in a .tex file that comes after a % will be ignored by LATEX. For example,
if you type this in your .tex file:
then, when you compile your document, you will see this in the final output:
I made 10
Note that the percent sign and everything after it were ignored by LATEX.2 Of course there will
be times when you want an actual percent sign in your final document! Don’t worry, I will show
you how to do this in section 3.5 below.
As I mentioned above, I have many comments in the .tex file that you should read. I
especially have a lot towards the beginning of the .tex file. Don’t skip over these comments
because they are a part of the lesson here. For example, I explain a lot about how to add
packages in the .tex file because it makes more sense to explain it there. If you don’t know
what a package is don’t worry. I am going to explain what they are later in section ??. You
will find, for example, that my discussion of packages that you see in the .pdf file is more basic
and introductory in nature, but the comments in the .tex file are more specific and detailed.
So read both.
you compile and you are not sure what line is causing the problem, try commenting out various lines you suspect
might be causing the problem and see if it compiles then. This will help you narrow down your search for the
error.
4
Two carriage returns gives you a new line that’s indented. (“Carriage return” means a new
line in the .tex file, that is, <enter><enter>.)
Use \noindent to force LATEX to not indent like this line. Note that anything in your .tex file
that starts with a \ will be interpreted as a command by LATEX. So typing \noindent in your
.tex file is a command to LATEX that tells it not to indent the line that follows.
To force a page break use: \newpage
5
3.3 Accents and quotation marks
There are lots of examples of accent marks. Here are a few: San José State, la forêt enchantée,
Möbius, and Peña. Be sure to use {} around the letter that you are accenting. For example,
Hungarian has the letter o with a double acute accent over the top like this: ő. You get this
accent mark over the o by typing \H{o}. So if you type, Paul Erd\H{o}s, you will get: Paul
Erdős. If you leave out the {} and only type Erd\Hos, you will get an error because LATEX will
be confused about how to interpret it; is the function \H? or \Ho? or \Hos? When you use {}
you make it clear where the name of the function ends and what the input to the function is
(and LATEX likes that).
If you want to use quotes you can’t do "word" because you will get: ”word”
Note the wrong direction for the first quotation mark. Instead you need to do press the single
quotes that point in the opposite direction like this: ‘‘word’’, to get this: “word.” Here is
a case where you really have to look at the .tex file to see what I mean because it is hard to
explain in words but easy to see in the code. ,
3.4 Text sizes and the scope of a command
Some commands in LATEX have the effect of changing the behavior of LATEX from that point
forward. If I use the \tiny command, then all text after that command will be tiny. This can
cause errors because you might have other environments (or math) later on in the document
that cannot be “tiny.” So you usually limit the scope of the command with {}.
If you type this in your .tex file:
This is normal size, {\tiny this is tiny,} and now we are back to normal.
The % is no longer interpreted as the beginning of a comment in the code so we get what we
intended to get.
Tilde is a complicated case because it can be done a few ways with different results: ˜ and
~, are two choices. (But how often do you want a floating tilde anyway?)
Here is a fun example. If you type < or > in your .tex file you will get ¡ and ¿ by default.
¿Cuándo es tu cumpleaños?... ¡Hoy es mi cumpleaños!
6
There are MUCH better ways to handle characters from languages other than English using
the babel package, but this is a hack for some quick Spanish. Note that < and > aren’t really
special function characters. (Well,... they are not special when you are in text mode like we
are now.) But I am pointing out the fact that they don’t create the output you might expect.
(Later in section 6, we will be discussing math mode where < and > will mean “less than” and
“greater than”... but that is only in math mode and we are not there yet.) When you are in
normal text mode, if you want to get < and > you need to type the commands \textless and
\textgreater.
Finally if you type the | character in your code you will get a small horizontal line — like
that. And you can do a bunch of them in a row to create a line. Like this:
—————————
This is useful for adding a horizontal dividing line. (Obviously, David...) I have some later in
this document to divide up consecutive examples.
You can combine these script styles with sizing commands like, say, very large small caps:
Large small caps. (That seems contradictory! But it isn’t. “Large” is the size
of the script and “small caps” is the name of the script style.)
And you can combine some of the script styles like:
This is bold underlined italics!
But some won’t combine. You can’t have italic small caps, for example.
Let me emphasize that all of the script variations you see above are variations on the Com-
puter Modern font. We will see the math script style for Computer Modern below.
The function \emph{} has different behavior than \textit{}. \emph{} is used to emphasize
something and it changes regular font to italics or italics to regular font depending on what is
the current style (\textit{} just forces italics). Compare this:
Here is a case where
I want to emphasize this point. you really need to
to this: look in the .tex file to
I want to emphasize this point. fully understand this
example.
If you want to type a short bit of text that comes out exactly like you type it including
any special characters, then you use the function \verb|***|. In the verbatim environment,
LATEX will ignore the meaning of special characters and just give you exactly what you type in
a typewriter script. I use this for typesetting a short block of computer programming code and
I also use the verbatim function for my students when I am typing up instructions on how to
use a calculator or learn LATEX!3 Now, \verb is a unique function because the delimiters for
3 For long sections of code I actually prefer to use the verbatim environment. I will explain environments later
in this document.
7
the argument are not {} in this case. The reason is because, well... what if you also want the
characters { or } in your verbatim text?... LATEX would get confused. With \verb, you can
actually use any normal character as a delimiter. I use the symbol | in general just because I like
it. That is, unless I want a | in my verbatim text (like the four I already have in this paragraph!)
which means I would need to use + or something else. This is a case where examples are clearer
than explanations.
Notice we got everything between the + + this time. I had to switch to + because I had a | in
my text.
Everything inside of \verb|stuff| has to be on the same line. You will get errors if you
press <enter> in the middle of your verbatim text and try to compile it.
\emph{Note that when you have a long verbatim line, then you have to split
the line manually and force a new line in your document. \LaTeX\ will not do
any formatting with verbatim text. Notice how this will run right into the margin if I let it!!!}
Always remember that LATEX will do exactly what it is told to do! And sometimes that is
not what you want to happen. So you have to fix it.
4 Environments
Environments are basically anything that looks like this:
\begin{environment name}
junk in the middle
\end{environment name}
(There is actually one important exception to this that you will see below in math mode that I
will show you in section 6.3.) So this document itself is an environment because it starts with
\begin{document} and ends with \end{document}. I used the verbatim environment to make
the example before this paragraph. The verbatim environment is an alternative to the \verb
function I explained earlier but it has essentially the same results. The abstract at the beginning
of this document is an environment. Here are just a few more useful examples (there are lots
more than what I have here):
This is the center environment.
This is the
flushright
environment.
8
Here is a long quote using the quote environment. It does the formatting for you! This is from
a really good book:
Alice was beginning to get very tired of sitting by her sister on the bank, and of
having nothing to do: once or twice she had peeped into the book her sister was
reading, but it had no pictures or conversations in it, ‘and what is the use of a book,’
thought Alice, ‘without pictures or conversations?’
So she was considering in her own mind (as well as she could, for the hot day made
her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would
be worth the trouble of getting up and picking the daisies, when suddenly a White
Rabbit with pink eyes ran close by her.
Bread
Cheese
Fruit
– Oranges
– Apples
* Fuji
* Granny Smith
– Kiwi
Coffee
As you can see it creates multi-level bullet points. My example here is just the default itemized
list. There is a very cool package called enumerate that you can add on to your LATEX document
which makes bullet point lists and enumerated lists very customizable. But wait!..., now that I
mention it, I realize that I just mentioned something called a “package” which I haven’t explained
to you yet! So here goes.
5 Packages
A package is an add-on to the standard LATEX. New packages are added in the preamble to the
document. The preamble is everything between \documentclass{classname} and \begin{document}. ⇐ Oops!
So, when you look at a .tex file, this is where the preamble is:
\documentclass{book}
**PREAMBLE STARTS HERE**
*
*
*
**PREAMBLE ENDS HERE**
\begin{document}
Call me Ishmael.... etc.
9
The preamble is the place where you set global parameters for the document (like title, author,
and date), create new commands, set the options you want (like pagination style, header and
footer style), etc. etc.
An example of a package that I have used in this document is the geometry package, which
makes changing the margins much simpler than with the standard LATEX methods. The general
syntax for adding a package is: \usepackage{package name goes here} (See the preamble
to the .tex file for my extensive commentary on packages.) I have also added the marginnote Here is a margin note
package which I really like for making notes in the margin (obviously). Margin notes can be where you can put
useful when working on a single document with a group of people or when you are proofreading text! I set the
someone’s paper and you want to make comments. I am also using a package that lets me use marginpar width to
color in this document as well. See the comments in the preamble to this .tex document to read be wider than normal
in the preamble using
about the awesome package called hyperref. It is the reason why all of the section references
the geometry package
in this file are live links. Also, if you click on a url in this document it should open up your so that I had room for
favorite browser and take you straight to the web site. And hyperref is also why you can see this. See my
a table of contents on the left hand side of the window if you are using Adobe Reader or any comments in the
similar viewer to read this .pdf file. If you are reading this document in the .pdf viewer included preamble to the .tex
with your .tex editor, then you probably won’t see the table of contents on the left. So open for details.
this file up in Adobe Reader and check it out!
The truth is that, technically, many of the things that packages help you do are actually
possible without the packages. But the packages make things MUCH easier. There are thousands
of packages that people have made. Most are free. If you are using MiXTeX in Windows, you
have a package manager that will show you a list of possible packages and the ones you have
installed. You can probably find it under:
Start ==> All Programs ==> MiXTeX ==> Maintenance ==> Package Manager
If you are on a Mac or using Linux, you are on your own. , Oh yeah... one last thing here, if
you add a package in your preamble that you don’t have currently installed, NO PROBLEM!
MiKTeX will install it for you on the fly when you compile. So you don’t actually have to use
the package manager very often.
10
also centers it. With display mode you get this:
x2 − y
Φ(x, y) = ω .
ζ
f (x)dx
In the displayed versions you can actually read the Greek letter zeta as the lower limit of the
integral. So, in general, larger or longer equations should be done in display mode. But you
have the control so it is ultimately your choice.
11
you will get the displayed equation with no automatic numbering:
f (x + h) − f (x)
f ′ (x) = lim .
h→0 h
Using \[ \] to have displayed equations is an example of an environment that doesn’t use
\begin{} and \end{}. Now, a word of warning: you will see old .tex files that will display
unnumbered equations with double dollar signs $$ $$ instead of \[ \]. This practice is out of
date and not advised. It is a relic of old versions of TEX and I have read that it will sometimes
cause errors with recent packages. So just don’t do it. But in case you see it in other people’s
work, you will know why.
If you want LATEX to number the equation for you, just use this instead:
\begin{equation}
f’(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}
\end{equation}
and you will get the same thing we got before but numbered so you can reference it later:
f (x + h) − f (x)
f ′ (x) = lim . (1)
h→0 h
ax+b ax + b
Compare cx+d to
cx + d
The problem with normal display math environments is that they usually do a lot of formatting
and sometimes you don’t want that. They often force the equation to be centered on the page
and they sometimes add extra spacing above and below the equation as well. Sometimes you
want display math style but you want it in a very particular spot. It comes up a lot when you
are formatting a table where some regular text needs to be lined up with some display math
side by side. See for example the table that I made on page 35. The second row of that table
has display math that is lined up with regular text. Using the \displaystyle command is the
easiest way to handle that situation. Let’s have an example in this section. Let’s say you are
really into electromagnetism and you want a sentence like this:
1
My favorite equation in the whole world is: E · dS = ρ dV
∂Ω ε0 Ω
Clearly you want the equation to be placed right after the colon in this situation. And if that
really was your favorite equation, it would be a shame to make it small. The \displaystyle
function is just what you need here.
Just in case that is your favorite equation and you want to see the code for that last example,
here it is but try not to be intimidated by it if it looks confusing. This example requires the
AMS packages to be included in the preamble which I will explain in section 6.5 and you also
need the esint package to get the cool double integral. I also use math bold, math roman, and
some symbols which I will explain later in section 6.9 as well as a spacing command that I will
explain in section 7. But here it is anyway:
12
$\displaystyle
\oiint_{\partial \Omega}\mathrm{\mathbf{E}\cdot d\mathbf{S}}
=
\frac{1}{\varepsilon_{0}}\iiint_{\Omega}\rho\, \mathrm{d}V$
In case you are wondering, my favorite equation is: eiπ + 1 = 0. Mind. Blown. (How is it The 5 most important
possible that a transcendental number, e, can be raised to the power of another transcendental numbers in all of
mathematics are
number, π, multiplied by the complex imaginary unit, i, and somehow that equals the integer contained in this one
-1! I know proofs of this equation... but still...) This beautiful equation happens to look just truly amazing equation.
fine in normal in-line mode. ,
6.5 A.M.S. packages
Before I go on to show you a bunch of math examples I have to mention the American Mathe-
matical Society packages. These are important expansions of the basic LATEX math capability.
They add many symbols, constructs, and environments that are not available with basic LATEX.
The in-line matrix I made earlier used the bmatrix environment from the amsmath package. See
the preamble to the .tex file for more information. Many of the examples below require these
extra packages.
\begin{align}
ax+b &= \mu-\theta\\
&\leq \eta+42\\
&< \epsilon.
\end{align}
ax + b = µ − θ (2)
≤ η + 42 (3)
< ϵ. (4)
You need to use the \\ command to get each line of math on a new line. But more importantly,
notice the use of & in the code for this example and the results. LATEX will align the lines based
on where I placed the &, so I chose to put them just to the left of =, ≤, and < so they line up
perfectly. I have found that putting them to the left of the relation symbols works well.
I often use align more like the following example where I am showing steps and I don’t want
to number everything. To get the unnumbered version of most environments just add a star
(a.k.a. asterisk) to the command like this:
\begin{align*}
\frac{d}{dx}(x^2+2x+1)^2 &= 2\cdot(x^2+2x+1)\cdot(2x+2)\\
&= (2x^2+4x+2)\cdot(2x+2)\\
&= 4x^3+8x^2+4x+4x^2+8x+4\\
&= 4x^3+12x^2+12x+4.
\end{align*}
13
And you will get this:
d 2
(x + 2x + 1)2 = 2 · (x2 + 2x + 1) · (2x + 2)
dx
= (2x2 + 4x + 2) · (2x + 2)
= 4x3 + 8x2 + 4x + 4x2 + 8x + 4
= 4x3 + 12x2 + 12x + 4.
2(x + 4) = 4x − 2
2x + 8 = 4x − 2
10 = 2x
5 = x.
You have to put {} around your exponents and subscripts if they are longer than one character.
So if you type
Notice that I don’t need the brackets around the subscript and superscript on z because they
are only one character long. It turns out that if you want a subscript that is, say, a Greek letter,
then you don’t need to use brackets even though the command for a Greek letter is more than
one character long. LATEX treats the \alpha in the next example as one object:
But I recommend you get in the habit of using brackets like this:
This “good habit” will help you avoid errors in other circumstances.
Using brackets is also important when a LATEX function has an alternate subscript-superscript
behavior. For example, the \sum function will give you an alternate behavior when you do
subscripts and superscripts. This is because we tend to write the indices of a summation directly
above and below the summation symbol. But the syntax is the same as the examples in the
previous paragraph. So if you type
\[ Notice how important
\sum_{i=0}^n i=\frac{n(n+1)}{2} it is that I put {}
\] around the i = 0 in
the code so that all
you get three characters went
n
X n(n + 1) below the summation
i= . sign.
i=0
2
14
Compare to the behavior here:
n
$F_{i=0}^n$ which gives you this: Fi=0
b b b
[ b
\ b
Y b
a b
G b
M b
O
a a a a a a a a
a
By-the-way, note that \sum and \Sigma are not the same thing! Sure, they are the same capital
Greek letter, but the first will look and behave differently than the second because the first one
is a LATEX math command and the second one is a math text symbol (which I will explain in
more detail in section 6.9). Here we have \sum_i^n on the left and \Sigma_i^n on the right.
n
X
̸= Σni
i
LATEX has many already built-in functions to make the most common math constructs. You
have already seen some but I want to emphasize here what can happen when you don’t use
them. For example, if you type this
which looks bad because LATEX writes the letters in sin and tan like they are variables. You
see, LATEX doesn’t know that you want the sine and tangent function. It just knows that you
are in math mode and you typed some letters. So it treats sin and tan like variables that are
being multiplied. Instead you want to use the built in functions for sine, \sin, and tangent,
\tan. These functions make the letters in Roman (as they should be) in order to differentiate
them from variables. Also these built in functions tend to space things better. So if you type this
which looks correct. All of the trig and inverse trig functions have corresponding LATEX functions,
as well as log, mod, max, min, and many other common constructs. Make sure you use the built
in math commands that are available in LATEX.
15
6.8.1 Unexplained Math Examples
β n p
x2 + sin y X
f (x, y) = dxdy + x2i + y
α ln x i=0
injective
f : X −−−−−→ Y
T
b
f (x)dx = F (a) − F (b)
a
p
For, x ∈ A and y1 ∈ B, δ ± x2 − y1 = log2 (ϕ).
a b
≮ ℵ0
c d
n
X n i n−i
n
(x + y) = xy
i=0
i
∂ 2
x cos(y) = −x2 sin(y)
∂y
2x 5z
· ≈ 2.73 × 10−5
z 2w
12 ≡ 2 (mod 5)
You can have the limits of integration on the right side of the integral sign like this:
π 2π 1
4 3
ρ2 sin(ϕ) dρ dθ dϕ = πr
0 0 0 3
Or you can have the limits above and below the integral sign like this:
2π π
g(θ, ϕ) dθ dϕ ←Some people prefer
to have the differential
0 π/6
operators in roman like
this instead of slanted
math style.
f (x, y, z) dx dy dz
f (y1 , y2 , . . . , yn ) dV
n
Pn
a11 a12 ··· a1n x1 i=1 a1i xi
.. .. x Pn
a21
. . · . = i=1 a2i xi
2
. .. .. .. ..
..
. . P .
n
an1 ··· ··· ann xn a
i=1 ni ix
(
0 if r − j is odd,
Pr−j = (5)
r! (−1)(r−j)/2 if r − j is even.
16
1
√ 1
2+
√ 1
2+ √
2 + ···
X
n Cm
0≤i≤m
0≤j≤n
A = {x + iy ∈ C : x ∈ Z and y ≥ 0}
iθ
e = cos(θ) + i sin(θ)
Cool equations from
1 f (z)
complex analysis. f (a) =
dz
2πi γ z − a
d|n := “d is a divisor of n”
———————————
Let’s solve x2 + 5x + 4 = 0 by factoring.
x2 + 5x + 4 = 0
(x + 4)(x + 1) = 0
x+4=0 or x + 1 = 0,
so
x = −4 and x = −1
are solutions.
——————————
For the chemistry fans, here are some isotopes:
14 238 16 2−
6C 92 U 8O
17
6.9 Issues with common math symbols, scripts, and math variations.
As we saw earlier in section 3.6, there are a variety of script styles available when you are in
text mode. But it turns out that \underline{} is the only one of those that also works in math
mode. The rest will not work in math mode... well, I should say, it might work but it probably
will not have the effect that you expect. I should clarify what I mean with some examples.
If I type,
then I get,
√ b
2x + µ − 3y 2 − ζ+ a
f (x)dx
So notice that I was able to use underline{} inside of math mode and I got what I expected:
underlined math. But watch what happens when you try to make some bold math with the
\textbf{} function inside of math mode. Typing this:
2x2 − z + 3x - 5y + z 3 .
Technically it compiled... so it “worked,” but notice what happened. Everything I put inside
of \textbf{} was converted to text mode. So I didn’t get bold math as I had hoped. I got
bold text surrounded by regular math. When you type \textbf{}, LATEX will go back to text
mode after the {, but then switch back to math mode after the }. This means that you will get
compilation errors if you try to put this in your document:
You will get errors because we have x^2 inside of the textbf{} command. The exponent com-
mand is only allowed in math mode but LATEX is in text mode. If you want math bold you have to
use the command \mathbf{**} but unfortunately there are limitations on which characters and
symbols can be bolded with basic LATEX. Notice that the pi in the following example isn’t bolded.
Neither are the + or − operators. Also notice that the bolded math is upright instead of slanted.
2x2 + π − Σ = f (x)
Because of the limitations of \mathbf{}, the AMS packages added \boldsymbol{} and \pmb{}
to somewhat correct for these deficiencies (but they have limitations too). You will have to
experiment with each version of bold to see if you like it. Here is an example that I borrowed
directly from the AMS documentation5 . to show the differences:
A∞ + πA0 ∼ A∞ + πA0 ∼ A∞ + π A0
You have likely noticed that I have used Greek letters many times in this document. And,
if you are reading along in the .tex file (as you should be!) you might have noticed that I have
only used Greek inside of math mode. It turns out that you can’t use the basic Greek letters in
text mode.6 With standard LATEX, Greek letters are treated as mathematical symbols only. So
5 Here is the AMS-L AT X website: http://www.ams.org/publications/authors/tex/amslatex
E
Here is the direct link to the short guide: ftp://ftp.ams.org/pub/tex/doc/amsmath/short-math-guide.pdf
6 If you want to actually type text in Greek, Hebrew, Cyrillic, Hangul, or some other script type, you have to
use an add-on package like babel to do the job. There are packages supporting all the major non-English types
of script.
18
if you type \delta in text mode, you will get an error. Thus, if you do want a δelta or a piece
of πie in the middle of your text, then you need to put it inside $ $. But keep in mind that
LATEX is treating these Greek characters as math and not text.
So the main lesson to learn in this section is that there are some things that only work in
text mode, some things that only work in math mode, and some things that work in both. If
you are not sure what will work, then give it a try and see what happens!
A sample of common stuff: ∅∅∞× ≤≱=≈≡∼≃ ±∂ ⊥ ∪∩ ⊂⊃⊆⊇ ∖ → ∇¬∀∃ ∈ ←The first two symbols in
this line are two different
versions of the empty set
If elliptic functions are that are available. Please
your thing, then you need: ℘ note that it is not the
Greek letter phi. The
empty set is a stylized
And a bit of Hebrew version of the letter Ø from
the Norwegian alphabet...
for the set theorists: ℵ I’m sorry... that is a pet
peeve of mine.
With these letters and symbols available in math mode, it makes it easier to have a nice
variety of symbols in your text. This is especially helpful when you are typesetting more ad-
vanced mathematics that has a lot of variables. The variety makes your document much more
readable. And many of these have become relatively standard for certain sets, constants, and
variables, like: R, Rn , C, Z, Q, GL(n, R), π, ℵ◦ , and ℘. Some things are context dependent but
7 http://www.sjsu.edu/people/david.goulette/courses/latex/
8 By-the-way, you should look at the .tex file to see how I align the math text examples here. Also, read my
comments right before the example for an explanation of the syntax. I used the tabular environment and I used
the & character to line up things just like I did with the align environment earlier.
19
common like: x, y and z for real variables, z, w, ζ, and ω for complex variables, θ, ϕ, and ρ
for spherical coordinates, the list goes on and on.
In section 3.2 we covered some of the basics of spacing and indentation. But sometimes
you need more control over spacing in your document. This can happen when you don’t like a
default LATEX behavior or when you are trying to do something outside the basics. Fortunately
we have some more advanced commands at our disposal. This section will not be exhaustive
of all possibilities but it will give you all of the tools you will likely ever need in order to space
things however you want. Also, let me mention that all of the functions I introduce here work
in both text mode and math mode, but might not always work in all situations... it depends on
how you use it. You will have to play with them and see what happens, and if one method gives
you errors or doesn’t work the way you want it to, there are other options you can try instead.
Off the top of my head I can’t remember getting errors doing spacing... but it might happen...
A word of warning. This is the first section in this document where I am giving you the tools
to manually override the behavior of LATEX at the local level. It is true that packages add-on
new behavior to LATEX but it is a global change. And it is also true that environments change
behavior locally, but in an environment, you are relying on the environment to do the changes
for you; you are not manually forcing the changes at a low level. In this section you are learning
commands that override LATEX directly. You are commanding LATEX to do very precise things.
So the results may look great or they may look terrible. And it is also possible to give LATEX
a spacing command that causes errors. So try to use these commands judiciously. It will take
some practice to know when and how to use them.
aa a a a a
the output will look like this:
aa a a a a
LATEX ignores all of the extra spaces after the first one in text mode. But when you are in math
mode, LATEX ignores all of the spaces including the first one. So typing this:
$xx x x x x$
will produce this:
9 Makingtables in latex really isn’t very hard to do but it is a big topic with a lot of options and details.
Once you learn the align environment and how to make matrices below, you will have the requisite knowledge
to take on tables no problem. A matrix really is just a tiny table! If you are interested in learning how to use
the tabular environment, (which is slightly more involved than what I am covering in this introduction) see
http://en.wikibooks.org/wiki/LaTeX/Tables
20
xxxxxx
But what I didn’t emphasize is that LATEX adjusts the spacing between letters and symbols
based on what the letters or symbols are. This is part of why LATEX looks so good. It adjusts
horizontal spacing to make the text look better depending on the context.
The first three, \, \: and \;, create a small, medium, and large space that is fixed in length.
The fourth is a backslash character followed by a space which creates a “control space” that
tends to be a little wider than \; but might depend on your document settings (I’m not sure
about this one...). The command \enspace \quad and \qquad create slightly larger spaces10 .
All six of these work in both math mode and text mode. Here is an example with various spaces
in text mode and mathmode along with the resulting output. The first two letters in each line
have no space between them for comparison.11
Notice how the third x and Σ get scrunched up with the one before it. You will likely never use
\! the way I just did. What I find it useful for is smashing together math symbols that you
think are too spaced out by default. The following example shows how I like to use negative
space and a little extra positive space to change the default spacing in an integral. Actually I
will use two negative space commands back-to-back here because I want to double the horizontal
“squishing.” Compare the following two examples where the first version is the default and the
second is my adjusted version. I show the code for both versions first then the results below.
Since I want to number these I will use the equation environment instead of \[ \].
10 The space created by \quad is roughly the width of a capital M, \enspace is half of a \quad and \qquad is a
double \quad.
11 Notice how the following example flowed over into the right hand margin. This is an example of L
AT X trying
E
to do exactly what I told it to do. I happen to be using the tabular environment here to line things up nicely
and LATEX tries to keep this environment together in one block without word-wrapping. This is an example of
how you have far more power to force what you want with LATEX than you do with any other word processing
software. I happen to be o.k. with this example stretching past the main column into the right hand margin,
but I am technically “breaking the rules” here.
21
\begin{equation}
\int_{a}^{b}\int_{\alpha}^{\beta}f(r,\theta) dr d\theta
\end{equation}
\begin{equation}
\int_{a}^{b}\!\!\int_{\alpha}^{\beta}\!f(r,\theta)\: drd\theta
\end{equation}
b β
f (r, θ)drdθ (6)
a α
b β
f (r, θ) drdθ. (7)
a α
In my opinion the default version, equation (6), has a little too much space between the two
integral signs and a bit too much space between the second integral sign and the f (r, θ). And
I think there should be a little more space between the f (r, θ) and the differential drdθ. The
second version, equation (7), makes these adjustments with the \! and \: commands. It is a
subtle difference. I like the second one better but you might not care either way. This was just
a simple example of how you might use negative space.
There is one limitation to \hspace{} though. It turns out that you cannot use \hspace{}
directly after a newline command (the reasons for this are technical and I don’t fully understand
them myself). For this reason there is an alternate version of this command: \hspace*{} You
can use \hspace*{} right after a line break. Here is an example where \hspace{} doesn’t work
and you have to use the starred version instead. The code is followed by the result:
\noindent
Here is a non-indented line\\
\hspace{4cm}This did not get shifted to the right.\\
\hspace*{4cm}This \emph{did} get shifted to the right by four centimeters.
You can also use negative values for the lengths to shift things to the left like we did above
with \!, but this allows you to shift as far to the left as you want. Here is a silly example of
shifting a word to the left:
\hspace*{15mm}Goulette\hspace{-2.4cm}David
David Goulette
The first \hspace* pushes my last name 2 cm to the right and then my first name “comes next”
in the code but is shifted 2.4 cm to the left so it actually comes before my last name in the final
document. So you can type stuff right over other things which might be a fun effect in some
22
instances. If you type this in your code:
This
is scrambled.
message
There are six common units of length that people use in LATEX: in, cm, mm, pt, ex, and
em. The first three, inches, centimeters and millimeters, are self explanatory and I have already
used them in the examples above. The unit pt is a “point” and is actually the standard unit of
measurement in typography (think “12 point font”). The unit ex is approximately the height
of a letter x in the current font and size. And the unit em is the width of an upper-case M.12
Here are the six common units from shortest to longest:
A\hspace{1pt}A AA
A\hspace{1mm}A AA
A\hspace{1ex}A A A
A\hspace{1em}A A A
A\hspace{1cm}A A A
A\hspace{1in}A A A
These units can be used in any LAT
EX function that takes a length as input (not just \hspace{}).
For example, the marginnote command, which I have used many times in this document, has
an optional input that shifts the margin note up or down by whatever length you want. There
are lots of commands and environments that use lengths.
So, if I were doing homework, this is an example of how I might put my name, title and date
on one line. The \hfill function fills the space in the middle to make things balanced. So you
could do this:
Note that because I did not use \noindent this time, the line is still indented. But since I used
four text blocks with three \hfill commands in between, the blocks are evenly spaced between
the indent and the right margin.
LaTeX/Lengths
23
horizontal space. The \phantom{} function creates invisible text that takes up the space of
“real” letters but you can’t see them in the final document! Here is an example:
abcdefg abcdefg
ab\phantom{cde}fg ab fg
So the \phantom{cde} created an “invisible” cde that takes up exactly the amount of space the
letters cde would take up, but you can’t see them in the final document. So you can clearly use
this as a method for creating horizontal space that matches a certain sequence of characters.
24
Horizontal spacing examples in text mode
xy xy
x\!y xy
x\,y xy
x\:y xy
x\;y xy
x\enspace y x y
x\quad y x y
x\qquad y x y
x\ y xy
x\hspace{1pt}y xy
x\hspace{1mm}y xy
x\hspace{1ex}y x y
x\hspace{1em}y x y
x\hspace{1cm}y x y
x\hspace{1in}y x y
x\hspace{1.1in}y x y
x\hspace{3.7cm}y x y
x\hspace{37mm}y x y
x\hspace{12pt}y x y
x\hspace{7em}y x y
x\hspace{5.3ex}y x y
x\hspace{-1pt}y xy
x\hspace{-2mm}y yx
x\hspace{-4mm}y yx
xabcdy xabcdy
x\phantom{abcd}y x y
xINVISIBLEy xINVISIBLEy
x\phantom{INVISIBLE}y x y
x\hfill y x y
x\hfill y\hfill z x y z
w\hfill x\hfill y\hfill z w x y z
A rose\hfill is a\hfill rose. A rose is a rose.
25
Note that all of these are the same as in text mode. I only changed the last example.
$xy$ xy
$x\!y$ xy
$x\,y$ xy
$x\:y$ xy
$x\;y$ xy
$x\enspace y$ x y
$x\quad y$ x y
$x\qquad y$ x y
$x\ y$ xy
$x\hspace{1pt}y$ xy
$x\hspace{1mm}y$ xy
$x\hspace{1ex}y$ x y
$x\hspace{1em}y$ x y
$x\hspace{1cm}y$ x y
$x\hspace{1in}y$ x y
$x\hspace{1.1in}y$ x y
$x\hspace{3.7cm}y$ x y
$x\hspace{37mm}y$ x y
$x\hspace{12pt}y$ x y
$x\hspace{7em}y$ x y
$x\hspace{5.3ex}y$ x y
$x\hspace{-1pt}y$ xy
$x\hspace{-2mm}y$ x
y
$x\hspace{-4mm}y$ yx
$xabcdy$ xabcdy
$x\phantom{abcd}y$ x y
$xINVISIBLEy$ xIN V ISIBLEy
$x\phantom{INVISIBLE}y$ x y
$x\hfill y$ x y
$x\hfill y\hfill z$ x y z
$w\hfill x\hfill y\hfill z$ w x y z
$2x^2\hfill y_2\hfill \Psi$ 2x2 y2 Ψ
26
7.2 Vertical Spacing
You usually don’t need to do vertical spacing quite as much as you do horizontal spacing so I
will just give you some basics.
The default value is 1. Play around with different values to see what you like. Another (and
probably better) option you have for either global OR local line spacing changes is to use the
setspace package.13
\noindent
This is on a line,\\
and this is on the next line with the usual space.\\[1cm]
But this is on a new line after 1 cm of space. Pretty simple, right?
This is on a line,
and this is on the next line with the usual space.
——————————
Basically, all of the measurement rules you learned regarding horizontal spacing in section 7.1
apply when you are doing vertical spacing except for one important thing: since this is a vertical
spacing command, positive lengths go in the downward direction and negative lengths go in the
upward direction. So:
This
is message
scrambled.
The words “is scrambled” are on a new line but that new line is shifted upward by 4.2 mm from
where it would normally be, which puts it right over the top of the previous line.
27
what doesn’t. Basically, the skip commands need to come between two paragraphs. Remember
that hitting the <enter> key (or <return> on a Mac) one time does not get you a new para-
graph. If you hit <enter><enter> then you will get a new paragraph. Anyway, none of the
following examples work correctly because the skip command is not between two paragraphs. I
show each one followed by the result.
This \bigskip
doesn’t \medskip
work\smallskip
either.
This doesn’t workeither.
The way to make sure that these work is to either have an empty line of code directly following
the skip function, or place the skip function right before the \\ command, or you do both (each
option will have a different result). If you have a \\ preceding the empty line it will have
the effect of adding an extra line and then also tacking on the skip space. The rules about
indentation that we covered in section 3.2 still apply. Examples that work:
\noindent
This does work! \bigskip
————
\noindent
This works too! \bigskip\\
This is not indented after a big skip.\smallskip
————
\noindent
This is a different example. \smallskip\\
\noindent
This is not indented after a space and a big skip.\\
28
This is a different example.
————
In this last example note that the \smallskip made a small space and the \\ combined with
<enter> <enter> added an additional space. The same thing happened with the \bigskip but
the skip is larger and I suppressed the indent manually. Compare this last example with the
earlier examples and you will see the differences.
First sentence.
————
First sentence.
\noindent
First sentence.\vspace{-3mm}\\
Second sentence shifted UP 3 mm.
First
Secondsentence.
sentence shifted UP 3 mm.
————
That last example shows how negative lengths shift the next line up in the negative vertical
direction.
29
One limitation to \vspace{} is that it will not work if you use it directly after a \pagebreak.
If you want vertical space at the beginning of a page you need to use \vspace*{} instead, then
it will work.14 . The vertical space it creates comes after the top margin, meaning the vertical
space will be added to the length of the top margin. Here some example code followed by the
result:
\pagebreak
\vspace*{1in}
This is on the next page, indented, after one inch of blank space below the top margin.
This is on this page.
14 If you ever want to make nice multiple columns I suggest you use the multicol package. This great package
allows you to do a \columnbreak to break to the next column (just like a page break). In this case, if you want
vertical spacing right after the \columnbreak command, you need to use \vspace*{}. For more on the multicol
package see the official documentation here: http://www.ctan.org/pkg/multicol
30
This is on the next page, indented, after one inch of blank space below the top margin.
7.2.5 \vfill
The command \vfill works just like \hfill but it does a rubber band spacing in the vertical
direction. Just like with \bigskip and \vspace{}, you have to be careful where you stick the
\vfill command. I’m not really sure why but lines always seem to be indented after a \vfill.
Anyway, I have two examples that show how I use it. The first is a nice example of how to use
\vfill along with the center environment on page 24. Here is a more basic example where I
have 3 lines of text that are spread out to fill the remaining space on this page. The first line is
at the top of the remaining space on this page, the third line is at the bottom, and the second
line is right in the middle. All indentation rules still apply. The \newline command at the end
is required to make sure that the block of text that follows is pushed to the next page:
This indented line is half way between the previous line and the bottom of the page.
This is not indented and is forced to the very bottom of the page.
31
8 More mathematics
Now we will return to cover a hodgepodge of mathematical topics. Some are very important
and are used all of the time. Some only come up once in a while. For example, section 8.1
is extremely important for mathematics. But section 8.2 covers more uses of the \phantom{}
function that might only come in handy occasionally.
32
8.1.1 \left and \right
The first option is to let LATEX automatically adjust the size for you. You do this by using the
pair of functions \left and \right. The \left and \right functions must directly precede the
grouping symbol that you would like LATEX to resize for you, like this:
For the sake of completeness, I will mention one more option you have with \left and
\right. You can use a period to suppress one of the \left or \right grouping symbols. You
just type \left. or \right. in place of one of the regular grouping symbols. Remember, that
you must pair a \left and a \right. But one of the pairs can have a period causing it not to
appear. Since I can’t think of an example that is simple and useful (sorry!), here is a contrived
example to understand the syntax:
33
\[
x_1+x_2+\cdots+x_n \Rightarrow
\left\{\;
\sum_{i=0}^n x_n
\right.
\]
( n
X
x1 + x2 + · · · + xn ⇒ xn
i=0
The reason I can’t come up with a simple example is that many of the situations where you
would use a singe bracket have better solutions than using this method.15 And the one place
where I would use \left. or \right. unfortunately uses too many things that I haven’t taught.
So I won’t explicitly show the code here (you can see it in the .tex file of course!). The \left.
or \right. commands can be useful if you want a grouping symbol on only one side of some
math with explanatory text on the other side. This example is silly but this is useful in serious
situations sometimes.
iθ This is not the only way
Cool equations from e = cos(θ) + i sin(θ)
to do this. This
=⇒ 1 f (z) example has a few
complex analysis. f (a) = dz things that are
2πi γ z − a
somewhat more
advanced.
Now the great part about \left and \right is that LATEX does the resizing for you auto-
matically. But the bad part (sometimes) is that the automatic results might not look good. And
there are also cases where \left and \right won’t change the default sizing at all (even when
you are hoping it will). But fortunately you have a second option whenever you don’t like the
automatic results. Well, actually... you have four options. And those options are...
\[
(\frac{x^2}{17})^2 \:
\bigg(\frac{x^2}{17}\bigg)^2 \:
\Bigg(\frac{x^2}{17}\Bigg)^2 \qquad
\|\aleph\|\:
\Big\|\aleph\Big\|\:
\Bigg\|\aleph\Bigg\| \qquad
[x^2] \:
\big[x^2\big] \:
\Big[x^2\Big]
\]
34
Just like with \left and \right, you have to put these functions directly before the grouping
symbol that you want to resize manually. But unlike \left and \right you do not have to
worry about matching left and right bracket. So you could use these with a single unpaired
grouping symbol if you want.
Here are all the sizes side by side with the same curly bracket. If you apply these sizes to
left and right brackets, like this:
\Bigg\{ \bigg\{ \Big\{ \big\{ \{ \varnothing \} \big\} \Big\} \bigg\} \Bigg\}
then you will get something that resembles a Russian matryoshka doll with nothing inside:
( ) This is an extremely
n o empty set! Actually,
{∅} this set is not empty at
all... it is a set in a set
in a set in a set in a set
But let me emphasize again that you will get these sizes no matter what. LATEX isn’t doing any in a set which happens
to contain nothing.
automatic adjusting for you. So it might or might not look good. You just have to play around
to see what you like. To let you see the size differences in display mode here is a table with all of
them side by side. The first one is the default size and the second entry uses \left and \right
so you can compare the cases. Note that \left and \right adjust to the size of the object it
encloses but the rest have fixed height no matter what is in the middle. Here is a table to show
you all of the options we have covered side-by-side.
\left
Sizes: Normal \right \big \Big \bigg \Bigg
! !
m m m m m m
Examples (x)( ) (x) x x x x
n n n n n n
Now that we know both methods, lets go back and fix how equation (9) looks. It turns out
that if you try to use \left and \right they won’t change the look at all. So typing this:
\[
S=\left\{
\left\{\Sigma,b\right\}, \left\{a,[x;y;z]\right\}, \mu,Z
\right\}
\]
35
Notice that I kept the innermost square brackets at normal size and increased the sizing outward.
It is a matter of taste but I think this final version is easier to read. You may not agree (or care
one way or the other) and that is fine. Vive la différence!
Here is one last case where \left and \right actually do not look nearly as good (in my
opinion) as forcing the size you want (this is a modified version of an example found in the
AMS-math documentation). Consider this:
\[
\left[ \sum_i a_i
\left\lvert \sum_j b_{ij}
\right\rvert^q
\right]^{1/3}
\]
which gives you this:
q 1/3
X X
ai bij
i j
\[
\Bigg[ \sum_i a_i
\bigg\lvert \sum_j b_{ij}
\bigg\rvert^q
\Bigg]^{1/3}
\]
I think the second version looks much better because the brackets and the absolute value aren’t
so tall. Also, the q in the exponent is tucked under the right bracket better in the second version.
Now, I only fuss over these things when I am finishing up a document that really matters.
You shouldn’t waste time worrying about minutia when you are drafting your documents. These
are the things that are easy to go back and fix after you have your ideas down and you want
to really make your work look professional. In general I try to make the document as readable
as possible and there are often a variety of choices. Sometimes the differences are subtle. I will
close this section with a final side-by-side comparison. Here I have three options of parentheses
side-by-side:
√ √ √ √ 2 √ √ 2
( 2 − x)2 = 2− x = 2− x .
The first of the three is the default size and the parentheses are a little too small. Either the
second one, using \big, or the third one, using \left and \right, look better to my eye. Take
your pick.
36
you can give an invisible \phantom{} character visible subscripts and/or superscripts. So you
can do things like this:
b
${}_a^b$ will give you this: a
Or equivalently
b
$\phantom{}_a^b$ will give you this: a
They are essentially the same. In this example we are using an invisible letter (with no width
since the brackets are empty) to force a subscript and a superscript that just seem to float in
space. Now it may not seem like this would be very useful, but what if you want a subscript and
superscript that precedes a letter or symbol? This is common in chemistry but also hypergeo-
metric functions and combinatorics use preceding subscripts occasionally. The easy way to do
this is with {} and/or \phantom{}. Here is an example of one common notation for “5 choose
3,” meaning all possible combinations of 3 things chosen from a set with 5 things:
So the phantom character lets you create the subscript 5 which follows directly before the letter
C. Since LATEX ignores the space, the 5 and the C get squished together just like you would
want them to be.
By-the-way, let me digress since I mentioned combinations. You should know that the bino-
mial coefficient notation is also available via the AMS packages. The function is \binom{}{}:
238
${}^{238}_{92}\mathrm{U}$ but that will give you this 92 U
which isn’t quite get the right result. The preceding 238 looks correct but the problem is
that we want the 92 to be shifted to the right by the same width as the 2. But this is exactly
what \phantom{} does for you! We just need to insert a \phantom{2} in front of the 92 like this:
238
${}^{238}_{\phantom{2}92}\mathrm{U}\quad $ which gives you 92 U
and the problem is solved. Now there is a little invisible 2 that is helping line things up perfectly.
37
Let f (x) = x2 and g(x) = x + 2. Then f composed with g is (f ◦ g)(x) = (x + 2)2 .
But sometimes you want text inside of math where you can’t easily leave math mode. Of-
ten you just want one or two words, like “and,” “or,” “such that,” or “therefore” in the middle
of your math. To do this use the \text{} function which is available via the AMS math package.
$\displaystyle Z =
\frac{\text{‘‘sample statistic’’} -\text{‘‘sampling distribution mean’’}}
{\text{‘‘standard error’’}}
=\frac{\widehat{p}-\mu_{\widehat{p}}}{\sigma_{\widehat{p}}}$
“sample statistic” − “sampling distribution mean” pb − µpb
Z= =
“standard error” σpb
Another common way you might want to enter text inside of math mode happens within the
align environment. You often want to interject a quick comment in the middle of some aligned
equations but the problem is that if you leave the align environment to type the text, then you
lose your alignment settings. So then you can’t align the equations that follow. The AMS math
packages have a command \intertext{} that allows you to do this. Here is an example of how
I might use this to explain something to a College Algebra class. Suppose I want to show the
steps to solving 2(x − 3)2 − 3 = 15. Then I might do this:.
\begin{align*}
2(x-3)^2-3 &= 15\\
2(x-3)^2 &= 18\\
2(x-3)^2 &= 18\\
(x-3)^2 &= 9.\\
\intertext{Taking square roots of both sides gives us}
x-3 &= \pm\sqrt{9}\\
x-3 &= \pm 3,\\
\intertext{so}
x &= 3\pm 3,\\
\intertext{therefore}
x = 0\quad&\text{and}\quad x = 6
\end{align*}
are the solutions.
2(x − 3)2 − 3 = 15
2(x − 3)2 = 18
2(x − 3)2 = 18
(x − 3)2 = 9.
38
so
x = 3 ± 3,
therefore
x=0 and x = 6
are the solutions.
Note how the alignment (using the & character) continues even after the use of \intertext{}.
Also note how I used \text{} to put the word “and” in the middle of the last line. I also used
some \quad space and an & to line the “and” up with the equals signs from above.
All of the examples I have shown you so far come up quite a bit so they will be useful. But
unfortunately any other example of putting text inside of math that I ever need requires some
things that are a little more advanced than I want to cover here. Specifically they require the
use of boxes: \parbox, \fbox, \framebox, \raisebox, etc. I used \raisebox to make my silly
Alice example on page 9. But that didn’t have math. I use a \parbox to do this:16
Using a parbox allowed me to put word-wrapped text inside of math braces with a mini para-
graph (which is what the “par” means in parbox). For more on boxes see:
http://en.wikibooks.org/wiki/LaTeX/Boxes
p̂ pb xyz
ˆ xd
yz
←−−→ ωζθ −
x− →
z }| {
ABCD
^ wxyz M N OP QRST U V 0 x1 ABΓ∆
| {z }
16 This clever use of words inside set braces came right from a book I happen to be reading today: Algebraic
39
This is a case where the extended under-stuff and over-stuff do not look good in my LATEX viewer.
It looks much better in Adobe Reader, but it still isn’t absolutely perfect even there (but it isn’t
too bad). There are packages that improve this if you really need it to look publishing perfect
or it just bothers you.17
Having these variable length over and under things makes it easy to do some useful things.
For example, an extensible bar is useful for complex variables (this also gives us a chance to use
big parentheses too):
\[
\overline{(e^{-iz}+w)^2} =
\Big(\overline{e^{-iz}+w}\Big)^2 =
\Big(\overline{e^{-iz}}+\overline{w}\Big)^2 =
\Big(e^{\overline{-iz}}+\overline{w}\Big)^2 =
\Big(e^{i\overline{z}}+\overline{w}\Big)^2
\]
2 2 2 2
(e−iz + w)2 = e−iz + w = e−iz + w = e−iz + w = eiz + w
Sometimes, for more advanced documents, you want more complicated stacked constructs.
Fortunately there are many ways to do this. But I will only show a few here. One way is to
use the subscript command after the \underbrace{} command or use the superscript command
after the \overbrace{}. These have the effect of putting the text above or below the brace. In
this example I use a brace and a bracket, and I also use the \text{} command we learned in
the last section, plus I use some horizontal spacing.
\[
\overbrace{f(x)=2x^3+3x^2+2x-2}^{\text{polynomial function}}
\hspace{1cm}
\underbracket{\Phi(x)= 2xe^x+13\cos(2x+35)+x^{\pi}}_{\text{transcendental function}}
\]
polynomial function
z }| {
f (x) = 2x3 + 3x2 + 2x − 2 Φ(x) = 2xex + 13 cos(2x + 35) + xπ
transcendental function
When LATEX tries to fit everything in, it can do some strange horizontal spacing. So here is
another example where I will show the unedited and the edited versions next to each other. I
improved the output with some horizontal spacing adjustment. Fortunately this sort of micro-
editing doesn’t come up too often. But when you want to do something special, you might need
the skills to make it look good.
\[
f(z)=f(x+iy)=
\underbrace{u(x,y)}_{\text{Real part}} +
i\underbrace{v(x,y)}_{\text{Imaginary part}}
\]
\[
f(z)=f(x+iy)=
\underbrace{u(x,y)}_{\text{Real part}} +\;
i\hspace{-7mm}\underbrace{v(x,y)}_{\hspace{5mm}\text{Imaginary part}}
\]
17 The Math Time Pro 2 package, created by Michael Spivak, has really elegant looking braces (and lots of
other features). Unfortunately the full package is not free. However the “Lite” version is free and has the nice
braces. Both the free and pay versions are available here: http://www.pctex.com/mtpro2.html
40
f (z) = f (x + iy) = u(x, y) +i v(x, y)
| {z } | {z }
Real part Imaginary part
Another useful pair of commands is the \underset and \overset functions. These both take
two inputs. You need, \underset{under-stuff}{regular-stuff} and the analogous inputs
for \overset. There are times when you might want something under or over another thing
that isn’t standard. This happens all the time in advanced mathematics when you are making
things up that have no standard notation. Maybe you want a math squiggle below your variable.
You can do this with the command \sim which usually is used for things like $p\sim q$, which
gives you p ∼ q. But this is how to hack an under-squiggle:
\underset{\sim}{x} to get x
∼
So the x is in-line in the normal place, and the twiddle is below the x. By-the-way, I don’t love
the amount of space between the x and the squiggle underneath so this is a perfect place to use
a \phantom{} character! If we use \widetilde over a phantom character but put the whole
thing in the underset below the x then it will push the squiggle up! (O.k. it’s a hack... I know.
But it is good to learn some hacks.) Now, another thing about \underset and \overset is that
the script inside of the under/over set is the size of an exponent. So if you want it to be normal
in-line size, then you have to force it with \displaystyle. Compare the following versions of
putting a twiddle underneath the x:
$\underset{\sim}{x} \qquad
\underset{\widetilde{x}}{x} \qquad
\underset{\widetilde{\phantom{x}}}{x} \qquad
\underset{\displaystyle \widetilde{x}}{x} \qquad
\underset{\displaystyle \widetilde{\phantom{x}}}{x} \qquad
\widetilde{x}$
x x x x x x
e
∼ x
e e x
e e
The first example is the one with \sim like before. I threw in the second example, with the
visible x, just so you how the x in the underset was functioning. But then I made it vanish in
the third one. But it is too small, so the fourth example shows the size fix, and the fifth one is
the result I was looking for. Note that the fifth version matches the sixth example which is the
twiddle that you get over the x in the normal way. You would would want the under-twiddle
to match the over-twiddle.
Just recently I was documenting a graph theory algorithm that I was coding and it had two
parts which found ascending and descending edges connecting vertices. Anyway, to make it clear
which version of the algorithm I was using, I differentiated the results of the two methods with:
This made my documentation much clearer than relying on the reader to know in context which
version of the algorithm I was using. Good notation is often the key to clear technical writing.
So the more you use LATEX the more creative ways you will find to make your documents clear
and expressive.
Here is a slightly more involved example which I might create for an algebra student to
remind them of the steps required for completing the square of a quadratic expression. Again,
41
this is a chance for us to apply a variety of things we have learned. One thing I do here is use
\underset inside of another \underset in order to get text below and arrow which is below one
of the coefficients. Also I use some \! for negative space to squeeze things together a bit (this
is not necessary). And I also needed variable sized parentheses:
\[
x^2+\!\underset{\underset{b=3}{\uparrow}}{3}\!\!x+2 =
x^2+3x+\overbrace{\underbrace{\left(\frac{3}{2}\right)^2}_{(b/2)^2}\!\!
-\left(\frac{3}{2}\right)^2}
^{\text{\footnotesize add and subtract}}\!\!+\, 2 =
\overbrace{x^2+3x+\left(\frac{3}{2}\right)^2}
^{\text{\footnotesize Perfect square}}\!- \frac{1}{4}=
\left(x+\frac{3}{2}\right)^2\!-\frac{1}{4}
\]
add and subtract Perfect square
z }| { z }| {
2 2 2 2
2 2 3 3 2 3 1 3 1
x + 3x + 2 = x + 3x + − + 2 = x + 3x + − = x+ −
↑ 2 2 2 4 2 4
b=3 | {z }
(b/2)2
8.5 Matrices
Now that you know what an environment is, you know what display math is, you are aware of
the AMS packages, and you have seen how the ampersand character works, you have all the
building blocks for matrices! And it even helps that you know how to adjust the spacing if needed
(since sometimes you need it with matrices). There are a variety of ways you can do matrices
but the best way is to use any one of the following environments: matrix, bmatrix, pmatrix,
Bmatrix, vmatrix, or Vmatrix. Note that I said these are environments, so you will need to do
\begin{pmatrix} and end it with \end{pmatrix}. The entries of the matrix go in between.
Unlike the align environment or the equation environment which are stand-alone math mode
environments, you have to put a matrix inside $ $ or inside a display math environment like
\[ \] or \begin{equation} *** \end{equation}. That is why my examples begin and end
with commands that invoke the math mode. Here is the syntax for a matrix with brackets:
\[
\begin{bmatrix}
a & b\\
c & d
\end{bmatrix}
\]
a b
c d
Note that the ampersand & is used to split the columns and line things up. Also you need a
newline, \\, command to move to a new row. So if you want more columns, use more &, if you
want more rows, use more \\. The brackets are created by the bmatrix environment itself.
The following example shows a rotation matrix which rotates a vector around the y-axis.
The equation is show with parentheses matrices. It requires a matrix environment for each
matrix:
42
\[
\begin{pmatrix}
\cos(\theta) & 0 & -\sin(\theta)\\
0 & 1 & 0\\
\sin(\theta) & 0 & \cos(\theta)
\end{pmatrix}
\begin{pmatrix}
u_1\\
u_2\\
u_3
\end{pmatrix}
=
\begin{pmatrix}
v_1\\
v_2\\
v_3
\end{pmatrix}
\]
cos(θ) 0 − sin(θ) u1 v1
0 1 0 u2 = v2
sin(θ) 0 cos(θ) u3 v3
Notice that I had two ampersands in each row of the first matrix because there were 3 columns.
Also note that you do not need the ampersands to line up. LATEX will do it for you. Writing
code for matrices is a little different that anything else because you have to think vertically even
though it will be printed horizontally. To help make things clearer I put the equals sign on it’s
own line. Here are the other versions of matrices that are available. The only difference between
the different versions is what type of grouping symbol they put around the entries (or nothing
at all in the first case). The syntax for filling in the entires is exactly the same.
a b
\begin{matrix}
c d
a b
\begin{Bmatrix}
c d
a b
\begin{vmatrix}
c d
a b
\begin{Vmatrix}
c d
You can have empty spots in a matrix and you can also use \cdots, \vdots, and \ddots,
. .
which will give you · · · , .., and . . inside of your matrix. The next example is a general linear
transformation T : Rn → Rm . It is shown here as an m×n matrix multiplied by an n dimensional
vector. The result is m dimensional. Admittedly, this is an involved example, but it comes up
if you do any matrix algebra. And I wanted to show you that you can put more complicated
things inside of a matrix like a summation. Note the way I space things in the code to line
up the ampersands. This is only for readability and is not necessary. I also use some vertical
spacing, blank spaces, and lots of dots.
43
\[
\begin{pmatrix}
a_{11}& a_{12} & \cdots & a_{1n}\\
a_{21}& a_{22} & & \vdots \\
\vdots& & \ddots & \vdots\\
a_{m1}& \cdots & \cdots & a_{mn}
\end{pmatrix}
\cdot
\begin{pmatrix}
x_1\vspace{1mm}\\
x_2\\
\vdots
\vspace{1mm}\\
x_n
\end{pmatrix}
=
\begin{pmatrix}
\sum_{i=1}^{n}a_{1i}\, x_i\vspace{2mm}\\
\sum_{i=1}^{n}a_{2i}\, x_i\\
\vdots\\
\sum_{i=1}^{n}a_{mi}\, x_i
\end{pmatrix}
\]
Pn
a11 a12 · · · a1n x1 i=1 a1i xi
.. x Pn
· . = i=1 a2i xi
a21 a22 . 2
. .. .. . .
.. . . P ..
.
n
am1 · · · · · · amn x n a
i=1 mi i x
Sometimes there are just too many entries to line things up so you just have to be careful with
your code. The default limit to the number of columns is 10.18 A matrix in Jordan canonical
form has the following form where any empty space is a zero:
\[
\begin{bmatrix}
\lambda_1 &&&&&&&&&\\
& \lambda_2 & 1 &&&&&&&\\
&& \lambda_2 &&&&&&&\\
&&& \ddots &&&&&&\\
&&&& \lambda_i & 1 &&&&\\
&&&&& \lambda_i & 1 &&&\\
&&&&&& \lambda_i &&&\\
&&&&&&& \ddots &&\\
&&&&&&&& \lambda_{n-1} &\\
&&&&&&&&& \lambda_n \\
\end{bmatrix}
\]
18 You can increase the default number of matrix columns allowed. See:
http://www.latex-community.org/forum/viewtopic.php?f=46&t=11214
44
λ1
λ2 1
λ2
..
.
λi 1
λi 1
λi
..
.
λn−1
λn
——————————————
What if you are doing some more advanced matrix theory with block matrices, in other
words, you need matrices inside of matrices? No problem! Here is a 5 × 6 matrix represented
as a 2 × 2 in block notation (by-the-way, I2 is shorthand for a 2 × 2 identity matrix and 0 3×4
is shorthand for a 3 × 4 matrix filled with zeros):
a b
c d 0 3×4
A3×2 B3×4
= e f
C2×2 D2×4
α β γ δ
I2
ϵ ζ η θ
Here is the code for this example:
\[
\begin{bmatrix}
A_{3\times2}& B_{3\times4}\\
C_{2\times2} & D_{2\times4}
\end{bmatrix}
=
\begin{bmatrix}
\begin{pmatrix}
a & b\\
c & d\\
e & f
\end{pmatrix}
&
\mathit{0}_{\,3\times 4}
\\
I_2
&
\begin{pmatrix}
\alpha & \beta & \gamma & \delta\\
\epsilon & \zeta & \eta & \theta
\end{pmatrix}
\end{bmatrix}
\]
If you really scrutinize this code you will see that the bmatrix on the right hand side of the
equals sign is a 2×2 matrix (there is only one &, meaning two columns, and only one \\ meaning
two rows). It just so happens that the upper left-hand entry is a pmatrix, and the lower right
hand entry is also a pmatrix.
———————————————————
45
The final example in this section utilizes many things we have learned in an example that
might look familiar to you (assuming you have taken a basic multivariable calculus course). This
is a formal mnemonic to remember how to compute the cross product of two vectors → −u,→−
v ∈ R3 .
This example uses the align environment, two types of matrices, bold math letters, two types
of over arrows (I put bigger ones on the variables and smaller arrows on the unit basis vectors),
and also some spacing just to make things look nice. We have really covered a lot of material!
→
−
u ×→
−
v = [u1 u2 u3 ] × [v1 v2 v3 ]
⃗i ⃗j ⃗k
= det u1
u2 u3
v1 v2 v3
u2 u3 ⃗ u u3 ⃗ u u2 ⃗
= i− 1 j+ 1 k
v2 v3 v1 v3 v1 v2
\begin{align*}
\overrightarrow{\mathbf{u}} \times \overrightarrow{\mathbf{v}}
&= [u_1\;\; u_2\;\; u_3]\times [v_1\;\; v_2\;\; v_3]\\[7pt]
&= \det \begin{pmatrix}
\vec{\mathbf{i}} & \vec{\mathbf{j}} & \vec{\mathbf{k}}\\
u_1 & u_2 & u_3\\
v_1 & v_2 & v_3
\end{pmatrix}\\[7pt]
&=
\begin{vmatrix}
u_2 & u_3\\
v_2 & v_3
\end{vmatrix}\vec{\mathbf{i}}
-
\begin{vmatrix}
u_1 & u_3\\
v_1 & v_3
\end{vmatrix}\vec{\mathbf{j}}
+
\begin{vmatrix}
u_1 & u_2 \\
v_1 & v_2
\end{vmatrix}\vec{\mathbf{k}}\\[4pt]
&=
(u_2 v_3-v_2 u_3)\,\vec{\mathbf{i}}-
(u_1 v_3-v_1 u_3)\,\vec{\mathbf{j}}+
(u_1 v_2-v_1 u_2)\,\vec{\mathbf{k}}
\end{align*}
46
maybe you want to reference an equation that you had earlier in your paper like, “If we substitute
42 for x in equation (7) on page 17, we will clearly prove that the Riemann Hypothesis is true.”
Stuff like that comes up all the time. Well, the numbers that I just typed in those examples
were hand typed. I wasn’t using dynamic referencing in the proper way. You do not have to
create any of the numbering manually. LATEX will do that for you just like it numbers equations
for you. I will not cover how to reference a figure or a theorem here. I will just cover how to
reference a section and an equation here.19 All other referencing you might do is similar.
In order to reference something, you first have to label it. The way you label something
in your code is by adding this: \label{labelname}. The “labelname” can be any name that
you want to give it (you can’t use special characters). You just have to put the \label right
after whatever you are labeling. Then later you can reference this label with \ref{labelname},
\eqref{labelname} or \pageref{labelname}. But first, here is an example of how you label
a section, just put it right after the section command like this:
Note that you can’t see the label in the final document. The label is just a marker in the code.
So if you want to reference this section somewhere in the paper you just have to reference this
label that we just created. To do the reference you just have do the following (I will explain my
use of the ~ character in a parenthetical aside below the example... I have never explained what
it does up to now):
LATEX will take this code and convert the \ref command into the number of the object that is
referenced inside the braces. So you will see this as a result:
(Let me make a parenthetical side comment regarding the tilde character. The ~ character
creates a non-breaking space. You do not have to have the ~ here in this reference; you could
just do this:
But I recommend adding the tilde. When you put a ~ between normal text characters in your
code, LATEX will create a space where the ~ is (just like it normally would if you put a space
in the code with space bar), but LATEX will not allow a line break where you have a ~. You
wouldn’t want a line break to split up the word “section” from the number, like this: section
9.1. That looks strange. One would prefer the 9.1 to come right after the word “section.” The
~ character “glues” them together to make sure the awkward line break won’t happen between
them. If a word wrap is needed, the whole block will be sent to the new line.)
Now, with basic LATEX, your section reference won’t be colored blue like it is in the above
example and it won’t be a live link either. You might remember that in section 5 above, I ← Note how I
mentioned that I am using the hyperref package in this document. The hyperref package referenced an earlier
section here. Very
19 I will cover how to add figures in another document and I will mention how to label them and reference them useful!
in that document. It is similar to labeling equations.
47
makes your references live links and I chose to have them colored blue to make it obvious
that they were links (see the preamble for more details). So if you make labels without using
hyperref, then the numbers will be black.
Instead of referencing the section number, you can alternatively reference the page that the
section label is on by doing this:
Notice that when you use \ref you get the number, but when you use \pageref you get the
page that it is on instead.
n
X
f (x) = ai xi (10)
i=0
So using \eqref creates parentheses which matches the automatic numbering format, but using
\ref just has the number. Also note that we needed to use the equation environment here,
and not simply \[ \], because we need the equation to be numbered in order to label it.
You can also label multiple lines within the align environment. Here is an example where I
label each line. Note that I put the label right before the newline command, except for the last
line where you don’t need the newline command:
\begin{align}
2x+3 &= 0\label{line}\\
x^2+y^2+x+y-1 &= 0\label{circ}\\
x^2-y^2-4x+2y+4 &= 0\label{hyperb}
\end{align}
Equation~\eqref{line} is a line, \eqref{circ} is a circle, and
\eqref{hyperb} is a hyperbola.
2x + 3 = 0 (11)
2 2
x +y +x+y−1=0 (12)
2 2
x − y − 4x + 2y + 4 = 0 (13)
48
Equation (11) is a line, (12) is a circle, and (13) is a hyperbola.
Remember that the labels do not create the numbers in parentheses on the right; the align
environment creates the numbering on each line. The labels just allow you to reference those
numbers.
Now, what if you have four lines of equations in the align environment but you only want
to label one of the four lines? Well, you need to add the command \notag to any line you do
not want to number, and you label the line you do want numbered. Like this:
\begin{align}
e^z &= -2 \notag \\
(y-3)^2+5 &= 0 \notag\\
x^3-14x^2+2x-3 &= 0 \label{cubic}\\
\cos(\theta) &= 2 \notag
\end{align}
Equation~\eqref{cubic} is the only of the four equations that has a solution
in $\mathbb{R}$. The rest have no real solutions.
ez = −2
(y − 3)2 + 5 = 0
x3 − 14x2 + 2x − 3 = 0 (14)
cos(θ) = 2
Equation (14) is the only of the four equations that has a solution in R. The rest have no real
solutions.
You can also create a custom tag for an equation if you don’t want it numbered. To do this
you use the \tag{label} or \tag*{label}. The starred version does not have parenthesis, the
non-starred version has them. Not that “label” can be whatever you want but it is important
to note that when you are inside the brackets for the \tag command, you are in text mode.
So if you want your “label” to be a math symbol, then you have to use dollar signs inside the
brackets. Here are some examples:
\[
f(x)=x^2\label{func1}\tag{A}
\]
\[
g(x)=x+3\label{func2}\tag*{B}
\]
\[
h(x)=(x+3)^2\label{func3}\tag{$\star$}
\]
\[
e^{i\pi}=-1\label{love}\tag*{$\heartsuit$}
\]
If you plug equation~\ref{func2} into equation~\eqref{func1}, then you get
equation~\eqref{func3}. I love equation~\ref{love}.
f (x) = x2 (A)
g(x) = x + 3 B
h(x) = (x + 3)2 (⋆)
iπ
e = −1 ♡
If you compose function B with function (A), then you get equation (⋆). I love equation ♡.
49
Notice the difference between the starred version and the regular version of \tag. Also note
that the first two examples had regular text as a tag (just the letters A and B) but the third
and the fourth example used the math-mode symbols \star and \heartsuit as the tag. So I
needed to enter math mode inside the tag with dollar signs. Even though the \tag{} is inside
of math mode, when you are inside of the {} of \tag, you are in text mode.
f (x) = k (15)
Equation (??) is a constant function.
Do you see the error? You won’t necessarily get an error when you compile the document if you
have a typo in the label like this, but you will see ?? where there should be a number.
The other issue with labels that can come up is that it can be hard to keep track of them
when you have dozens of labels. For instance, at the moment, as I am typing this line I currently
have 35 labels in this document! The way that I keep track of these is by naming the labels
with a special prefix that reminds me of what the label means. Now I have not been using these
prefixes in the examples in this section, just so you know. But here are examples of how I would
label sections, equations, figures, theorems, lemmas, and corollaries:
\label{sec:SectionName}
\label{eqn:EquationName}
\label{fig:FigureName}
\label{thm:TheoremName}
\label{lem:LemmaName}
\label{cor:CorollaryName}
You get the idea. The nice thing about this is that when you use a more sophisticated LATEX
editor like TEXstudio, it will keep track of your labels for you and you can select your labels
from a pull down menu. So the naming scheme with the prefixes will keep the various types of
labels bunched together in alphabetical order.21
20 I use the L
AT X editor called T Xstudio and I don’t have to compile two times for the labels to be correct.
E E
But if you use a more basic TEX editor you might have to.
21 You can get T Xstudio for free here: http://texstudio.sourceforge.net/
E
50
10 Future additions to this document
script sizes inside of math-mode I explained displaystyle but there is also textstyle, script-
style and scriptscriptstyle.
51