intro to latex - ucla statistical consulting...

11
UCLA Department of Statistics Statistical Consulting Center Intro to LaTeX David Diez April 23, 2009 David Diez Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center Outline Intro to LaTeX interface Working with text Tabbing and tables Figures Math and equations David Diez Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center Prep Getting started General Tables / tabbing Figures Math More Practice Installation Installing LaTeX Mac Installation. Download MacTeX, which can be found on 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. 1 Another option: http://www.winshell.de/. (Winshell and MikTeX) David Diez Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center Prep Getting started General Tables / tabbing Figures Math More Practice Getting started Opening TeXShop Finder (icon: ) Applications > TeX > TeXShop David Diez Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Upload: vantruc

Post on 06-Feb-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to LaTeX - UCLA Statistical Consulting Centerscc.stat.ucla.edu/page_attachments/0000/0077/intro_latex_1_09S.pdf · Intro to LaTeX David Diez ... Math and equations David Diez

UCLA Department of StatisticsStatistical Consulting Center

Intro to LaTeX

David Diez

April 23, 2009

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Outline

Intro to LaTeX interface

Working with text

Tabbing and tables

Figures

Math and equations

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Installation

Installing LaTeX

Mac Installation. Download MacTeX, which can be found onhttp://www.tug.org/mactex/. LaTeX can then be accessed via theprogram TeXShop, which is included in this installation.

Windows Installation. Download proTeXt, which can be found onhttp://www.tug.org/protext/1. LaTeX can be accessed viaTeXnicCenter, which is included in this installation.

Both the MacTeX and proTeXt downloads are very large: 1.2GB and540MB, 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

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Getting started

Opening TeXShop

Finder (icon: ) → Applications > TeX > TeXShop

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Page 2: Intro to LaTeX - UCLA Statistical Consulting Centerscc.stat.ucla.edu/page_attachments/0000/0077/intro_latex_1_09S.pdf · Intro to LaTeX David Diez ... Math and equations David Diez

Prep Getting started General Tables / tabbing Figures Math More Practice

Getting started

Creating a basic document

Open a file. Use File > New or command-N (apple-N) to open a newdocument if one isn’t already open.

Templates. Choose the LatexTemplate.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Getting started

Creating a basic documentUpdate the \title, \author, and then type a sentence above the lastline, i.e. type right above \end{document}. Create a folder on thedesktop and then save this file into that folder.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Getting started

Compiling

Hit command-T or go to Typeset > Typeset. After compiling, double-clickon the PDF page to magnify. Try triple-clicking... and quadruple-clicking.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Getting started

Files from the compile

LaTeX takes the .tex document and it produces a bunch of other files.

While each of these files has a purpose, only one file – in addition to theoriginal LaTeX file – is of interest: the PDF. As more methods of LaTeXare 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 majorityof LaTeX use.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Page 3: Intro to LaTeX - UCLA Statistical Consulting Centerscc.stat.ucla.edu/page_attachments/0000/0077/intro_latex_1_09S.pdf · Intro to LaTeX David Diez ... Math and equations David Diez

Prep Getting started General Tables / tabbing Figures Math More Practice

Getting started

Console

When the code was compiled, two windows popped up. The console tellsyou what LaTeX is doing when it reads (compiles) the document. Ifyou’ve made an error (or just did something LaTeX doesn’t like), theconsole will tell you. If the error is critical, LaTeX will stop compiling:

While it is always good to fix the problem immediately (hit ),you can continue compiling by hitting return on the keyboard.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Getting started

Errors are inevitable

Errors are inevitable with LaTeX, and they are common. To help identifyerrors, it is recommended that you compile frequently. At first, thisprobably means compiling every 1-2 minutes to verify your outputmatches what you anticipated. (You can continue working while adocument compiles.)

Common errors that will make more sense as we go along...

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

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Getting started

Commenting

Return to the basic file you just created. What’s with all the red (or grayin Windows)? These are comments, which is writing that will be ignoredby LaTeX. Comments are made by using the percent symbol: %.

Any text following a % on that line only will be ignored by LaTeX.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Getting started

Dual screens

In TeXShop, there is also the option to have dual screens by clicking onthe icon to the right of Templates:

This can be very useful when writing or editing a long document.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Page 4: Intro to LaTeX - UCLA Statistical Consulting Centerscc.stat.ucla.edu/page_attachments/0000/0077/intro_latex_1_09S.pdf · Intro to LaTeX David Diez ... Math and equations David Diez

Prep Getting started General Tables / tabbing Figures Math More Practice

Getting started

Example template

Download the following zipped folder2 to your desktop

http://www.stat.ucla.edu/∼david/latexTemp.zip

Unzip the file (on a Mac, double-click). latexTemp’s contents:

latexTemp.tex, other LaTeX files from compiling, a PDF of thispresentation, a file named latexTempPractice.pdf, and one imagefile, lower40.pdf.

folders containing several figures and, for one figure, the R codeused to produce the figure.

latexTemp.tex follows the rest of this presentation and is filled withexamples and extra comments. Open this file now.

2If this URL no longer works, find the zip file on http://scc.stat.ucla.edu/ in themini-course section.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Header section

Document type

Near the top of every LaTeX document is a line with the command\documentclass, which basically says what kind of document you aremaking.

Right now we’ll just use the class article. Other types of documentscan also be written, such as letters (letter), presentations (beamer), oreven books (book). Plenty of examples and help can be found online forthese other classes.

Alter [11pt] to change the default font size for the document, if desired.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Header section

Packages

Packages are loaded at the start of the document. These packagescontain new commands and environments or can be used to alter yourdocument in particular ways. For example, the package amsmath is givesmore equation options and fullpage formats the PDF output indifferent way.

Many common packages are included in a LaTeX installations, however,many other packages are not (amsmath is usually included in a LaTeXinstallation... fullpage is not). Additional packages can be downloadedand installed, as needed (we won’t cover this).

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Outline

Sections and subsections

Documents are often broken up into sections and subsections, and thishierarchy will automatically be numbered by LaTeX.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Page 5: Intro to LaTeX - UCLA Statistical Consulting Centerscc.stat.ucla.edu/page_attachments/0000/0077/intro_latex_1_09S.pdf · Intro to LaTeX David Diez ... Math and equations David Diez

Prep Getting started General Tables / tabbing Figures Math More Practice

Text

Paragraphs

To end a paragraph and create a new one, do a double “enter” and thiscreates a line break.

To put an extra line space between paragraphs, use the \\ commandfollowed by a double line break in the .tex document.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Text

Indentation and space

Indentation. To create an indent, use \indent, and to prevent anindent 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 aregenerally more useful in tables than in text.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Text

Playing with the font

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 coloredvia \color. Font can be made to look typewriterish via \texttt.

Font size. Text can also be made tiny, scriptsize, footnotesize, small, large,

Large, LARGE, etc. via \tiny, \scriptsize, etc.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Text

Lists

Lists can be created via

which results in a bulleted list such as the following:

Spacing

Text

Macros

A couple additional examples are in latexTemp.tex.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Page 6: Intro to LaTeX - UCLA Statistical Consulting Centerscc.stat.ucla.edu/page_attachments/0000/0077/intro_latex_1_09S.pdf · Intro to LaTeX David Diez ... Math and equations David Diez

Prep Getting started General Tables / tabbing Figures Math More Practice

Text

Macros

Many more text alterations can be done using the Macros.

Macros offer many many options to alter text.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Tabbing

Tabbing

Like in other text editors, LaTeX offers tabbing. While this environmenttends to rarely be used (at least by me), it can be very useful underparticular conditions.

See latexTemp.tex for a brief introduction to tabbing and a few examples.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Basic tables

Basic tables

Tables are created using the tabular environment. The {lcr} gives thealignment. The ampersands (&) are used to define when to go to thenext column.

The \\ command tells LaTeX to start a new row.

The result:

Left Center Right1 2 3

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Basic tables

Centering and adding lines

Building the table up:

The result:

Estimate Std. Error t value Pr(>|t|)(Intercept) -0.2852 0.8434 -0.34 0.7452x 0.4192 0.1499 2.80 0.0266

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Page 7: Intro to LaTeX - UCLA Statistical Consulting Centerscc.stat.ucla.edu/page_attachments/0000/0077/intro_latex_1_09S.pdf · Intro to LaTeX David Diez ... Math and equations David Diez

Prep Getting started General Tables / tabbing Figures Math More Practice

Captions & referencing

Floating table with a captionTo add a caption or label, the table must be floated (i.e. add on thetable environment). Then \caption can be used:

The output:

Estimate Std. Error t value Pr(>|t|)(Intercept) -0.2852 0.8434 -0.34 0.7452x 0.4192 0.1499 2.80 0.0266

Table: A regression summary.

The table is numbered when a caption is added in the articledocument class.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Captions & referencing

Referencing

Tables can be dynamically referenced (many examples throughoutlatexTemp.tex).

Usually a table has a number if it has a caption, e.g. Table 4. Thesenumbers 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’tcontrol this. Thankfully a table number can be dynamic referenced:

\label{regressTable} labels the table and then this table can bereferenced using the LaTeX code Table∼\ref{regressTable}.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

xtable

Using the R package xtable

Inside R:

> library(xtable) # may need install.packages(’xtable’)

> 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:

Estimate Std. Error t value Pr(>|t|)(Intercept) -0.1563 0.6243 -0.25 0.8107

x 0.1094 0.1145 0.96 0.3760z 2.6170 0.4308 6.08 0.0009

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Figures

Basic figures

A couple of the most basic examples of how figures can be added usingthe \includegraphics command.

The first file is stored in the same folder as the LaTeX file while thesecond is a couple folders away. The files and folder names used shouldhave no spaces.

The [height=1.0in] is an optional argument to control the figure size.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Page 8: Intro to LaTeX - UCLA Statistical Consulting Centerscc.stat.ucla.edu/page_attachments/0000/0077/intro_latex_1_09S.pdf · Intro to LaTeX David Diez ... Math and equations David Diez

Prep Getting started General Tables / tabbing Figures Math More Practice

Figures

Figure centering

Just like tables, a figure can be centered:

How does the second method look similar to the floating tables? (This isa floating figure.)

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Figures

Adding a caption and referencing

Just like in the floating table environment, a floating figure can have acaption and reference.

Just like with tables, the \label command must come after the\caption command for the reference to work properly.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Keeping organized

Keeping organized

Some tips to keeping organized when using LaTeX:

Do not include several LaTeX documents in a single folder.

When choosing a \label name for a figure, have it match the figurefile name.

Organize figure files into folders.

If you use code to produce a figure, save it to the same folder as thefigure and with the same name (but a different extension, of course).

Remember, no spaces in file or folder names.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Math in text

Inserting math into text

LaTeX makes it easy to add Greek letters like α, ζ, µ, etc. into text. Inthe same way, equations can be added easily as well: y = x3,

∑z j ,

x1 + · · ·+ xn.

It does take some practice to get used to writing math in LaTeX but,with practice, it becomes second nature. It is also sometimes easy togeneralize. For instance, look at how to create α above: $\alpha$. Howdo you think β would be created?

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Page 9: Intro to LaTeX - UCLA Statistical Consulting Centerscc.stat.ucla.edu/page_attachments/0000/0077/intro_latex_1_09S.pdf · Intro to LaTeX David Diez ... Math and equations David Diez

Prep Getting started General Tables / tabbing Figures Math More Practice

Math in text

Equation array

While math inside text is nice, sometimes the equations are long andshould be on their on lines. In such a case, use the eqnarray oreqnarray∗ environment:

The result in LaTeX for eqnarray∗:

∞∑k=0

0.5k =1

1− 0.5= 2

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Math in text

Equation referencing

Just like tables and figures, equations can also be referenced. Useeqnarray (no asterisk) to make the equation be a numbered equation:

∞∑k=0

0.5k =1

1− 0.5= 2 (1)

\label{powerSeries} can be put inside the equation array and then bereferenced via \ref{powerSeries}.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Math in text

Matrices

Matrices also can be made in LaTeX:(4 1 193 8 8

)The code:

The syntax for an array is the same as for tabular (a table).

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Math in text

LaTeX and Matrix Panels

Since it is a little difficult to learn all the math syntax, a good place tolook for the proper syntax (using a click interface) is to the LaTeX andMatrix Panels:

The Matrix Panel is especially useful since matrices can require a lot ofwriting.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Page 10: Intro to LaTeX - UCLA Statistical Consulting Centerscc.stat.ucla.edu/page_attachments/0000/0077/intro_latex_1_09S.pdf · Intro to LaTeX David Diez ... Math and equations David Diez

Prep Getting started General Tables / tabbing Figures Math More Practice

Examples

Diverse possibilities

LaTeX has a lot of power to do diverse tasks:

Chemistry figures

Circuits

Feynman diagrams

Musical scores

Crossword puzzles

Sudoku puzzles

These different applications require packages that are generally notincluded in a basic download of LaTeX but that can typically bedownloaded at no cost.

The ability to make elegant documents and figures is one of LaTeX’smajor draws. It offers users nearly unlimited control of their documents(even though it might feel a bit out of control at first).

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Resources

Resources

Online help:

http://info.stat.ucla.edu/grad/ — links to many useful sites

http://forums.stat.ucla.edu/?5 — online forum help

A good resource in book format:

Guide to LaTeX, by Helmut Kopka and Patrick W. Daly

A document that can be used to find LOTS of symbols (4MB):

www.ctan.org/tex-archive/info/symbols/comprehensive/symbols-a4.pdf

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Survey

Survey

Please take the time to complete a survey about this mini-course:

http://scc.stat.ucla.edu/survey

Your feedback is greatly appreciated.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Practice

Try this #1

Produce the following result:

Some examples may be utilized in latexTemp.tex.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Page 11: Intro to LaTeX - UCLA Statistical Consulting Centerscc.stat.ucla.edu/page_attachments/0000/0077/intro_latex_1_09S.pdf · Intro to LaTeX David Diez ... Math and equations David Diez

Prep Getting started General Tables / tabbing Figures Math More Practice

Practice

Try this #2

Using the image in latexTemp/normalPlots/lower82/, produce thefollowing plot. Make it 0.8 inches tall, center it, add a caption, and add areference. Be sure to write a sentence that references the Figure andcompile your LaTeX document twice so the reference works.

60.1 63.4 66.7 70 73.3 76.6 79.9

82%(0.82)

18%(0.18)

Feel free to utilize examples in latexTemp.tex or to use the LaTeXFloat Figure template.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center

Prep Getting started General Tables / tabbing Figures Math More Practice

Practice

Try this #3

Produce the following result using the eqnarray∗ environment:

Some examples may be utilized in latexTemp.tex.

David Diez

Intro to LaTeX UCLA Department of Statistics Statistical Consulting Center