-
Notifications
You must be signed in to change notification settings - Fork 5
/
latex-intro.html
295 lines (208 loc) · 13.7 KB
/
latex-intro.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Introduction to LaTeX</title>
<script type="text/javascript">
window.onload = function() {
var imgs = document.getElementsByTagName('img'), i, img;
for (i = 0; i < imgs.length; i++) {
img = imgs[i];
// center an image if it is the only element of its parent
if (img.parentElement.childElementCount === 1)
img.parentElement.style.textAlign = 'center';
}
};
</script>
<style type="text/css">
body, td {
font-family: sans-serif;
background-color: white;
font-size: 13px;
}
body {
max-width: 800px;
margin: auto;
padding: 1em;
line-height: 20px;
}
tt, code, pre {
font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace;
}
h1 {
font-size:2.2em;
}
h2 {
font-size:1.8em;
}
h3 {
font-size:1.4em;
}
h4 {
font-size:1.0em;
}
h5 {
font-size:0.9em;
}
h6 {
font-size:0.8em;
}
a:visited {
color: rgb(50%, 0%, 50%);
}
pre, img {
max-width: 100%;
}
pre {
overflow-x: auto;
}
pre code {
display: block; padding: 0.5em;
}
code {
font-size: 92%;
border: 1px solid #ccc;
}
code[class] {
background-color: #F8F8F8;
}
table, td, th {
border: none;
}
blockquote {
color:#666666;
margin:0;
padding-left: 1em;
border-left: 0.5em #EEE solid;
}
hr {
height: 0px;
border-bottom: none;
border-top-width: thin;
border-top-style: dotted;
border-top-color: #999999;
}
@media print {
* {
background: transparent !important;
color: black !important;
filter:none !important;
-ms-filter: none !important;
}
body {
font-size:12pt;
max-width:100%;
}
a, a:visited {
text-decoration: underline;
}
hr {
visibility: hidden;
page-break-before: always;
}
pre, blockquote {
padding-right: 1em;
page-break-inside: avoid;
}
tr, img {
page-break-inside: avoid;
}
img {
max-width: 100% !important;
}
@page :left {
margin: 15mm 20mm 15mm 10mm;
}
@page :right {
margin: 15mm 10mm 15mm 20mm;
}
p, h2, h3 {
orphans: 3; widows: 3;
}
h2, h3 {
page-break-after: avoid;
}
}
</style>
</head>
<body>
<h1>Introduction to LaTeX</h1>
<h2>A brief introduction using an example document</h2>
<p>Chris Paciorek, Department of Statistics, UC Berkeley</p>
<h1>0) This Tutorial</h1>
<p>This tutorial covers the basics of LaTeX, which is widely used in scientific and academic contexts to prepare documents. LaTeX is particularly good at handling mathematical notation and LaTeX syntax for specifying math can be embedded in a wide variety of contexts (Jupyter/IPython Notebook, R Markdown, Microsoft Word equation editor, etc.). Here we'll briefly discuss the use of LaTeX for standalone documents, as a way to get started with using LaTeX syntax more generally.</p>
<p>A <a href="http://youtu.be/8khoelwmMwo">screencast</a> of this material accompanies this document.</p>
<p>We'll use a virtual machine developed here at Berkeley, <a href="http://bce.berkeley.edu">the Berkeley Common Environment (BCE)</a>. BCE is a virtual Linux machine - basically it is a Linux computer that you can run within your own computer, regardless of whether you are using Windows, Mac, or Linux. This provides a common environment so that things behave the same for all of us. However, you could also install LaTeX directly on your computer, e.g., MacTeX for Mac or MiKTeX for Windows.</p>
<p>This tutorial assumes you are able to use the UNIX command line; we provide a tutorial on the <a href="http://statistics.berkeley.edu/computing/training/tutorials">Basics of UNIX</a> in the context of BCE.</p>
<p>Materials for this tutorial, including the R markdown file that was used to create this document are available on github at (<a href="https://github.com/berkeley-scf/tutorial-latex-intro">https://github.com/berkeley-scf/tutorial-latex-intro</a>). You can download the files by doing a <code>git clone</code> from a terminal window on a UNIX-like machine. The following will work from the command line on BCE:</p>
<pre><code class="bash">git clone https://github.com/berkeley-scf/tutorial-latex-intro
</code></pre>
<p>Or go to the <a href="https://github.com/berkeley-scf/tutorial-latex-intro">github site for this material</a> and you can download a zipped copy of the materials (see the “Download ZIP” button in the lower right of the page).</p>
<p>To create this HTML document, simply compile the corresponding R Markdown file in R as follows (the following will work from within BCE after cloning the repository as above).</p>
<pre><code class="bash">Rscript -e "library(knitr); knit2html('latex-intro.Rmd')"
</code></pre>
<p>This tutorial by Christopher Paciorek is licensed under a Creative Commons Attribution 3.0 Unported License.</p>
<!--
#pandoc --number-sections spark.md -o spark.html
#Rscript -e "library(knitr); knit('spark.Rmd')"
#pandoc --mathjax --number-sections spark.md -o spark.html
-->
<h1>1) The LaTeX document format</h1>
<p>LaTeX allows you to do essentially anything in a document, including math syntax, including tables and figures, adding a bibliography, including a table of contents, etc. </p>
<p>The basic premise of LaTeX, in contrast to Word, is that it is a markup language for defining your document. It's NOT a what-you-see-is-what-you-get (WYSIWYG) program. Rather than specifying the exact formatting of a given word, line, or paragraph, you specify what kind of object everything is and LaTeX deals with the formatting. You do this in a plain text document using LaTeX syntax. That said, you can monkey with the formatting of individual pieces of your document, as well as monkeying with the formatting of the entire document.</p>
<p>This is not meant as an in-depth introduction to LaTeX, but rather to get you going quickly based on an example, so let's dive into that.</p>
<h2>1.1) Basic LaTeX syntax</h2>
<p>The file <em>demo_paper.tex</em> provides an example LaTeX document. The material is taken from a scientific paper but includes only bits and pieces and is not meant to actually be read. Rather it illustrates a lot of the syntax of LaTeX. </p>
<p>Look through the document (you might start by looking at <em>demo_paper.pdf</em> and looking for the syntax in the LaTeX file that creates different aspects of the PDF) and note the following:</p>
<ul>
<li>The <em>preamble</em> (the part before <code>\begin{document}</code>), which sets up the document.</li>
<li>The use of <code>%</code> to make comments that won't be included in the document.</li>
<li>The commands for defining sections and subsections. </li>
<li>The use of user-defined macros (see <code>\newcommand</code>) to define commonly-used syntax that can be used anywhere in the document.</li>
<li>The specification of math syntax in so-called <em>math mode</em>, which is done inside special symbols (<code>\begin{eqnarray}</code>, <code>$</code>, and <code>\[</code>, among others). </li>
<li>The insertion of figures and tables. LaTeX will figure out where to put them.</li>
<li>The use of <code>\label{sec:methods}</code> and <code>\ref{sec:methods}</code> to define and refer to sections, tables, and figures. LaTeX will figure out the numbering for you.</li>
<li>The inclusion of a bibliography using BibTeX with <code>\bibliography{name_of_file_with_refs.bib}</code>, whose style can be specified with <code>\bibliographystyle{name_of_style}</code>. </li>
<li>Citations of sources using <code>\cite</code> and related syntax.</li>
</ul>
<p>The reference information used in <em>demo_paper.tex</em> comes from the <em>refs.bib</em> file included with this tutorial, which contains reference information in the structured format expected by BibTeX. Note that entries in the Bibtex format needed for a .bib file can often be obtained directly from bibliographic sources such as Google Scholar.</p>
<p>You should be able to take <em>demo_paper.tex</em> as a starting point for your own document. In fact, we provide a version of it, <em>demo_paper_shell.tex</em> with the content removed, ready for you to use.</p>
<h2>1.2) Additional functionality for LaTeX</h2>
<p>There is a huge variety of add-on packages for LaTeX that provide additional functionality. These are generally provided as <em>style</em> files whose names end in .sty. To use a package, you use the <code>\usepackage</code> command. The corresponding .sty file should generally be in the directory in which you are working or in a standard location on your machine. For example there is a line numbering package provided in <em>lineno.sty</em> that will allow you to add line numbers to your document by putting the following code in your preamble:</p>
<pre><code>% load the package and specify the left option to put linenumbers on the left side of the page
\usepackage[left]{lineno}
% enable line numbering in your document
\linenumbers
</code></pre>
<p>To try this in <em>demo_paper.tex</em> simply remove the <code>%</code> from in front of those two lines so they are not commented out.</p>
<p>In addition, there is a wide variety of classes that format your document in a pre-specified way, such as for particular academic journals. To use a class, you use the <code>\documentclass{}</code> syntax in the first line of your document. The classes are provided by class files whose names end in .cls. The <em>article</em> class (<code>\documentclass{article}</code>) is a standard class that comes with LaTeX; it's the class used in <em>demo_paper.tex</em>. </p>
<p>Analogously, there is a wide variety of biblography styles available via .bst files. Many journals have .bst files that will format things as they want them to be. In <em>demo_paper.tex</em> I use the <em>plainnat</em> style provided in the <em>natbib</em> package.</p>
<p>As noted for .sty files, the .sty, .cls, and .bst files should generally either be in the directory of your LaTeX file or in a standard location on your machine where LaTeX knows to look for them.</p>
<h1>2) Compiling LaTeX to PDF and other formats</h1>
<h2>2.1) Compiling via the UNIX command line</h2>
<p>The standard way to create a document from a LaTeX file is to create a PDF using <code>pdflatex</code>. There are also ways to create HTML from LaTeX and to convert from LaTeX to other formats (e.g., see <code>pandoc</code>). Here we'll just see the creation of a PDF, including use of a bibliography. In general you'll need to run <code>pdflatex</code> multiple times as there are multiple layers of processing.</p>
<pre><code class="bash">pdflatex demo_paper
bibtex demo_paper # to create the bibliography file (.bbl)
pdflatex demo_paper # to get the cross-referencing and numbering right
pdflatex demo_paper # to get the bibliography and refs right
</code></pre>
<p><code>bibtex</code> creates the bibliography file (.bbl) specific to this document from one or more .bib files (which are basically a database of references and can include references not used in the document) and the relevant bibliography style file.</p>
<h2>2.2) Other interfaces to LaTeX – TeXShop and MiKTeX</h2>
<p>MacTeX (for Mac; includes TeXShop) and MiKTeX (for Windows) provide installations of LaTeX that include the ability to easily create a PDF and see the PDF alongside the source LaTeX file. You should be able to see how to create a PDF using the pull-down menus.</p>
<h1>3) LyX</h1>
<p>LyX is a popular graphical front-end to LaTeX that runs on Mac, Windows, and Linux. It provides an editor window that has the look and feel of word processing software such as Word but with all the power of LaTeX. You can import/export to/from LaTeX and compile to PDF. It supports BibTeX and allows you to insert R code chunks in Sweave format (see our <a href="https://github.com/berkeley-scf/tutorial-dynamic-docs">tutorial on dynamic documents with R</a>). </p>
<p>Note that to use LyX on BCE, you need to run the commands in the <em>bce.sh</em> file to install LyX.</p>
<p>The <em>demo_paper.lyx</em> file is the LyX version of the demo file. To open a LyX file from the command line, type <code>lyx demo_paper</code> without the .lyx extension.</p>
<p>To enter math mode, type <code>Ctrl-m</code> (or find the icon on the toolbar) and to enter math mode for a displayed equation, use <code>Ctrl-Shift-m</code>. Then just type in math mode as you would ordinarily do in a LaTeX text file, but the math will be formatted and displayed on the fly for you to examine. I find this allows me to catch errors easily. The <em>insert</em> menu provides a point-and-click way to add various LaTeX objects to your document. You can also directly type in LaTeX within LyX using the TeX icon on the toolbar.</p>
<p>To create a PDF, do <code>File -> Export -> PDF (pdflatex)</code>.</p>
<p>One nice thing about LyX is that you can insert elements by point-and-click and if you then export your document as LaTeX you can see the underlying LaTeX syntax that you can type directly in the future. </p>
<h1>4) Other resources</h1>
<p>In addition to a wide variety of resources available via Googling…</p>
<ul>
<li>You might look at (<a href="http://www.latex-tutorial.com">www.latex-tutorial.com</a>) for tutorial info. </li>
<li>There are many good books on LaTeX, including <em>LaTeX, A Document Preparation System</em> by Leslie Lamport and others listed <a href="http://latex-project.org/guides/books.html">here</a>.</li>
<li>Phil Spector formerly of the UCB Statistics Department, has a nice (albeit a bit old) <a href="https://www.stat.berkeley.edu/%7Espector/latex2e.pdf">overview</a>.</li>
<li>Our <a href="http://statistics.berkeley.edu/computing/training/tutorials">tutorials on UNIX, dynamic documents and related topics</a> may also be useful.</li>
</ul>
</body>
</html>