Phys 87: Notes For Seminar On How To Make Beautiful Technical Documents With Latex
Phys 87: Notes For Seminar On How To Make Beautiful Technical Documents With Latex
Phys 87: Notes For Seminar On How To Make Beautiful Technical Documents With Latex
Chapter 1
Getting started
This is a quick course on how to use LaTeX to produce typeset quality documents. LaTeX is particularly powerful in producing typeset quality documents
with complex mathematical expressions, but it can do much more. It is also much
more difficult to learn to use than popular comercial editors, like Microsoft Word
or OpenOffice Writer, with their mathematical expression drop-down menus. But
once you pass the steep learning curve it is easy to use and the results are fantastic.
These notes are typeset with LaTeX. This gives you a first impression of what
LaTeX can do. Here is an equation, just to show how well it looks
Z
R 4
1
h(x1 ) (xn )i =
[d] ei d x L(x) (x1 ) (xn ) .
Z
Being designed with scientists in mind, LaTeX helps you number equations and
refer to them in text, organize references and cite them in text, include graphics
and tables, and much of this is done automatically. Once you get used you will
want to write even simple memos with LaTeX, but if all you need is to write simple
memos you probably don;t want to spend the time and effort it takes to learn it.
The course is hands-on. I hope you all bring your laptop to class so we can
work real time with laTeX and learn together.
There are many resources at your disposal for learning LaTeX. You can find
a variety of books in the library and the bookstore. But we don;t have to spend
money. For a textbook we will use the readily available LaTeX Wiki book. By
the way, this text in red is a live link: click on it. You can consult it on-line or
download the pdf version, or both. For our purposes this not only suffices, but it
has too much information. I will guide you through it (that;s what they pay me to
do!) but you are welcome and encouraged to do more: read it and get additional
insights into how the program works and its capabilities.
Once we download and install LaTeX you will find additional documentation
and tutorials with the distribution. These can also be of great help.
1
1.1
Installation
The engine for LaTeX, together with many of the packages that make it all work and
the links/permissions/directories setups, makes installation by hand a big chore.
Installers are available for all platforms that make it easy by automating the process. But you must be patient, its a big download!
We will do this the first day this class meets. Once we get it going we will
discuss some basics, while we wait for the downloads to complete.
1.2
Mac
1.3
Windows
1.4. UNIX
you can read detailed instructions under read the tutorial close to the top. The
installation Wizard asks for where to install MiKTeX (the default, C:\Program
Files (x86)\MiKTeX 2.9 is fine) and then for Preferred paper (default A4) and
Install missing packages on the fly (default Ask me first). Change the preferred
paper to letter.
You should have now a new collection of apps, among them the MikTeX Package
Manager. Things are easier if you follow the detailed instructions for working with
the package manager. Open this app (either find the app or Start>Programs>MiKTeX
2.9>Browse Packages). Select all packages then click the plus sign on the top left,
right under File. You can also accomplish this from entries under the menu bar
(for example, under File you will see an Install All Packages). If you cannot
install all packages at once, try installing first just one, then try again.
Click the Give Back link on the title bar to donate some money. Giving them
ten bucks helps a lot and it is certainly much cheaper than buying a textbook or
comercial software.
1.4
Unix
If you have a unix system I have to assume you are at ease with getting into the
guts of your computer and I can just direct you to the Tex Live -Quick Install at
http://www.tug.org/texlive/quickinstall.html
Click the How you can help link on the home page http://www.tug.org/
texlive/ to donate some time and/or money. Giving them ten bucks helps a lot
and it is certainly much cheaper than buying a textbook or comercial software.
1.5
All three distributions above include TeXworks. This is the editor we will use by
default. That means its the editor I will use to demonstrate stuff in class. It
comes with an integrated viewer making the process of previewing the output to
your documents straightforward.
However, if you are accustomed to some other text editor you can use that too.
By a text editor I mean one that can produce and modify plain text files. Microsoft
Word and OpenOffice Writer are not good for this. But Emacs and Vim are. If
you know these or something like these, and your are well versed in how to use
them, feel free to stick to them. If you do that you will have to be able to run
LaTeX commands from a command line window:
Windows: Open the Command Prompt window by clicking the Start button
If you are not using TeXworks you will also need a pdf viewer. Everyone has
Acrobat Reader. But its ok to use anything else (like Skim and Preview on the
Mac or Nitro and Sumatra on Windows).
My favorite is Emacs. The main reason is that I have used it for ages. But it is
ultra-customizable and has a myriad bells and whistles. Aquamacs is a Mac version
which I like. A windows version is available from https://ftp.gnu.org/gnu/emacs/windows/.
1.6
Compilation
Chapter 2
Basics
2.1
First Project
2.1.1
Debugging
Now, lets insert a mistake into the input file and learn how to deal with it. Go
back to your first project input file and type a line right below Hello world! as
5
CHAPTER 2. BASICS
follows:
\documentclass{article}
\begin{document}
Hello world!
\parr
\end{document}
Now save your work (always save your work before you compile!) and compile.
The green button turn red with a cross, to indicate a problem. The console window
Console output will appear below your typed text, but this time it will stay up. It
should look like this:
This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014) (preloaded
format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./first_project.tex
LaTeX2e <2014/05/01>
Babel <3.9l> and hyphenation patterns for 79 languages loaded.
(/usr/local/texlive/2014/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2014/texmf-dist/tex/latex/base/size10.clo))
(./first_project.aux)
! Undefined control sequence.
l.6 \parr
?
The important thing to note is, first, the compiler stopped because there was an
! Undefined control sequence. and, second, that the problem is l.6 \parr,
meaning it is on line 6 of the file and that the problem is with \parr.
The question mark at the end of the consoles output indicates the program
has stopped and is awaiting further instructions. It is saying, man, I got stuck,
now what? You will notice that below the console window there is a one-line text
box into which you can type your instruction to the compiler. There is a variety
of thing you can tell the compiler, we will go through three options.
Option 1: Help In the text box type ?-ret, that is a question mark and hit
return. You should see
? ?
Type <return> to proceed, S to scroll future error messages,
R to run without stopping, Q to run quietly,
I to insert something, E to edit your file,
1 or ... or 9 to ignore the next 1 to 9 tokens of input,
H for help, X to quit.
?
The first question mark was there before, the second one is the one you typed. The
next few lines remind you of your options. The last question mark tells you that
the program is waiting for your input again.
Option 2: Proceed Now click in the text box and hit return without typing
anything. The program runs through the end, and the output is produced. It looks
exactly as before, with Hello World! near the top. The following is added to the
console window
[1{/usr/local/texlive/2014/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./first_project.aux) )</usr/local/texlive/2014/texmf-dist/fonts/type1/public/a
msfonts/cm/cmr10.pfb>
Output written on first_project.pdf (1 page, 11915 bytes).
SyncTeX written on first_project.synctex.gz.
Transcript written on first_project.log.
There is useful information in this, but lets not get bogged down with all of it
now. Notice only that an output file, first project.pdf, was written. You can
find it in the directory (folder) where you saved your project. So the output is not
just on the output screen. It is in a file that you can share with others and print.
Option 3: Quit Sometimes you know that something has gone horribly wrong
and you just want the compiler to stop immediately, aborting the compilation.
To see how this works, hit the green Typeset button again and when compilation
stops, type x-ret into the type-box. The console then adds below the question
mark the lines:
No pages of output.
Transcript written on "first_project.log".
Since you have told the compiler to stop, there in no output.
CHAPTER 2. BASICS
2.1.2
Fixing it
Go back tou your project. Remove one r from \parr. Your input file reads:
\documentclass{article}
\begin{document}
Hello world!
\par
\end{document}
Typeset this. No error messages any more. Output is produced. The sequence of
characters \par is an allowed sequence. We will study this in detail later. For now
what matters is that we know what to do if we get stuck! We use the compilation
window to find where the error is, fix it by editing the input document and Typeset
again.
2.2
Syntax Basics
From here on these notes are more like bullet points. I will basically list the
topic and we will work with it in class. I will include form time to time specific
instructions, but most instruction will come verbally and on the blackboard, and
we will continuously experiment with the knowledge on our laptops.
Characters: AZ, az, 09, some symbols (comma, period, etc) excluding
reserved characters (see bellow)
White space
Empty lines
Reserved characters: # $ % &
{}\
Latex Groups:
{
\bf This is bold.
}
This is no longer bold.
LaTeX commands: start with \ followed by letters only, the name of the
command.
10
CHAPTER 2. BASICS
\end{center}
\end{document}
Chapter 3
Document classes
We have seen we always start with \documentclass. This command takes two
arguments:
\documentclass[options]{class}
As you have seen you do not have to specify options. We have used the class
article. There are other classes, such as book and letter. See the complete list
in the LaTeX Wikibook. We will stick with article for now.
There are many options. You can enter them in a comma separated list. Lets
try it. Start a new document with
\documentclass[letterpaper,notitlepage,11pt]{article}
\begin{document}
Hello world again!
\end{document}
Now change 12pt for 11pt.
See the complete list of options in the LaTeX Wikibook.
3.2
Using packages
12
\documentclass[letterpaper,notitlepage,11pt]{article}
\usepackage{color}
\begin{document}
Hello {\color{red} world} again!
\end{document}
Packages can take options too:
\usepackage[options]{packagename}
You can call many packages:
\usepackage[options]{packagename1}
\usepackage[options]{packagename2}
\usepackage{packagename3,packagename4,packagename5}
Try this:
\documentclass[letterpaper,notitlepage,11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{color}
\begin{document}
Hello {\color{red} world} again!
\end{document}
3.3
Top Matter
Now we add a title, author, date and abstract to our scientific breakthrough:
\documentclass[letterpaper,notitlepage,11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{color}
\begin{document}
\title{The Origin of Species}
\author{Charles Darwin}
\date{December 2014}
\maketitle
\begin{abstract}
I copy-pasted this from wikipedia in
http://en.wikipedia.org/wiki/On_the_Origin_of
want): On the Origin of Species, published on 24 November 1859, is a
work of scientific literature by Charles Darwin which is considered
to be the foundation of evolutionary biology. Its full title was On
13
3.4
3.4.1
Now you can add some text to the article. Lets all do the same. Go to
http://en.wikipedia.org/wiki/On the Origin of Species and select and copy the first
three paragraphs, from where it says On the Origin of Species, published . . .
until . . . become the unifying concept of the life sciences. Then paste this in your
document, and remove the Hello world again! line.
You will see that you code contains a blank line between paragraphs. Play
with removing the blank line and with inserting extra blank lines and look at the
output. Now you know how to make paragraphs. and that extra blank lines are
ignored.
3.4.2
Line Breaks
3.4.3
Sections
Your article may need different sections and subsections. Insert before the first
paragraph the following:
14
\section{Introduction}
Your document should look something like this:
\documentclass[letterpaper,notitlepage,11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{color}
\begin{document}
\title{The Origin of Species}
\author{Charles Darwin}
\date{\today}
\maketitle
\begin{abstract}
I copy-pasted this from wikipedia (copy-paste what you
want): On the Origin of Species, published on 24 November 1859, is a
work of ... blah blah ... a process
of natural selection.
\end{abstract}
\section{Introduction}
On the Origin blah blah
blah blah life sciences.
\end{document}
You may also need sections of sections, or subsections. Type in
\subsection{Generalities}
right after the \section{Introduction} line and again between the first and second paragraphs
\subsection{More stuff}
Play with leaving blank lines before and after these commands.
There are more sectioning commands. See the Wikibook for the complete list.
3.4.4
Page break
Now insert between the second and third paragraphs the command
\newpage
15
16
dissident anatomists and the general public, but during the first half
of the 19th century the English scientific establishment was closely
tied to the Church of England, while science was part of natural
theology. Ideas about the transmutation of species were controversial
as they conflicted with the beliefs that species were unchanging parts
of a designed hierarchy and that humans were unique, unrelated to
other animals. The political and theological implications were
intensely debated, but transmutation was not accepted by the
scientific mainstream.
\newpage
The book was written for non-specialist readers and attracted
widespread interest upon its publication. As Darwin was an eminent
scientist, his findings were taken seriously and the evidence he
presented generated scientific, philosophical, and religious
discussion. The debate over the book contributed to the campaign by T.
H. Huxley and his fellow members of the X Club to secularise science
by promoting scientific naturalism. Within two decades there was
widespread scientific agreement that evolution, with a branching
pattern of common descent, had occurred, but scientists were slow to
give natural selection the significance that Darwin thought
appropriate. During "the eclipse of Darwinism" from the 1880s to the
1930s, various other mechanisms of evolution were given more credit.
With the development of the modern evolutionary synthesis in the 1930s
and 1940s, Darwins concept of evolutionary adaptation through natural
selection became central to modern evolutionary theory, and it has now
become the unifying concept of the life sciences.
\end{document}
3.5
It helps finding stuff out if the document you are editing has the commands and
environment delimiters in different color than normal text.
In TeXworks go to the pull down menu from the titlebar Format>Syntax Coloring>LaTeX.
Your commands should appear in blue and the environments in dark green
Chapter 4
Math
There is a lot of stuff here. We take it one step at a time. Steep learning curve!
4.1
4.1.1
Equations
Inline equations
Our third project starts with (make sure you select Format>Syntax Coloring>LaTeX
from the title bar form every project from here on!)
\documentclass[11pt]{article}
\begin{document}
My fist equation is $F=ma$.
\end{document}
Now add to the last line:
My fist equation is $F=ma$. My second is \( a=F/m\).
4.1.2
Displayed equations
18
CHAPTER 4. MATH
4.1. EQUATIONS
19
You run this and get in place of \ref{accel} a couple of question marks, as
in ??. At this point we want to find out what went wrong (nothing really as you
will see). We want to look at error messages in the console output, but it has
disappeared form the screen in TeXworks. Force it back by selecting from the top
menu Window > Show Console Output. You should see, among other stuff:
LaTeX Warning: There were undefined references.
LaTeX Warning: Label(s) may have changed. Rerun to get cross-references
right.
Several comments are in order:
(i) The clue to what must be done is in Rerun to get cross-references right..
So hit the typeset button again. Now the typeset document will display the
number 1 in place of \ref{accel}. The two LaTeX warning lines don
come up in the console anymore.
(ii) You can check more detail of the console output in a file created with .log
extension, as in third project.log
(iii) Two passes necessary: first one creates auxiliary file third project.aux,
second one reads it. It keeps track of references/labels. Look at file listing in
folder.
Notice no parenthesis around 1. So try instead (\ref{accel}).
Now, to make the point of labeling obvious, insert before the current equation,
another equation:
...
\begin{equation}
\label{eq:Newton}
F=ma
\end{equation}
is the same as
\begin{equation}
\label{accel}
a=F/m.
\end{equation}
The period is in the right place. I have placed it inside the
displayed equation (\ref{accel}). I want to write enough that there will be a few
lines after the displayed equation blah blah blah. I added equation (\ref{eq:Newton}).
\end{document}
20
4.2
CHAPTER 4. MATH
amsmath package
Now insert
\usepackage{amsmath}
after documentclass and replace equation* for equation in the environment
name.
The same can be accomplished using \nonumber after the equation in the
equation environment (with or without the amsmath package.
Get a copy of the amsmath users guide. There are lots of goodies there, some
of which we explore next.
4.2.1
21
The ampersand before the equal sign, &=, sayas, align at equal sign. The ampersand is used in many LaTeX constructions for alignment.
Notice this comes with one equation number for the whole block so we give it
only one label.
\begin{equation}
\label{manyeqs}
\begin{split}
a+b&=c\\
e+f&=g
\end{split}
\end{equation}
The equations (\ref{manyeqs}) whatever.
For separate numbered aligned equations,
\begin{align}
\label{manyeqs1}
a+b&=c\\
\label{manyeqs2}
e+f&=g
\end{align}
The equations (\ref{manyeqs1}) and
(\ref{manyeqs2}) whatever.
Here is a more complicated situation. You want two equations per line, aligning
the two sets of equations:
\begin{align}
\label{manyeqs1}
a+b&=c & l & = h + n \\
\label{manyeqs2}
e+f&=g& o+p+q &=r
\end{align}
The equations (\ref{manyeqs1}) and
(\ref{manyeqs2}) whatever.
Note the additional & to separate the two equations in one line.
There are a few other constructions. See the amsmath users guide.
4.2.2
22
CHAPTER 4. MATH
4.3
4.3.1
More math
Subscripts and superscripts
4.3.2
Greek
23
There is no \Alpha nor \Beta because they are the same symbols as A and B.
For some symbols there is a variant alternative:
\begin{equation}
\epsilon+\varepsilon = \theta+\vartheta=\phi+\varphi
\end{equation}
See the complete list of math symbols in the Wikibook.
4.3.3
Simple symbols
Some of the common symbols on the keyboard work straightforwardly (in math
mode!):
a+n!-b/c=[a*(bc)] < d > e=|g|
4.3.4
4.3.5
Some symbols that cannot be found in the basic set in can be found elsewhere. A
good place to start is the amssymb package. Load it:
\usepackage{amssymb}
and try the following (in math mode!):
a \gtrsim b \lesssim c
Consult the AMS math list of symbols for more symbols. Really, go there and
try some. Here are a few:
\aleph \beth i, \gimel,
\otimes , \times , \div , \cap , \cup
\infty , \forall , \partial , \hbar ~
24
CHAPTER 4. MATH
4.3.6
But if you cannot find it in amssymb, then what. Almost anything you can dream
of is available. Consult and play with the comprehensive LaTeX symbol list. You
will find all sorts of weird stuff, and sometimes several packages that do similar
things. For example, the packages wasysym, marvosym and mathabx all supply
astronomical symbols. Lets try it:
...
\usepackage{wasysym}
...
\mercury, \earth
...
These work both in text and in math mode. Try both.
The message is that other than the common symbols that you will memorize
from repeated use, other symbols are easy to find in all these resources.
4.3.7
Fractions
4.3.8
25
\prod_1^{10} \omega_k
4.4
This is particularly useful with a little text, but this looks bad:
a=g\quad then implies \quad g=2
which gives a = g
thenimplies
g = 2. We need instead
26
CHAPTER 4. MATH
a\!a~a\!\!a
You can see that normally you want to stay away from this.
Now we can go back to the example above with integrals and re-write:
\int_0^1 \!\!f(x)\, dx,\qquad \sum_{n=0}^7 x_n,\qquad
\prod_1^{10} \omega_k
In text mode, the special character ~ leaves a blank space. But it is special: in
this~word it interprets the whole expression as one word (even if there is a blank
space between this and word). So LaTeX tries to avoid a line break in the middles
of the expression. It is useful to avoid starting a line with, for example, a number.
For example: Your text says blah blah in equation 7 we see blah blah. But after
typesetting you may find . . .
blah blah in equation
7 we see blah blah, that is the line ends with equation and the next one starts
with 7. To avoid this you type blah blah in equation~7 we see blah blah.
It is good practice to do this as you type, so that you dont have to go back
and edit your document every time this occurs.
4.5
4.5.1
Bad:
cos(\theta)=sin(\tfrac{\pi}2-\theta)
Good:
\cos(\theta)=\sin(\tfrac{\pi}2-\theta)
We also have, among others,
\exp(x)=e^x,\quad \ln x=\log x,\quad \sinh(z),\quad \theta=\arcsin P
By now you should know where to find a more complete list!
4.5.2
4.5.3
27
Roots
4.5.4
Vector stuff
4.5.5
dot-dot-dot
28
4.6
CHAPTER 4. MATH
Delimiters
Bad:
(\frac12 a+\frac{x+y}{z+d})
Better:
\left(\frac12 a+\frac{x+y}{z+d}\right)
Other delimiters:
( a ), [ b ], \{ c \}, | d |, \| e \|, \langle f \rangle
The last one is very common to denote averages. They all get resized and dont
have to match:
\left\langle\frac12 a+\frac{x+y}{z+d}\right]
4.6.1
4.6.2
Multiline equations
If you try
\begin{multline}
\left(\frac12 a\\
+\frac{x+y}{z+d}\right)
\end{multline}
you get the error message
! Missing \right. inserted.
<inserted text>
\right .
l.34 \end{multline}
Instead we need
\begin{multline}
\left(\frac12 a \right. \\
\left.+\frac{x+y}{z+d}\right)
\end{multline}
4.6. DELIMITERS
29
This works in any environment that goes over many lines. For example:
\begin{align*}
\left(\frac12 a \right. & \\
&\left. +\frac{x+y}{z+d}\right)
\end{align*}
Note that the alignment symbol cannot be inside the delimiters. The following
gives an error message:
\begin{align*}
\left(\frac12 & a \right. \\
&\left. +\frac{x+y}{z+d}\right)
\end{align*}
4.6.3
Manual Control
Again, LaTeX generally makes the right decision on how to resize delimiters, but
sometimes you want to override it:
( \big( \Big( \bigg( \Bigg(
Now that you have seen it, where would you use it? Here is one example:
(a+f(x)),\quad \left(a+f(x)\right), \quad \big(a+f(x)\big)
Chapter 5
5.1
Matrices
5.1. MATRICES
31
\[
2A+3\times\begin{pmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{pmatrix}
=0
\]
For a determinant
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}
You can use other delimiters by use of the \left and \right commands:
\left(\begin{matrix}
a & b & c \\
d & e & f \\
g & h & i
\end{matrix}\right]
5.1.1
5.1.2
\cdots &
\cdots &
& \ddots
\cdots &
a_{1n} \\
a_{2n} \\
& \vdots \\
a_{mn}
Extra space
32
M = \begin{bmatrix}
\frac{5}{6} & \frac{1}{6} & 0
\\[0.3em]
\frac{5}{6} & 0
& \frac{1}{6} \\[0.3em]
0
& \frac{5}{6} & \frac{1}{6}
\end{bmatrix}
Here em is a distance measure, roughly equal to the width of the letter m. So the
space we have added is 0.3em, that is 30% of the width of m.
The construction \\[distance] can be used also in line breaks. Type the following in text mode:
Here is a short line\\[1in]
and the new line of text continues 1 in below.
5.1.3
Array
5.2. TABLES
33
5.2
Tables
We often need tables to display information succinctly. Tese are not matrices,
they belong in text.But they look like matrices without delimiters, only with text
entries. So the construction is similar.
34
5.2.1
This is basically the same as array but with text entries (and in text mode). So in
the last example change the environment, add a few vertical and horizontal lines
and take it out of the displayed equation:
\documentclass[11pt]{article}
\begin{document}
Here is a table:
\begin{tabular}{r||r|r}
-a & -b & c \\
\hline\hline
d & e & f \\
\hline
g & h & i
\end{tabular}
\end{document}
A more interesting table has actual text in it and occupies its own paragraph:
\documentclass[11pt]{article}
\begin{document}
Here is a table:
\begin{tabular}{c||l|l}
Jersey & First Name & Last Name \\
\hline\hline
10 & Cristiano & Ronaldo \\
\hline
11 & Didier & Drogba
\end{tabular}
\end{document}
If you want it centered you should use the center environment. Try it!
Fixed Width Columns: Text Wrapping
Sometimes the text in one of the entries in the column is long. LaTeX adjusts the
table by making the column wider. You may not want this:
\begin{center}
\begin{tabular}{c||l|l}
Jersey & First Name & Last Name \\
\hline\hline
5.2. TABLES
35
(Pele)
36
(Pele)\\
The command \cline takes as argument the rangle of columns over which you
want the horizontal line. It can be only one column, in which case the start and
end of the range of columns is the same. For example, writing \cline{2-2} instead
of \cline{2-3} would have given a line across column 2 only, rather than columns
2 and 3 in the example above.
Breather: Good coding practice
Without change to the output, you can code the above as
\begin{center}\begin{tabular}{c||l|p{1in}}Jersey&First Name&Last Name\\
\hline\hline
10&Cristiano&Ronaldo\\\cline{2-3}&Edson&
Arantes\newline do Nascimento (Pele)\\\hline 11&Didier&Drogba
\end{tabular}\end{center}
You decide which is clearer. Think possible errors and debugging!
Partial vertical lines
How do we do the opposite, that is, a vertical line that does not span all rows?
Use \multicolumn with three arguments: the first is the number of columns, the
second is alignment, l, c, r with possible vertical bars, and the third one is the
text:
\begin{center}
\begin{tabular}{|c||l|p{1in}|}
\hline
\multicolumn{3}{|c|}{Dream Team}\\
\hline
Jersey & First Name & Last Name \\
\hline\hline
10 & Cristiano & Ronaldo \\
\cline{2-3}
5.2. TABLES
& Edson & Arantes\newline do Nascimento
\hline
11 & Didier & Drogba\\
\hline
\end{tabular}
\end{center}
37
(Pele)\\
38
\begin{tabular}{c}
2.30\\
100.23\\
0.12345
\end{tabular}
\end{center}
Instead use:
\begin{center}
\begin{tabular}{r@{.}l}
2&30\\
100&23\\
0&12345
\end{tabular}
\end{center}
Here is whats going on. We used two columns, one right justified and the next
left justified. The separator is not a vertical line but whatever goes in @{separator
symbol}, in this case the separator symbol being a period.
5.3
Advanced Tables
Several packages, like array, tabularx and tabulary, give you many more options
for constructing complicated tables. You know where to find more details. Here
is just one example, using the switch \rowcolors provided by the xcolor package
with option table:
\documentclass[11pt]{article}
\usepackage[table]{xcolor}
...
\begin{document}
...
{ \rowcolors{1}{yellow}{pink}
\begin{tabular}{|c||l|p{1in}|}
\hline
\multicolumn{3}{|c|}{Dream Team}\\
\hline
Jersey & First Name & Last Name \\
\hline\hline
10 & Cristiano & Ronaldo \\
39
(Pele)\\
Chapter 6
6.1
Use this in documents to (i) automatically place the table, (ii) automatic table number referencing, (iii) include a caption. Example, using something from
fourth project.tex:
Here is some text. In table \ref{mytable} we show our Dream Team
\begin{table}[b]
\begin{center}
\begin{tabular}{|c||l|p{1in}|}
\hline
\multicolumn{3}{|c|}{Dream Team}\\
\hline
Jersey & First Name & Last Name \\
\hline\hline
10 & Cristiano & Ronaldo \\
\cline{2-3}
& Edson & Arantes\newline do Nascimento (Pele)\\
\hline
11 & Didier & Drogba\\
\hline
\end{tabular}
40
41
\end{center}
\caption{
This table shows blah blah \label{mytable}
}
\end{table}
The environment table takes one positioning argument, b in this example. It
stands for, place this table at the bottom of a page, as close as possible to where
it was inserted in the code. You can also insert it h for here, and t for at the top
of the page. The top of the page is the most commonly used.
The command \caption takes an argument text that constitues the caption to
the table. You can insert a label and refer to it just as we did with equations. The
counter for table numbers is independent of the counter for equation numbers.
6.1.1
List of tables
Sometimes in a very long document you want a list of tables showing where to find
each table. Just add \listoftables. Add this now to the previous example, just
before your \end{document}:
\newpage
\listoftables
6.2
Inserting Graphics
42
The attribute width=dimen sets the width of the image to the desired dimension.
This and height=dimen are the most common, but there are a few others, like
scale=factor which amplifies the image by the factor (reduces it if factor is smaller
than 1) and trim=l b r t which is used for cropping by lengths l, b, r and t, from
the left, bottom right and top, respectively. For trim to work you have to add the
attribute clip (or rather set clip=true but adding just clip works):
\includegraphics[trim= 8cm 0cm 0cm 0cm,clip ]{gl-5-triton.png}
See the complete list of attributes in the WikiBook.
Try this:
\includegraphics[width=2in, height=4in]{gl-5-triton}
The extension is not necessary: LaTeX knows to look for .png files as graphic
files. In fact, if you are typesetting with pdfLaTeX, and we are, and are using the
graphicx package, and we are, then LaTeX looks for files with extensions .jpg,
.png and pdf, and assumes that if you did not write an extension that it will
find the full file with extension in the current directory (and looks for all three
extensions).
6.2.1
Graphics folder
6.3
43
Figure Environment
6.3.1
List of figures
6.4
More. . .
We will not do this in the course but you may want to play with putting boxes
around images, text wrapping, inserting text on top of an image, including full
pdf pages, and more. Consult the Wikibook. Play with it! There is also a lot of
information there about converting among different formats abd editing graphics.
Chapter 7
Itemized lists
7.1.1
45
7.2
Numbered lists
46
7.2.1
47
7.3
Description Lists
Chapter 8
Biblioraphy
A painful aspect of composing a technical or academic document is the bibliography. We would like to have a simple way of citing papers or books, and to list
them accordingly at the back of our document. Sometimes we are asked to have
the listing of references alphabetized, sometimes the publisher (or prof) wants us
to list them in the order in which they are used/cited in the document. The style
of the references may change from publisher to publisher too. What a pain!
Fortunately LaTeX can take care of all this for you.
8.1
49
Note that the labels are completely arbitrary text tokens. You choose! Now
add before the \end{document} the following references:
\begin{thebibliography}{99}
\bibitem{albert1}
On the Relation between the Expansion and the Mean Density of the Universe,
A. Einstein, W. de Sitter, Proc.Nat.Acad.Sci. 18 (1932) 213-214
\bibitem{einsteinRosen}
On Gravitational waves,
Albert Einstein, N. Rosen, J.Franklin Inst. 223 (1937) 43-54
\end{thebibliography}
The blank lines between bibitems are ignored by LaTeX: you can add more or have
none at all. Blank lines within a bibitem produce unwanted line breaks.
As with other referencing in LaTeX the first time you run this you get question
marks in place of citing references, as in Einstein discussed . . . of the universe[?],
and then he went on to say something about gravitational waves[?], whatever they
are. The Console output contains,
LaTeX Warning: Citation albert1 on page 1 undefined on input line 15.
LaTeX Warning: Citation einsteinRosen on page 1 undefined on input line 16.
and then lower down, near the end,
LaTeX Warning: There were undefined references.
LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.
The last line tell you how to fix the problem. Just run LaTeX again. But the
Warning messages are more generally useful for debugging problems, as we will
discuss in lecture. So typeset again. Now the question marks are replaced by [1]
and [2], and the Console output gives no Warning messages.
Try this: List the two references in the bibliography in the reverse order. What
happens? Make sure you look at the typeset output and the Console output after
each time you run LaTeX now.
The mandatory argument of the thebibliography environment indicates the
width of the field used for enumerating. It counts the digits. So if the argument is
1 or 7 or 9 it means one digit which is useful for a list with no more than 9 entries.
Similarly if the argument is 34 or 87 or 99 it tells LaTeX you will use two digits
for the list. Thats 99 references maximum. So it is common to use as argument
9, or 99, or 999. It is never a bad idea to overestimate the number of references in
your work.
50
CHAPTER 8. BIBLIORAPHY
8.2
BibTeX is more flexible, but more difficult to use. It has the great advantage
that bibliography files are stored and can be reused in many documents. They are
databases. Only the references you need are used.
We need to
1. Make one (or more) plain text bibliography files (databases), extension .bib
2. Include \cite{bibitemlabel} in text as before
3. Include
\bibliographystyle{plain}
\bibliography{samplebib1}
before \end{document}
4. Typeset with pdfLaTeX. Then run BibTeX. Then typeset twice with pdfLaTeX.
To run BibTeX choose BibTeX from the drop-down menu in TeXworks (the one
that normally says pdfLaTeX).
This is a lot to swallow at once. Lets go one step at a time
8.2.1
The bibliography files are plain text files. You can create them with any plain text
editor. We can use TeXworks for this. From the File menu choose New. Enter
the bibliography information (bellow), and when you are ready Save as . . . . In the
Save dialogue box make sure
1. to save in the same folder as you LaTeX document
2. to choose BibTeX databases (*.bib) from the Files of Type: drop-down menu.
51
52
CHAPTER 8. BIBLIORAPHY
This is almost what we want. The label for the reference, einstein1937gravitational
is not what I use in my LaTeX file to cite this reference. So I edit the file to change
the first line to read @article{einsteinRosen,
Next I look for einstein de sitter On the Relation between the Expansion and
the Mean Density of the Universe and paste into my file
@article{einstein1932relation,
title={On the Relation between the Expansion and the Mean Density of the Universe},
author={Einstein, Albert and De Sitter, Willem},
journal={Proceedings of the National Academy of Sciences of the United States of America
volume={18},
number={3},
pages={213},
year={1932},
publisher={National Academy of Sciences}
}
and edit the first line to read @article{albert1,
Many journals give you the full reference in BibTeX format. Example:
1. Go to www.sciencemag.org
2. Navigate: Science Journals > Science > Science Home > Table of Contents
> Reports
3. For any report navigate Abstract > Download Citation (found under Article
Tools on left margin) > BibTeX
4. Copy and paste to your database
You are out of luck with Nature. But fine with many journals, including American Physical and Chemical Societies journals, IEEE journals, and more.
53
54
CHAPTER 8. BIBLIORAPHY
8.2.2
BibTeX styles
Chapter 9
Dimensions
55