UCLA Department of Statistics Outline
Statistical Consulting Center
Intro to LaTeX interface
Intro to LaTeX Working with text
Tabbing and tables
David Diez Figures
Math and equations
April 23, 2009
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Installation
Getting started
Installing LaTeX Opening TeXShop
Mac Installation. Download MacTeX, which can be found on Finder (icon: ) → Applications > TeX > TeXShop
http://www.tug.org/mactex/. LaTeX can then be accessed via the
program TeXShop, which is included in this installation.
Windows Installation. Download proTeXt, which can be found on
http://www.tug.org/protext/1. LaTeX can be accessed via
TeXnicCenter, which is included in this installation.
Both the MacTeX and proTeXt downloads are very large: 1.2GB and
540MB, respectively.
The interfaces for Macs and PCs are different, however, the LaTeX
“code” works for both.
1Another option: http://www.winshell.de/. (Winshell and MikTeX)
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Getting started Getting started
Creating a basic document Creating a basic document
Open a file. Use File > New or command-N (apple-N) to open a new Update the \title, \author, and then type a sentence above the last
document if one isn’t already open. line, i.e. type right above \end{document}. Create a folder on the
desktop and then save this file into that folder.
Templates. Choose the LatexTemplate.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Getting started Getting started
Compiling Files from the compile
Hit command-T or go to Typeset > Typeset. After compiling, double-click LaTeX takes the .tex document and it produces a bunch of other files.
on the PDF page to magnify. Try triple-clicking... and quadruple-clicking.
While each of these files has a purpose, only one file – in addition to the
original LaTeX file – is of interest: the PDF. As more methods of LaTeX
are used, this list of LaTeX output files might grow... but again, they
(except the .tex and .pdf files) can just be ignored for the vast majority
of LaTeX use.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Getting started Getting started
Console Errors are inevitable
When the code was compiled, two windows popped up. The console tells Errors are inevitable with LaTeX, and they are common. To help identify
you what LaTeX is doing when it reads (compiles) the document. If errors, it is recommended that you compile frequently. At first, this
you’ve made an error (or just did something LaTeX doesn’t like), the probably means compiling every 1-2 minutes to verify your output
console will tell you. If the error is critical, LaTeX will stop compiling: matches what you anticipated. (You can continue working while a
document compiles.)
While it is always good to fix the problem immediately (hit ), Common errors that will make more sense as we go along...
you can continue compiling by hitting return on the keyboard. Misspelling a command
Not escaping special characters
Not balancing {braces}
Not balancing $’s
Not balancing out beginning/ending environments (e.g.
\begin{document} and \end{document})
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Getting started Getting started
Commenting Dual screens
In TeXShop, there is also the option to have dual screens by clicking on
the icon to the right of Templates:
Return to the basic file you just created. What’s with all the red (or gray
in Windows)? These are comments, which is writing that will be ignored
by LaTeX. Comments are made by using the percent symbol: %.
Any text following a % on that line only will be ignored by LaTeX.
This can be very useful when writing or editing a long document.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Getting started Header section
Example template Document type
Download the following zipped folder2 to your desktop Near the top of every LaTeX document is a line with the command
\documentclass, which basically says what kind of document you are
http://www.stat.ucla.edu/∼david/latexTemp.zip making.
Unzip the file (on a Mac, double-click). latexTemp’s contents: Right now we’ll just use the class article. Other types of documents
latexTemp.tex, other LaTeX files from compiling, a PDF of this can also be written, such as letters (letter), presentations (beamer), or
presentation, a file named latexTempPractice.pdf, and one image even books (book). Plenty of examples and help can be found online for
file, lower40.pdf. these other classes.
folders containing several figures and, for one figure, the R code
used to produce the figure. Alter [11pt] to change the default font size for the document, if desired.
latexTemp.tex follows the rest of this presentation and is filled with
examples and extra comments. Open this file now.
2If this URL no longer works, find the zip file on http://scc.stat.ucla.edu/ in the
mini-course section.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Outline
Header section
Packages Sections and subsections
Packages are loaded at the start of the document. These packages Documents are often broken up into sections and subsections, and this
contain new commands and environments or can be used to alter your hierarchy will automatically be numbered by LaTeX.
document in particular ways. For example, the package amsmath is gives
more equation options and fullpage formats the PDF output in
different way.
Many common packages are included in a LaTeX installations, however,
many other packages are not (amsmath is usually included in a LaTeX
installation... fullpage is not). Additional packages can be downloaded
and installed, as needed (we won’t cover this).
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Text Text
Paragraphs Indentation and space
To end a paragraph and create a new one, do a double “enter” and this
creates a line break.
To put an extra line space between paragraphs, use the \\ command Indentation. To create an indent, use \indent, and to prevent an
followed by a double line break in the .tex document. indent use \noindent. Indenting can be suppressed via
\setlength{\parindent}{0in}.
Space. To create horizontal space, use \hspace{1cm}. Similarly,
follow the \\ command followed by the \vspace{0.5cm}
command to create some extra vertical space. \hspace and \vspace are
generally more useful in tables than in text.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Text Text
Playing with the font Lists
Lists can be created via
Emphasize (italicize). Use the command \emph, e.g.
\emph{emphasize}, to emphasize (italicize) a single word.
More manipulation. Text can also be bolded via \textbf or colored
via \color. Font can be made to look typewriterish via \texttt.
Font size. Text can also be made ,tiny scriptsize, footnotesize, small, large, which results in a bulleted list such as the following:
Spacing
LARGELarge, , etc. via \tiny, \scriptsize, etc. Text
Macros
A couple additional examples are in latexTemp.tex.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Text Tabbing
Macros Tabbing
Many more text alterations can be done using the Macros. Like in other text editors, LaTeX offers tabbing. While this environment
tends to rarely be used (at least by me), it can be very useful under
particular conditions.
Macros offer many many options to alter text. See latexTemp.tex for a brief introduction to tabbing and a few examples.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Basic tables Basic tables
Basic tables Centering and adding lines
Tables are created using the tabular environment. The {lcr} gives the Building the table up:
alignment. The ampersands (&) are used to define when to go to the
next column.
The \\ command tells LaTeX to start a new row. The result:
The result: Estimate Std. Error t value Pr(>|t|)
Left Center Right (Intercept) -0.2852 0.8434 -0.34 0.7452
12 3 x 0.4192 0.1499 2.80 0.0266
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Captions & referencing Captions & referencing
Floating table with a caption Referencing
To add a caption or label, the table must be floated (i.e. add on the Tables can be dynamically referenced (many examples throughout
table environment). Then \caption can be used: latexTemp.tex).
The output: Usually a table has a number if it has a caption, e.g. Table 4. These
numbers may change if more tables are added to the document, e.g.
Table 4 may change to be Table 5 at a later time – the user doesn’t
control this. Thankfully a table number can be dynamic referenced:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.2852 0.8434 -0.34 0.7452
x 0.4192 0.1499 2.80 0.0266
Table: A regression summary. \label{regressTable} labels the table and then this table can be
referenced using the LaTeX code Table∼\ref{regressTable}.
The table is numbered when a caption is added in the article
document class.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
xtable Figures
Using the R package xtable Basic figures
Inside R:
> library(xtable) # may need install.packages(’xtable’) A couple of the most basic examples of how figures can be added using
the \includegraphics command.
> x <- 1:9
> z <- rnorm(9)
> y <- x/7 + z∗2 + rnorm(9)
> xtable(summary(lm(y ∼ x+z)))
[... output that can be copied/pasted into LaTeX ...]
R output directly copied/pasted into LaTeX: The first file is stored in the same folder as the LaTeX file while the
second is a couple folders away. The files and folder names used should
(Intercept) Estimate Std. Error t value Pr(>|t|) have no spaces.
x -0.1563 0.6243 -0.25 0.8107
z 0.1094 0.1145 0.96 0.3760 The [height=1.0in] is an optional argument to control the figure size.
2.6170 0.4308 6.08 0.0009
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Figures Figures
Figure centering Adding a caption and referencing
Just like tables, a figure can be centered: Just like in the floating table environment, a floating figure can have a
caption and reference.
How does the second method look similar to the floating tables? (This is Just like with tables, the \label command must come after the
a floating figure.) \caption command for the reference to work properly.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Keeping organized Math in text
Keeping organized Inserting math into text
Some tips to keeping organized when using LaTeX: LaTeX makes it easy to add Greek letters like α, ζ, µ, etc. into text. In
Do not include several LaTeX documents in a single folder. the same way, equations can be added easily as well: y = x3, zj ,
When choosing a \label name for a figure, have it match the figure
file name. x1 + · · · + xn.
Organize figure files into folders.
If you use code to produce a figure, save it to the same folder as the It does take some practice to get used to writing math in LaTeX but,
figure and with the same name (but a different extension, of course). with practice, it becomes second nature. It is also sometimes easy to
Remember, no spaces in file or folder names. generalize. For instance, look at how to create α above: $\alpha$. How
do you think β would be created?
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Math in text Math in text
Equation array Equation referencing
While math inside text is nice, sometimes the equations are long and Just like tables and figures, equations can also be referenced. Use
eqnarray (no asterisk) to make the equation be a numbered equation:
should be on their on lines. In such a case, use the eqnarray or
eqnarray∗ environment: ∞ 1
0.5k = 1 − 0.5 =2 (1)
k =0
The result in LaTeX for eqnarray∗: \label{powerSeries} can be put inside the equation array and then be
referenced via \ref{powerSeries}.
∞ 1
1 − 0.5
0.5k = =2
k =0
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Math in text Math in text
Matrices LaTeX and Matrix Panels
Matrices also can be made in LaTeX: Since it is a little difficult to learn all the math syntax, a good place to
4 1 19 look for the proper syntax (using a click interface) is to the LaTeX and
38 8 Matrix Panels:
The code:
The syntax for an array is the same as for tabular (a table).
The Matrix Panel is especially useful since matrices can require a lot of
writing.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Examples Resources
Diverse possibilities Resources
LaTeX has a lot of power to do diverse tasks: Online help:
Chemistry figures http://info.stat.ucla.edu/grad/ — links to many useful sites
Circuits http://forums.stat.ucla.edu/?5 — online forum help
Feynman diagrams
Musical scores A good resource in book format:
Crossword puzzles Guide to LaTeX, by Helmut Kopka and Patrick W. Daly
Sudoku puzzles
A document that can be used to find LOTS of symbols (4MB):
These different applications require packages that are generally not
included in a basic download of LaTeX but that can typically be www.ctan.org/tex-archive/info/symbols/comprehensive/symbols-a4.pdf
downloaded at no cost.
The ability to make elegant documents and figures is one of LaTeX’s
major draws. It offers users nearly unlimited control of their documents
(even though it might feel a bit out of control at first).
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Survey Practice
Survey Try this #1
Please take the time to complete a survey about this mini-course: Produce the following result:
http://scc.stat.ucla.edu/survey Some examples may be utilized in latexTemp.tex.
Your feedback is greatly appreciated.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX
Prep Getting started General Tables / tabbing Figures Math More Practice Prep Getting started General Tables / tabbing Figures Math More Practice
Practice Practice
Try this #2 Try this #3
Using the image in latexTemp/normalPlots/lower82/, produce the Produce the following result using the eqnarray∗ environment:
following plot. Make it 0.8 inches tall, center it, add a caption, and add a
reference. Be sure to write a sentence that references the Figure and
compile your LaTeX document twice so the reference works.
18%
(0.18)
82%
(0.82)
Some examples may be utilized in latexTemp.tex.
60.1 63.4 66.7 70 73.3 76.6 79.9
Feel free to utilize examples in latexTemp.tex or to use the LaTeX
Float Figure template.
David Diez UCLA Department of Statistics Statistical Consulting Center David Diez UCLA Department of Statistics Statistical Consulting Center
Intro to LaTeX Intro to LaTeX