You are on page 1of 24

A LATEX Problem Set Template (With Solutions)

Garrett Darl Lewis


Department of Politics
Princeton University
January 23, 2016

Contents
1

The Main Body


1.1 Text . . . . . .
1.2 References . . .
1.3 Theorems, &c.
1.4 Math . . . . . .
1.5 Code . . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

Tables

Pictures
3.1 Drawing in LATEX . . . . . . . .
3.1.1 The Prisoners Dilemma
3.1.2 Probability Tree . . . . .
3.1.3 Red-Black Tree . . . . .
3.1.4 H-tree and B-tree . . . .
3.1.5 Mindmap and Layers . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

2
3
3
5
6
8
8

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

Conclusion

.
.
.
.
.
.

12
13
13
14
16
18
20
23

Abstract
This document demonstrates the use of many features which are likely to be useful in
preparing LATEX documents. There are sections demonstrating basic text, math, code, and
images (both internally and externally produced). I also throw in little features such as bold
and italic sections, which you can find the code for within the TEX file. If there are things that
you would like to see added, let me know. This is just a quick collection of features I thought
to throw together in an evening. Of course, there are any number of websites (and presumably

Department of Politics, Princeton University 130 Corwin Hall, Princeton, NJ 08544-1012 E-mail:
glewis@princeton.edu,

books, but who are we kidding) which go through this same material and heaps more. This
document merely attempts to demonstrate how many of the common features available in
this typesetting environment may be applied in context. For the full range of capabilities,
I highly recommend using online resources which include the complete documentation for
these commands. Whenever I introduce a new feature, though, I include a section in blue
which demonstrates the code used to produce the output in question. If you are completely
new to LATEX, you will want to download some software to begin. I use TeXworks, but that
is only one of many options out there. Many have a stronger UI than mine, and the most
bare-bones versions have no GUI.

The Main Body

\ s e c t i o n { The Main Body }\ l a b e l { mainbody }


The following code is sufficient to produce a basic document. In the preamble, it defines the
type of document being created (documentclass), any special packages that might be required to
compile the document (usepackage), as well as a statement of the title and author of the work. As
I introduce more sophisticated capabilities of the software, I will also discuss more packages and
settings that can be called in the preamble when they are needed.
At the end of the preamble, the command, \begin{document}, begins the main body. After this
command, I tell the software to include a title, table of contents, and an abstract. I then insert the
main text, and finally call a bibliography using references written to the file, BiblioTemp.bib. The
command, \end{document} ends the document. After saving, I typically run three commands in
my compiler to prepare the document and ensure that all of the auxiliary files have been properly
updated. These three commands are pdfLaTeX, MakeIndex, and BibTeX. It is generally not necessary to run all three, but as it it better than spending hours on end trying to figure out why your
document is not compiling properly, only to find out that your *.bbl file was out of date.
\ documentclass [ l e t t e r ,12 pt ]{ a r t i c l e }
%T h e r e a r e s e v e r a l p a c k a g e s which I u s e t h r o u g h o u t t h e f i l e . Not
a l l w i l l be h e l p f u l i n e v e r y document , b u t t h e f i r s t s e v e r a l
included here are almost always u s e f u l .
I try to note the
r e l e v a n t h e a d i n g m a t e r i a l i n t h e body below a s I u s e i t .
% Important packages
\ u s e p a c k a g e { l a t e x s y m , amsmath , amssymb , amsthm , n a t b i b , s e t s p a c e }
% D e f i n e t h e t i t l e , \&c
\ t i t l e {My T i t l e }
\ a u t h o r {Me\ t h a n k s { D e p a r t m e n t o f P o l i t i c s , P r i n c e t o n U n i v e r s i t y }\\
D e p a r t m e n t o f P o l i t i c s \\ P r i n c e t o n U n i v e r s i t y }
\ d a t e {\ t o d a y }
\ doublespacing

% Use d o u b l e s p a c i n g

\ b e g i n { document }
\ maketitle
\tableofcontents

% B e g i n t h e body
% Generate the t i t l e
% G e n e r a t e a ToC

\ begin { a b s t r a c t }
Put your a b s t r a c t here !
\ end { a b s t r a c t }
\ s e c t i o n { The Body}
H e l l o World !
\ newpage
% Skip t o t h e n e x t page
\ b i b l i o g r a p h y { BiblioTemp } % I n s e r t a b i b l i o g r a p h y
\ end { document }
% E v e r y t h i n g down h e r e i s i g n o r e d

1.1

Text

Basic text is pretty easy. Just define your document class, open your document, and start typing,
maybe adding a chapter or section along the way. However thats pretty boring, especially if
youre not an English major and have to use things like, you know, MATH. Lets look at some of
the options. . . 1
In the previous paragraph, we saw features like italics and bold face type, as well as an ellipsis
and a footnote. These are typeset as,
. . . h a v e t o u s e t h i n g s l i k e , \emph{ you know } , \ t e x t b f {MATH} . Let
s l o o k a t some o f t h e o p t i o n s \ d o t s \ f o o t n o t e { I want t o q u i c k l y
p o i n t o u t a few f e a t u r e s t h a t c a n d r i v e someone c r a z y i f t h e y a r e
n o t p a y i n g a t t e n t i o n . Many symbols , s u c h a s \& , \ t e x t a s c i i t i l d e
, and \ t e x t b a c k s l a s h \ r e q u i r e s p e c i a l n o t a t i o n i n \LaTeX , s i n c e
t h e s y m b o l s t h e m s e l v e s h a v e c e r t a i n meaning i n t h e s o f t w a r e .
Make s u r e you p r o o f r e a d y o u r f i n i s h e d documente s p . where you
a r e l i k e l y t o u s e symbols t o e n s u r e t h a t t h e r e a r e no odd
missing items or s t r a n g e a d d i t i o n s .}

1.2

References

\ subsection { References }
In many instances, you want to refer to something that is in a different section of your document
or even in a different document. To do the former, you need only label your sections, equations,
theorems, &c. as you go along, just as I did here in Section 1.1. By default, numbers are used to
1

I want to quickly point out a few features that can drive someone crazy if they are not paying attention. Many
symbols, such as &, , and \ require special notation in LATEX, since the symbols themselves have certain meaning in
the software. Make sure you proofread your finished documentesp. where you are likely to use symbolsto ensure
that there are no odd missing items or strange additions.

reference items, with a unique counter for each environment (sections, equations, &c.), but there
are ways to change that. In sections 1.3 and 1.4, I mention counter tools and the tag feature.
. . . l a b e l y o u r s e c t i o n s , e q u a t i o n s , t h e o r e m s , \&c . a s you go a l o n g
, j u s t as I did here in Section \ r e f { t e x t } . . .
And of course there is the question of references to other works. The most useful commands
for that are cite and citep (?) (Ignore the fact that Im citing myself to back up this claim). The
bibliography style I use here is JPE and I compile my references using BIBTEX. Of course, different
journals and subfields may prefer different formats from these presented here, but hopefully this
will get you off the ground.
. . . u s e f u l commands f o r t h a t a r e \emph{ c i t e } and \emph{ c i t e p } \
c i t e p { Lewis14 } . . .
. . . I c o m p i l e my r e f e r e n c e s u s i n g $\ mathrm {B{\ s c r i p t s t y l e { IB }}} $\
TeX . . .
When referencing you bibliography, the program will skip over citations if it they cannot be
found in a *.bib file (that has also been appropriately compiled using BIBTEX). If this happens,
you will receive a question mark in lieu of the citation. Luckily, the *.bib file is easy to assemble,
since it is little more than a list of all your sources:
@ARTICLE{ Lewis14 ,
AUTHOR = { Lewis , D a r l } ,
TITLE
= {A \LaTeX T e m p l a t e } ,
YEAR
= {2014} ,
JOURNAL = {A J o u r n a l } ,
VOLUME = { 1 } ,
NUMBER = { 1 } ,
PAGES
= {112}
}
%
@BOOK{ Lewis14a ,
AUTHOR = { Lewis , D a r l } ,
TITLE
= {A \LaTeX Book } ,
YEAR
= {2014} ,
PUBLISHER = {My Computer } ,
ADDRESS = {NJ } ,
}
%
@INCOLLECTION{ Lewis14b ,
AUTHOR = { Lewis , D a r l } ,
TITLE
= { A n o t h e r \LaTeX T e m p l a t e } ,
YEAR
= {2014} ,
EDITOR = { Lewis , D a r l } ,
BOOKTITLE = { A n o t h e r \LaTeX Book } ,
PUBLISHER = {My Computer } ,
4

ADDRESS

= {NJ } ,

}
To insert the list of references at the end of your document, simply define the bibliography
style and call the bibliography before you end the document. The nocite option allow the software
to include references in the works cited section whether or not thay have been cited within the
document.
\ bibliographystyle { jpe }
\ n o c i t e {}
\ b i b l i o g r a p h y { BiblioTemp }

1.3

Theorems, &c.

There are a few environments readily available for writing theorems and proofs and the like, but
sometimes you have to define your own. Luckily, thats easy to do. In the preamble of my document, I have defined four environments, Theorem, Definition, Proposition, and Corollary, using
these commands:
\ newtheorem { t h e o r e m }{ Theorem }
\ newtheorem { d e f i n i t i o n }{ D e f i n i t i o n }
\ newtheorem { p r o p o s i t i o n }{ P r o p o s i t i o n }
\ newtheorem { c o r o l l a r y }{ C o r o l l a r y }
LATEX counts each environment separately unless specified otherwise using any of the counter
tools such as addtocounter and newcounter.
Theorem 1. All governments are unjust.
Proof. Consider an arbitrary government. Since it is arbitrary, it is obviously unjust. The assertion
being correct for an arbitrary government, it is thus true for all governments.
\ begin { theorem }
All governments are u n j u s t .
\ end { t h e o r e m }
\ begin { proof }
C o n s i d e r an a r b i t r a r y g o v e r n m e n t . S i n c e i t i s a r b i t r a r y , i t i s
o b v i o u s l y u n j u s t . The a s s e r t i o n b e i n g c o r r e c t f o r an a r b i t r a r y
government , i t i s t h u s t r u e f o r a l l governments .
\ end { p r o o f }
Definition 1 (Circular Definition). See: Definition, Circular.
\ begin { d e f i n i t i o n }[ C i r c u l a r D e f i n i t i o n ]
See : \emph{ D e f i n i t i o n , C i r c u l a r } .
\ end { d e f i n i t i o n }
Proposition 1. Will you marry me?
5

Corollary 1. And sign a prenup.


\ begin { p r o p o s i t i o n }
W i l l you m a r r y me?
\ end { p r o p o s i t i o n }
\ begin { c o r o l l a r y }
And s i g n a p r e n u p .
\ end { c o r o l l a r y }
Proposition 2 (Humor). This section has some really bad humor.
Proof. Reread the section. The rest is trivial.
\ b e g i n { p r o p o s i t i o n } [ Humor ]
T h i s s e c t i o n h a s some r e a l l y bad humor .
\ end { p r o p o s i t i o n }
\ begin { proof }
Reread t h e s e c t i o n .
\ end { p r o o f }

1.4

The r e s t i s

trivial .

Math

The value of LATEX becomes most apparent when writing equations. After a while, it becomes very
easy to simply type equations as you go in a way that does not accrue with many other software
platforms. Most mathematical symbols are simply a \ followed by the name of the symbol, and
most of the common structures you are likely to encounter are designed to be implemented with
relative ease and flexibility. If you want to write an inline equation, just use the $ like this a+b = c.
. . . I f you want t o w r i t e an i n l i n e e q u a t i o n , j u s t u s e t h e \$ l i k e
t h i s $a +b= c$ .
If you want to to write something longer or make it more visible by pulling it out of the text,
though, use the equation environment:
ei + 1 = 0

(1)

\ begin { equation }
\ mathrm { e } { i \ p i }+1=0
\ end { e q u a t i o n }
If you dont want to attach a number, there are several options. I usually just go with this one...


v

+ v v = p + T + f
t

\[
\ r h o \ l e f t ( \ f r a c {\ p a r t i a l \ m a t h b f { v }}{\ p a r t i a l t }+\ m a t h b f {v }\ c d o t \
n a b l a \ m a t h b f {v }\ r i g h t ) =\ n a b l a p+\ n a b l a \ c d o t \ m a t h b f {T}+ f
\]
And of course, if you want to name a multiline equation,
a+b=c
2(a + b) (a + b) = 2c c
2a + 2b 2c = a + b c
2(a + b c) = a + b c
2=1

(Bad Math)

\ b e g i n { e q u a t i o n }\ t a g {Bad Math }
\ begin { s p l i t }
a+b&=c \\
2 ( a+b ) (a+b ) &=2cc \\
2 a +2b2c&=a+bc \\
2 ( a+bc )&=a+bc \\
2&=1
\ end { s p l i t }
\ end { e q u a t i o n }
Note the use of the split environment and the & to align equations in this system.
There are other ways of separating an equation into multiple lines with slightly varying effects.
These include, multline, align, gather, and eqnarray, among others.
Sometimes you may want to nest a few related equations under one number. Try this:
a+b=c
c=d+e
d=f +g
e=h+i

(2a)
(2b)
(2c)
(2d)

\ b e g i n { s u b e q u a t i o n s }\ l a b e l { mygrp }
\ begin { align }
a+b&=c \ l a b e l { f i r s t g r p }\\
c&=d+e \\
d&=f +g \\
e&=h+ i
\ end { a l i g n }
\ end { s u b e q u a t i o n s }
If you label them properly, you can easily refer back to previous equations like (2). That makes
it easy to talk about how similar (2a) is to the first equation I presented here in Section 1.4.
7

I f you l a b e l them p r o p e r l y , you c a n e a s i l y r e f e r b a c k t o p r e v i o u s


e q u a t i o n s l i k e \ e q r e f { mygrp } .
For more on mathematical features and symbols, I will refer you to a well-written document in
?, which offers a quick run-down of the capabilities of the software. For a comprehensive list of
symbols available, both in the equation environment and the text environment, see ?.

1.5

Code

Sometimes you may want to put code directly into your document. The easiest way is with the
verbatim package, something you will see in several of the images that come later in this document.
% Dont do this!
i=1
while i>0
i++
end;
\ begin { verbatim }
% Don t do t h i s !
i =1
w h i l e i >0
i ++
end ;
\ end { v e r b a t i m }
Of course, with verbatim, youre not restricted to writing code. Any text is fine as long as youre
willing to live with that plain ASCII feel. On the other hand, if you are looking for something
more sophisticated to insert plain text or code, as I have been doing throughout this document,
you may prefer to use the more-versatile listings package, which has been explicitly designed for
inserting code into LATEX files. To implement that package, simply replace the word, verbatim,
in the previous block of code with the word, lstlisting. Throughout this document, I have use the
following settings, defined in the preamble:
\ l s t s e t { b r e a k l i n e s = t r u e , b a c k g r o u n d c o l o r =\ c o l o r { b l u e ! 1 0 } , l a n g u a g e
= , b r e a k i n d e n t =0 p t }

Tables

Tables in LATEX, like everything else, take some getting used to, but they can come out very nicely
once youve taken the time to assemble them. Many statistical packages (including Stata and R)
also offer macros which will output data in LATEX format to save you the trouble. By default, the
software places tables where they best fit in the text, which is not always precisely where they are
written in the source.

Table 1: The auto data


Model 1
Coef. p-value
Weight (lbs.)
Mileage (mpg)
Foreign*Mileage
Foreign car type
Constant
Adj. R2
No. of cases

1.747
.050

.008
.567

1.946
.273
74

.590

Model 2
Coef. p-value
4.614
.263
.307
11.240
14.450
.526
74

.000
.020
.006
.000
.002

Source: auto.dta

\ b e g i n { t a b l e }\ c a p t i o n { The a u t o d a t a \ l a b e l { a u t o }}
\ begin { center }
\ b e g i n { t a b u l a r }{ l {2}{ r r }}
\ hline
&\m u l t i c o l u m n {2}{ c }{ Model 1}
&\m u l t i c o l u m n {2}{ c }{ Model 2}\\
& Coef . & $p$v a l u e
& Coef . & $p$v a l u e \\
\ h l i n e \\[ 1 ex ]
Weight ( l b s . )
& 1.747 & .008 & 4.614
& .000\\
M i l e a g e ( mpg )
& .050 & . 5 6 7 & . 2 6 3
& .020\\
Foreign Mileage
&
&
& .307
& .006\\
Foreign car type
&
&
& 11.240 & .000\\
Constant
& 1 . 9 4 6 & . 5 9 0 & 14.450 & . 0 0 2 \ \
\ hline
Adj . $R 2 $
& .273 &
& . 5 2 6 & \\
No . o f c a s e s
& 74
&
& 74
& \\
\ hline
\ small \ t e x t i t { Source :} auto . dta
\ end { t a b u l a r }
\ end { c e n t e r }
\ end { t a b l e }
That table wasnt very complicated. Lets look at some other features now which add a little
fun to to the process. Here I am going to write a table which has been turned sideways (ostensibly
because it fits better in landscape mode) using the rotating package. To avoid interfering with the
surrounding text, this table is placed at the end of the document. Rewriting this second table for
statistical data would not be a challenging task.
\ begin { sidewaystable }[ h ]
\ caption { Performance After Post F i l t e r i n g } % t i t l e of the t a b l e
\ centering % centering table
\ b e g i n { t a b u l a r }{ l c c r r r r r r r } % c r e a t i n g 10 c o l u m n s
9

\ h l i n e \ h l i n e \\[ 1 ex]% i n s e r t i n g d o u b l e l i n e
Audio &A u d i b i l i t y & D e c i s i o n &\m u l t i c o l u m n {7}{ c }{Sum o f
E x t r a c t e d B i t s } \\ [ 0 . 5 ex ]
\ h l i n e % i n s e r t s s i n g l e l i n e
& &s o f t &1 & $1$ & 1 & 1 & $1$ & $1$ &
1 \\[ 1 ex ]
\ r a i s e b o x { 1 . 5 ex }{ P o l i c e } & \ r a i s e b o x { 1 . 5 ex }{5}& h a r d & 2 & $4$ &
4 & 4 & $2$ & $4$ & 4 \ \ [ 1 ex ]
& &s o f t &1 & $1$ & 1 & 1 & $1$ & $1$ &
1 \\[ 1 ex ]
\ r a i s e b o x { 1 . 5 ex }{ B e e t h o v e n } & \ r a i s e b o x { 1 . 5 ex }{5}& h a r d &8 & $8$
& 2 & 8 & $8$ & $8$ & 6 \ \ [ 1 ex ]
& &s o f t & 1 & $1$ & 1 & 1 & $1$ & $1$
& 1 \\[ 1 ex ]
\ r a i s e b o x { 1 . 5 ex }{ M e t a l l i c a } & \ r a i s e b o x { 1 . 5 ex }{5}& h a r d &4 & $8$
& 8 & 4 & $8$ & $8$ & 8 \ \ [ 1 ex ]
\ h l i n e % i n s e r t s s i n g l e l i n e
\ end { t a b u l a r }
\ l a b e l { t a b : LPer }
\ end { s i d e w a y s t a b l e }
Of course, sometimes you may have a desire to draw tables for not-statistical purposed. Below
are several small tables with different features that you may find helpful. These examples use the
color and cancel packages, as well as a new command, hilight, defined in the preamble:
\newcommand {\ h i l i g h t } [ 1 ] { \ c o l o r b o x { y e l l o w }{#1}}
1. Prisoners Dilemma
T
B

L
1, 1
2, 1

R
1, 2
0, 0

L
1, 1
2, 1

R
1, 2
0, 0

2. Prisoners Dilemma # 2
T
B
3. Prisoners Dilemma # 3

Agent 2

Agent 1
L
R



T 
1, 1
1, 2
B 2, 1
0,0

10

\ begin { enumerate }
\ i t e m P r i s o n e r s Dilemma
\ s e t l e n g t h {\ a r r a y c o l s e p }{0 p t }
\[
\ b e g i n { a r r a y }{ c c c c c c }
& & \ quad L \ quad & & \ quad R \ quad & \ \ [ . 2 ex ]
\ c l i n e {35} T \ quad & \ v l i n e {} & \ quad 1 , 1 \ quad & \ v l i n e {} & \
quad 1 ,2 \ quad & \ v l i n e {} \\
\ c l i n e {35} B \ quad & \ v l i n e {} & \ quad 2 , 1 \ quad & \ v l i n e {} & \
quad 0 , 0 \ quad & \ v l i n e {} \\
\ c l i n e {35}
\ end { a r r a y }
\]
\ i t e m P r i s o n e r s Dilemma \# 2
\ s e t l e n g t h {\ a r r a y c o l s e p }{0 p t }
\[
\ b e g i n { a r r a y }{ c c c c c c c }
\ c l i n e {26} \ v l i n e {} & \ quad
& \ v l i n e {} & \ quad L
\ quad
& \ v l i n e {} & \ quad R
\ quad & \ v l i n e {} \\
\ c l i n e {26} \ v l i n e {} & \ quad T \ & \ v l i n e {} & \ quad 1 , 1 \ quad &
\ v l i n e {} & \ quad 1 ,2 \ quad & \ v l i n e {} \\
\ c l i n e {26} \ v l i n e {} & \ quad B \ & \ v l i n e {} & \ quad 2 , 1 \ quad &
\ v l i n e {} & \ quad 0 , 0 \ quad & \ v l i n e {} \\
\ c l i n e {26}
\ end { a r r a y }
\]
\ i t e m P r i s o n e r s Dilemma \# 3
\ s e t l e n g t h {\ a r r a y c o l s e p }{0 p t }
\[
\ b e g i n { a r r a y }{ c c c c c c c c }
& & &\m u l t i c o l u m n {5}{ c }{ Agent \ 1} \\
& & \ quad
& & \ quad L \ quad & & \ quad R \ quad &
\\
\ c l i n e {47}
Agent \ 2 \ & & \ quad T & \ v l i n e {} & \ c a n c e l {\
quad 1 , 1 \ quad } & \ v l i n e {} & \ quad 1 ,2 \ quad & \ v l i n e {} \\
\ c l i n e {47}
& & \ quad B & \ v l i n e {} & \ quad 2 , 1 \ quad & \
v l i n e {} & \ h i l i g h t {\ quad 0 , 0 \ quad}& \ v l i n e {} \\
\ c l i n e {47}
\ end { a r r a y }
\]
\\
\ end { e n u m e r a t e }

11

Firm A

And the same game in an alternate, fancier format:

Defect

Payoff Matrix
Firm B
Defect Cooperate
0
2
1
0
1

Cooperate
2

1
1

\ begin { center }
\ b e g i n { t a b u l a r }{ c c c c c }
&&\m u l t i c o l u m n {2}{ c }{ P a y o f f M a t r i x }\\
&&\m u l t i c o l u m n {2}{ c }{ Firm B}\\
&&D e f e c t&C o o p e r a t e \\
\ c l i n e {34}
\ r a i s e b o x { 0.25cm}{\ r o t a t e b o x {90}{A}}&\ m u l t i c o l u m n {1}{ p { 1 . 5 cm}}{
D e f e c t }&
\ m u l t i c o l u m n { 1 } { | p { 1 . 5 cm}}{\ h f i l l \ h i l i g h t { $0$ }\ n e w l i n e \ h i l i g h t {
$0$ }\ h f i l l }&
\ m u l t i c o l u m n { 1 } { | p { 1 . 5 cm } | } { \ h f i l l $2$ \ n e w l i n e $1$ \ h f i l l }\\
\ c l i n e {34}
\ r a i s e b o x { 0.25cm}{\ r o t a t e b o x {90}{ Firm }}&\ m u l t i c o l u m n {1}{ p { 1 . 5 cm
}}{ C o o p e r a t e }&
\ m u l t i c o l u m n { 1 } { | p { 1 . 5 cm}}{\ h f i l l $1$ \ n e w l i n e $2$ \ h f i l l }&
\ m u l t i c o l u m n { 1 } { | p { 1 . 5 cm } | } { \ h f i l l \ c a n c e l { $1$ }\ n e w l i n e \ c a n c e l {
$1$ }\ h f i l l }\\\ c l i n e {34}
\ end { t a b u l a r }
\ end { c e n t e r }

Pictures

Here is an image with a caption included using the graphicx package. I have placed it inside a
figure to make manipulation easier (i.e. placement within the paper, centering, &c).
\ begin { f i g u r e }[ h ! ]
\ c a p t i o n { I f you l i k e n e r d y comics , t r y \emph{ xkcd } .
Caltech !}
\ centering
\ i n c l u d e g r a p h i c s [ w i d t h =\ t e x t w i d t h ] { n a s h . png }
\ end { f i g u r e }

12

Also , go

Figure 1: If you like nerdy comics, try xkcd. Also, go Caltech!

3.1

Drawing in LATEX

Of course, sometimes you dont have a preexisting picture and want to draw one yourself. Here are
a few ways you can do that using the TikZ package. I start with some that will probably be more
useful and move up to a couple of much more complicated diagrams. Of course there are many
more options out there if you search for TikZ examples. Look at ? to start.
3.1.1

The Prisoners Dilemma

As a basis for measuring the capabilities of TikZ, here is a replication of the game as it was presented in the last example of the previous section. To complete this graphic, I have loaded three
libraries in the preamble:
\ u s e t i k z l i b r a r y { calc , matrix , p o s i t i o n i n g }

13

Payoff Matrix

Firm A

Firm B
Defect Cooperate
0

Defect
0

-1
2

Cooperate
-1

1
1

\ begin { t i k z p i c t u r e }
\ m a t r i x [ m a t r i x o f math nodes , e v e r y odd row / . s t y l e ={ a l i g n = r i g h t } ,
e v e r y e v e n row / . s t y l e ={ a l i g n = l e f t } , e v e r y node / . s t y l e ={ t e x t w i d t h
= 1 . 5 cm } , row s e p = 0 . 2 cm , column s e p = 0 . 2 cm ] (m) {
0&1\\
0&2\\
2&1\\
1&1\\
};
\ draw (m. n o r t h e a s t ) r e c t a n g l e (m. s o u t h w e s t ) ;
\ draw (m. n o r t h ) (m. s o u t h ) ;
\ draw (m. e a s t ) (m. w e s t ) ;
\ coordinate ( a )
\ coordinate (b)
\ node [ a b o v e =5 p t
\ node [ a b o v e =5 p t

at
at
of
of

( $ (m. n o r t h w e s t ) ! 0 . 2 5 ! (m. n o r t h e a s t ) $ ) ;
( $ (m. n o r t h w e s t ) ! 0 . 7 5 ! (m. n o r t h e a s t ) $ ) ;
a , anchor=base ] { Defect };
b , anchor=base ] { Cooperate };

\ coordinate ( c )
\ coordinate (d)
\ node [ l e f t =20 p t
\ node [ l e f t =20 p t

at
at
of
of

( $ (m. n o r t h w e s t ) ! 0 . 2 5 ! (m. s o u t h w e s t ) $ ) ;
( $ (m. n o r t h w e s t ) ! 0 . 7 5 ! (m. s o u t h w e s t ) $ ) ;
c , a l i g n = c e n t e r , t e x t w i d t h =1cm ] { D e f e c t } ;
d , a l i g n = c e n t e r , t e x t w i d t h =1cm ] { C o o p e r a t e } ;

\ node [ a b o v e =18 p t o f m. n o r t h , a l i g n = c e n t e r ] ( f i r m b ) { Firm B } ;


\ node [ l e f t = 2 . 4 cm o f m. west , r o t a t e =90 , a l i g n = c e n t e r , a n c h o r = c e n t e r ]
{ Firm A} ;
\ node [ a b o v e =5 p t o f f i r m b ]
\ end { t i k z p i c t u r e }
3.1.2

{ Payoff Matrix };

Probability Tree

This TikZ graphic uses the inputenc package, as well as the trees library within TikZ.

14

P (B1 B2 ) =

3
7

6
9

3
9

P (B1 W2 ) =

3
7

3
9

P (W1 B2 ) =

4
7

5
9

P (W1 W2 ) =

4
7

4
9

W
Bag 2
3W, 6B

B
3

6
9

Bag 1
4W, 3B

W
4
7

Bag 2
4W, 5B

5
9

W
4
9

% Set the o v e r a l l layout of the t r e e


\ t i k z s t y l e { l e v e l 1 } = [ l e v e l d i s t a n c e = 3 . 5 cm , s i b l i n g d i s t a n c e = 3 . 5 cm
]
\ t i k z s t y l e { l e v e l 2 } = [ l e v e l d i s t a n c e = 3 . 5 cm , s i b l i n g d i s t a n c e =2cm ]
% D e f i n e s t y l e s f o r b a g s and l e a f s
\ t i k z s t y l e { bag } = [ t e x t w i d t h =4em , t e x t c e n t e r e d ]
\ t i k z s t y l e { end } = [ c i r c l e , minimum w i d t h =3 p t , f i l l , i n n e r s e p =0 p t ]
% The s l o p e d o p t i o n g i v e s r o t a t e d e d g e l a b e l s . P e r s o n a l l y
% I f i n d s l o p e d l a b e l s a b i t d i f f i c u l t t o r e a d . Remove t h e s l o p e d
options
% to get horizontal labels .
\ b e g i n { t i k z p i c t u r e } [ grow= r i g h t , s l o p e d ]
\ node [ bag ] {Bag 1 $4W , 3B$}
child {
node [ bag ] {Bag 2 $4W , 5B$}
child {
node [ end , l a b e l = r i g h t :
{$P ( W 1\ c a p W 2 ) =\ f r a c {4}{7}\ c d o t \ f r a c {4}{9} $
} ] {}
e d g e from p a r e n t
node [ a b o v e ] {$W$}
node [ below ] {$\ f r a c {4}{9} $}
}
child {
node [ end , l a b e l = r i g h t :
{$P ( W 1\ c a p B 2 ) =\ f r a c {4}{7}\ c d o t \ f r a c {5}{9} $
} ] {}
e d g e from p a r e n t
node [ a b o v e ] {$B$}
node [ below ] {$\ f r a c {5}{9} $}
15

}
e d g e from p a r e n t
node [ a b o v e ] {$W$}
node [ below ] {$\ f r a c {4}{7} $ }
}
child {
node [ bag ] {Bag 2 $3W , 6B$}
child {
node [ end , l a b e l = r i g h t :
{$P ( B 1 \ c a p W 2 ) =\ f r a c {3}{7}\ c d o t \ f r a c {3}{9} $
} ] {}
e d g e from p a r e n t
node [ a b o v e ] {$B$}
node [ below ] {$\ f r a c {3}{9} $}
}
child {
node [ end , l a b e l = r i g h t :
{$P ( B 1 \ c a p B 2 ) =\ f r a c {3}{7}\ c d o t \ f r a c {6}{9} $
} ] {}
e d g e from p a r e n t
node [ a b o v e ] {$W$}
node [ below ] {$\ f r a c {6}{9} $}
}
e d g e from p a r e n t
node [ a b o v e ] {$B$}
node [ below ] {$\ f r a c {3}{7} $ }
};
\ end { t i k z p i c t u r e }
3.1.3

Red-Black Tree

This image uses the verbatim package to comment out blocks of text with notes describing the
image. It also uses the arrows library in TikZ.
33

15

10

47

20

38

51

x
5

18

36

39 49

\ b e g i n { comment }
16

: Author : Madit
A r e d b l a c k t r e e i s a s p e c i a l t y p e o f b i n a r y t r e e , u s e d i n
computer s c i e n c e to o r g a n i z e p i e c e s of comparable data , such as
t e x t f r a g m e n t s o r numbers .
( Wikipedia )
\ end { comment }
\tikzset{
t r e e n o d e / . s t y l e = { a l i g n = c e n t e r , i n n e r s e p =0 p t , t e x t c e n t e r e d ,
f o n t =\ s f f a m i l y } ,
a r n n / . s t y l e = { t r e e n o d e , c i r c l e , w h i t e , f o n t =\ s f f a m i l y \
b f s e r i e s , draw= b l a c k ,
f i l l = b l a c k , t e x t w i d t h = 1 . 5 em},% a r b r e r o u g e n o i r , noeud n o i r
a r n r / . s t y l e = { t r e e n o d e , c i r c l e , r e d , draw= r e d ,
t e x t w i d t h = 1 . 5 em , v e r y t h i c k },% a r b r e r o u g e n o i r , noeud r o u g e
a r n x / . s t y l e = { t r e e n o d e , r e c t a n g l e , draw= b l a c k ,
minimum w i d t h = 0 . 5 em , minimum h e i g h t = 0 . 5 em}% a r b r e r o u g e n o i r ,
nil
}
\ b e g i n { t i k z p i c t u r e }[>,>= s t e a l t h , l e v e l / . s t y l e ={ s i b l i n g d i s t a n c e
= 5cm / # 1 ,
l e v e l d i s t a n c e = 1 . 5 cm } ]
\ node [ a r n n ] {33}
c h i l d { node [ a r n r ] {15}
c h i l d { node [ a r n n ] {10}
c h i l d { node [ a r n r ] {5} e d g e from p a r e n t node [ a b o v e
left ]
{ $x$ }} %f o r a named p o i n t e r
c h i l d { node [ a r n x ] {}}
}
c h i l d { node [ a r n n ] {20}
c h i l d { node [ a r n r ] {18}}
c h i l d { node [ a r n x ] {}}
}
}
c h i l d { node [ a r n r ] {47}
c h i l d { node [ a r n n ] {38}
c h i l d { node [ a r n r ] {36}}
c h i l d { node [ a r n r ] {39}}
}
c h i l d { node [ a r n n ] {51}
c h i l d { node [ a r n r ] {49}}
c h i l d { node [ a r n x ] {}}
}
}
17

;
\ end { t i k z p i c t u r e }
3.1.4

H-tree and B-tree

Here are two images side-by-side. The vast majority of the code in this subsection is dedicated to
defining the commands which ultimately produce the final diagrams in the final lines of code!

\tikzset{
h t r e e l e a v e s / . i n i t i a l =2 ,
s i b l i n g a n g l e / . i n i t i a l =20 ,
h t r e e l e v e l / . i n i t i a l ={}
}
\makeatletter
\ d e f \ h t r e e @ g r o w t h{%
\ p g f t r a n s f o r m r o t a t e {%
( \ p g f k e y s v a l u e o f { / t i k z / s i b l i n g a n g l e } ) ( .5 .5\
tikznumberofchildren
+\ t i k z n u m b e r o f c u r r e n t c h i l d )}%
\ p g f t r a n s f o r m x s h i f t {\ t h e \ t i k z l e v e l d i s t a n c e }%
\ p g f k e y s v a l u e o f { / t i k z / h t r e e l e v e l }%
}
\ t i k z s t y l e { h t r e e }=[
g r o w t h f u n c t i o n =\ h t r e e @ g r o w t h ,
s i b l i n g a n g l e =180 ,
h t r e e l e v e l ={
\ t i k z l e v e l d i s t a n c e =.707\ t i k z l e v e l d i s t a n c e
\ p g f s e t l i n e w i d t h {.707\ the \ p g f l i n e w i d t h }
}
]
18

\ t i k z s t y l e { b t r e e }=[
g r o w t h f u n c t i o n =\ h t r e e @ g r o w t h ,
s i b l i n g a n g l e =60 ,
h t r e e l e v e l ={
\ t i k z l e v e l d i s t a n c e =.55\ t i k z l e v e l d i s t a n c e
\ p g f s e t l i n e w i d t h {.707\ the \ p g f l i n e w i d t h }
}
]
\ l o n g \ d e f \ ge@addto@macro#1#2{%
\ begingroup
\ toks@ \ e x p a n d a f t e r \ e x p a n d a f t e r \ e x p a n d a f t e r {\ e x p a n d a f t e r #1#2}%
\ x d e f #1{\ t h e \ toks@}%
\ endgroup }
\newcommand {\ h t r e e } [ 2 ] [ ] { %
\ d e f \ h t r e e @ s t a r t {\ n o e x p a n d \ c o o r d i n a t e }
\ d e f \ h t r e e @ e n d {}
\ foreach \ l in {0 ,... ,#2} {
\ g@addto@macro \ h t r e e @ s t a r t { c h i l d f o r e a c h \ n o e x p a n d \x i n { 1 , 2 }
{\ i f f a l s e }\ f i }
\ g@addto@macro \ h t r e e @ e n d {\ i f f a l s e {\ f i }}
\ global \ let \ htree@start \ htree@start
\ g l o b a l \ l e t \ htree@end \ htree@end
}
\ e d e f \ htree@cmd {\ h t r e e @ s t a r t \ h t r e e @ e n d ; }
\ begin { scope }[ htree , # 1 ]
\ htree@cmd
\ end { s c o p e }
}
\ makeatother
\ begin { t i k z p i c t u r e }[
r o t a t e =90 ,
yscale =.5 ,
l e v e l d i s t a n c e =3cm ,
l i n e w i d t h =8 p t ,
]
\ h t r e e {4}
\ h t r e e [ b t r e e , y s h i f t =12cm , x s h i f t =3cm ] { 7 }
\ end { t i k z p i c t u r e }

19

3.1.5

Mindmap and Layers

Finally, this complicated diagram is useful for visualizing the relationship between divergent concepts. It also looks really cool. It uses the times package in addition to TikZ and the TikZ libraries,
mindmap and backgrounds.

Computer
Science

Categorical
Methods

Networks
Algorithms

Probability
Optimization
Images

Compression
& Transmission

Logic

Kabatyanski,
Tsfasman,
Rybakov,
Zykin

Nadirashvili
Rybko

Shehtman

OTARIE
(Sobolevski)

Kucherov,
Roytberg
Raffinot

Koshevoy

Vereschagin

Condensed
Matter

Partial Eifferential Equations


& Numerical
Methods

Loheac

Astronomy
& Cosmology

Combinatorics
and Discrete
Mathematics

Medicine

Physics

Choice and
Decision
Theory
Genomics

Economics
Biology

Quantitative
Calculations

\ b e g i n { t i k z p i c t u r e } [ mindmap ,
l e v e l 1 c o n c e p t / . a p p e n d s t y l e ={ l e v e l d i s t a n c e =130 , s i b l i n g a n g l e
=30} ,
e x t r a c o n c e p t / . a p p e n d s t y l e ={ c o l o r = b l u e ! 5 0 , t e x t = b l a c k } ,
t r a n s f o r m shape , s c a l e = . 7 ]
% A p p l i e d a r e a : c o m p u t e r s c i e n c e and i t s s u b f i e l d s
\ b e g i n { s c o p e } [ mindmap , c o n c e p t c o l o r = o r a n g e , t e x t = w h i t e ]
\ node [ c o n c e p t ] { Computer S c i e n c e } [ c l o c k w i s e from =5]
c h i l d { node [ c o n c e p t ] ( l o g ) { C a t e g o r i c a l Methods }}
c h i l d { node [ c o n c e p t ] ( a l g ) { A l g o r i t h m s }}
c h i l d { node [ c o n c e p t ] ( cod ) { C o m p r e s s i o n \& T r a n s m i s s i o n }}
20

c h i l d { node [ c o n c e p t ] ( img ) { I m a g e s }}
c h i l d { node [ c o n c e p t ] ( o p t ) { O p t i m i z a t i o n }}
c h i l d { node [ c o n c e p t ] ( r e s ) { N e t w o r k s } } ;
\ end { s c o p e }
% A p p l i e d a r e a : t h e o r e t i c a l p h y s i c s and i t s s u b f i e l d s
\ b e g i n { s c o p e } [ mindmap , c o n c e p t c o l o r = r e d , t e x t = w h i t e ]
\ node [ c o n c e p t ] a t ( 5 , 15) { P h y s i c s }
c h i l d [ grow = 10 , l e v e l d i s t a n c e =1 60 ]
{ node [ c o n c e p t ] ( q i n ) { Q u a n t i t a t i v e C a l c u l a t i o n s }}
c h i l d [ grow = 20 ]
{ node [ c o n c e p t ] ( csm ) { Astronomy \& Cosmology }}
c h i l d [ grow =1 10 ]
{ node [ c o n c e p t ] ( mat ) { Condensed M a t t e r } } ;
\ end { s c o p e }
% A p p l i e d a r e a : b i o l o g y and i t s s u b f i e l d s
\ b e g i n { s c o p e } [ mindmap , c o n c e p t c o l o r = g r e e n ! 5 0 ! b l a c k , t e x t = w h i t e ]
\ node [ c o n c e p t ] a t ( 6 . 5 , 1 5 ) { B i o l o g y }
c h i l d [ grow =165 , l e v e l d i s t a n c e =1 20 ]
{ node [ c o n c e p t ] ( med ) { M e d i c i n e }}
c h i l d [ grow = 60 ]
{ node [ c o n c e p t ] ( gen ) { Genomics } } ;
\ end { s c o p e }
% A p p l i e d a r e a : e c o n o m i c s ( one s u b f i e l d )
\ b e g i n { s c o p e } [ mindmap , c o n c e p t c o l o r = v i o l e t , t e x t = w h i t e ]
\ node [ c o n c e p t ] a t ( 1 1 , 1 4) { Economics }
c h i l d [ grow =70 , l e v e l d i s t a n c e =1 20 ]
{ node [ c o n c e p t ] ( d e c ) { C h o i c e and D e c i s i o n T h e o r y } } ;
\ end { s c o p e }
% R e s e a r c h e r s l i s t e d by t h e i r main s p e c i a l i z a t i o n i n
mathematics
\ b e g i n { s c o p e } [ mindmap , c o n c e p t c o l o r = b l u e ]
% C o m b i n a t o r i c s and d i s c r e t e m a t h e m a t i c s
\ node [ c o n c e p t , t e x t = w h i t e ] a t ( 5 . 2 , 1 0 . 8 )
{ C o m b i n a t o r i c s and D i s c r e t e M a t h e m a t i c s }
[ c l o c k w i s e from = 15 0]
c h i l d [ c o n c e p t c o l o r = b l u e ! 5 0 ] { node [ c o n c e p t ] ( v e r ) {
V e r e s c h a g i n }}
c h i l d [ c o n c e p t c o l o r = b l u e ! 5 0 , l e v e l d i s t a n c e = 12 5]

21

{ node [ c o n c e p t ] ( kab ) { K a b a t y a n s k i , Tsfasman , Rybakov ,


Z y k i n }}
child [ concept color=blue !50]
{ node [ c o n c e p t ] ( kch ) { Kucherov , R o y t b e r g }}
c h i l d [ c o n c e p t c o l o r = b l u e ! 5 0 ] { node [ c o n c e p t ] ( r a f ) {
R a f f i n o t }}
c h i l d [ c o n c e p t c o l o r = b l u e ! 5 0 , l e v e l d i s t a n c e = 13 5]
{ node [ c o n c e p t ] ( k s h ) { Koshevoy } } ;
% Partial d i f f e r e n t i a l equations
\ node [ c o n c e p t , t e x t = w h i t e ] a t ( 3 , 11)
{ P a r t i a l E i f f e r e n t i a l Equations
\& N u m e r i c a l Methods }
c h i l d [ c o n c e p t c o l o r = b l u e ! 5 0 , grow =0 , l e v e l d i s t a n c e = 14 0]
{ node [ c o n c e p t ] ( l h c ) {Loh {\ e } a c }}
c h i l d [ c o n c e p t c o l o r = b l u e ! 5 0 , grow =60 , l e v e l d i s t a n c e = 11 5]
{ node [ c o n c e p t ] ( o t r ) {OTARIE ( S o b o l e v s k i ) }}
c h i l d [ c o n c e p t c o l o r = b l u e ! 5 0 , grow = 95 ] { node [ c o n c e p t ] ( n d r
)
{ N a d i r a s h v i l i }};
% Probability
\ node [ c o n c e p t , t e x t = w h i t e ] a t ( 7 . 2 , 3 . 2 ) { P r o b a b i l i t y }
c h i l d [ c o n c e p t c o l o r = b l u e ! 5 0 , grow = 70 , l e v e l d i s t a n c e =1 20 ]
{ node [ c o n c e p t ] ( r b k ) { Rybko } } ;
% Logic
\ node [ c o n c e p t , t e x t = w h i t e ] a t ( 1 1 . 5 , 5 ) { L o g i c }
c h i l d [ c o n c e p t c o l o r = b l u e ! 5 0 , grow =165 , l e v e l d i s t a n c e = 12 0]
{ node [ c o n c e p t ] ( s h t ) { Shehtman } } ;
\ end { s c o p e }
% Connections of r e s e a r c h e r s to applied s u b f i e l d s
\ b e g i n { p g f o n l a y e r }{ b a c k g r o u n d }
\ draw [ c i r c l e c o n n e c t i o n b a r ]
( kab ) e d g e ( cod )
( kch ) e d g e ( a l g ) e d g e ( gen )
( l h c ) e d g e ( med )
( ksh ) edge ( dec )
( n d r ) e d g e ( mat )
( o t r ) e d g e ( o p t ) e d g e ( csm ) e d g e ( img )
( r a f ) e d g e ( a l g ) e d g e ( gen )
( r b k ) e d g e ( r e s ) e d g e ( mat )
( s h t ) edge ( log ) edge ( dec )
22

( v e r ) e d g e ( q i n ) e d g e ( cod ) ;
\ end { p g f o n l a y e r }
\ end { t i k z p i c t u r e }

Conclusion

Now that you have made it this far, it is time to just jump in and start playing around with the
software. Practice makes perfect, right? If you have more requests for information to include, let
me know. I will try to update this document over time to add functionality as well as make the
learning experience easier. Hopefully there is enough here to at least give you an idea of what to
look for as you attempt to write documents in LATEX.
January 23, 2016

References

23

24
5
5

Metallica

Police
Beethoven

Audibility

Audio

1
8
1
4

1 1 1
8 8 2
1 1 1
4 8 8

soft
hard
soft
hard

1 1 1
8 8 8

1 1 1
8 8 6

Decision
Sum of Extracted Bits
soft
1 1 1 1 1 1 1
hard
2 4 4 4 2 4 4

Table 2: Performance After Post Filtering

You might also like