• Home
• Foundations
Thomas A. Alspaugh
LATEX

LATEX is a free, open-source document typesetting system that is particularly strong for typesetting mathematics conveniently and everything beautifully, and for handling cross-references and bibliographies painlessly. LATEX source files are text, so it is well suited for collaboration, emailing, and version control, and all text-oriented tools can be used with it. Major journals and conferences provide LATEX class files that accomplish their formatting requirements painlessly. Pronounced la-Tek or Lay-tek, said Lamport. Not Lay-teks (shudder). Lamport's LATEX is a macro package based on Knuth's TEX, and Knuth said TEX was not spelled t-e-x but actually τ-ε-χ (tau epsilon chi).

# The books

The key references are:

Leslie Lamport. LaTeX: A Document Preparation System: User’s Guide and Reference Manual. Addison-Wesley, second edition, xvi+272 pages, 1994.

This second edition covers the current version of LATEX.

Frank Mittelbach and Michael Goossens. The LaTeX Companion. Addison-Wesley, second edition, xxvii+1090 pages, 2004. First edition listed Goossens as first author.

This second edition covers far more than the first. The first edition listed Goossens's name first.

Scott Pakin. The Comprehensive LATEX Symbol List. Nov. 2009.
Abstract

Every symbol you could possibly want, plus thousands you will never need.

# Simple things people often struggle with

1. % begins a line comment. Use \% for a percent character.
2. Special characters #$%&^_~\{} are used to control LATEX. For a literal, precede most of them with a backslash: \# \$ \% \& \{ \}.

LATEX provides the \textasciicircum, \textunderscore, \textasciitilde, and \textbackslash commands to produce text versions of those characters.

Tildes are most often needed in URLs, for which use the url package and enclose each URL in a \url{ } command. The \url command handles some other special characters, as if they were in a URL.

The underscore package redefines _ in text mode to be an underscore character.

3. Quotation marks are expressed with  and '.
1. Never use " as a quotation mark.
2. For single quotes, use  ': X' formats as ‘X’.
3. For double quotes, use  '': X'' formats as “X”.
4. Watch out for Windows quote characters!
4. LATEX distinguishes hyphens, en-dashes, and em-dashes.
1. Hyphens appear within words only, like pre-condition. A single dash - makes a hyphen.
2. En-dashes (a dash the width of an n, roughly) are used in ranges only. A double dash -- makes an en-dash. Example: pages 3--5 formats as "pages 3–5".
3. Em-dashes (a dash the width of an m, roughly) are used sometimes to divide parts of a sentence. A triple dash --- makes an em-dash. Example: Frogs---a kind of reptile---live in water formats as "Frogs—a kind of reptile—live in water".
5. The tilde character ~ represents a non-breaking space. It is commonly used between "Figure" (or "Table", "Section", etc.) and the figure (table, section, …) number: Figure~\ref{fig:process} formats as "Figure 1", with never a line break between "Figure" and "1".
6. To prevent line breaks in a word or phrase, enclose it in an mbox: \mbox{words}.
7. To encourage line breaks in a word at specific points (but not others), put a hyphenation command \- where a hyphenated break would be good: hy\-phen\-ation will only be broken and hyphenated at those two points, and not at "hyphena-tion" as it otherwise might. To encourage specific hyphenations at every occurrence of a word, list it with dashes at hyphenation points in a \hyphenation command in the file's preamble:
\hyphenation{hy-phen-ation per-co-late}
8. LATEX puts a wider space after the period at the end of a sentence (in English — in some other languages such as French, this is not the custom and LATEX doesn't do it there). This is implemented by putting extra space after every period preceded by a lower-case letter. This works almost all the time (it handles initials, for example), but not for abbreviations like et al. which are commonly followed by a period. To prevent the extra space, use the interword space command which is a backslash followed by a space. Example: \textit{et al.}\ .
9. If you need to typeset a URL, use the url package and put the URL in the argument of a \url command, thus:
    \usepackage{url}  % immediately after the \documentclass command
…
\url{URL}

10. It is best to edit your LATEX paragraphs not as single long lines in whatever editor you use, but broken into fairly short lines with no more than one sentence to a line. For long sentences, have no more than one phrase to a line. This lets you use all the Unix text commands effectively (such as diff), and lets subversion identify your changes effectively and show them to you in a useful way.
11. It's tempting to use the citation labels BibTEX generates as the citation keys by which you reference them in \cite commands. This is not a good idea:
1. the generated labels change depending on the bibliography style you use;
2. collisions are frequent, so that you end up with keys like RW98a and RW98b, and the a's and b's change depending on what references are cited in a particular document;
3. the risk of citing the wrong reference is increased, because you can't tell what RW98b (or often even AD94) is without looking it up.

# The tools

The free TeXShop application for OSX (www.uoregon.edu/~koch/texshop) makes formatting LATEX very convenient. It can be used with its built-in editor or with your own favorite text editor.

tetex seems to be the best latex implementation at present. It is free and available for OSX and Windows. For OSX, I recommend installing fink (www.finkproject.org) and then tetex.

Some tetex installations (including fink's) are set up to produce A4 (European) pdf files. To produce 8.5"x11" pdfs, you can put these commands in your file's preamble:

\setlength{\pdfpagewidth}{8.5in}
\setlength{\pdfpageheight}{11in}


I have some tools that you will find helpful: bibfile, bibKeys, latexCheck, latexCites.

# How to use LATEX effectively

The LATEX language is primarily about expressing structure, and only secondarily about presentation.

Structure your document first, then adjust the presentation only if something really doesn't look right. Remember that LATEX is usually a much better typesetter than you are.

A document typically consists of:

• a title and related things (author, contact information, date, …)
• an abstract (for some document classes)
• sections
• references
• appendices

Each section may have subsections; each subsection may have subsubsections; and so forth.

Content (in the form of paragraphs, lists, block quotes, figures, etc.) goes in the sections, subsections, subsubsections, etc.

# Structure of a LATEX main file

The main file consists of:

• A \documentclass{C} line, where C is the document class. The standard classes are
• article
• book
• letter
• report
• slides
• the preamble, consisting of:
• Zero or more \usepackage{package} lines. These load a package of commands and environments that you can then use in your document.
• The \title{title}, \author{author}, and other commands for the title information, any \newcommand definitions (see Lamport), any \setlength commands (see Lamport), and other definitions or redefinitions of things.
• The document environment containing all your text, commands, and other environments:
• \begin{document}
• \maketitle if you are using a title
• \begin{abstract}, the text of the abstract, and \end{abstract}, if you are using an abstract
• Everything else, including \input{filename} commands that include a file named filename.tex. It must have the .tex suffix and this suffix can't appear in the command argument.
• \end{document}

# Writing LATEX files: commands and environments

Everything is done with commands, environments, and text.

Text consists of letters, digits, spaces, and punctuation .:;,?!'()[]-/*@. It may have commands and environment mixed in with it as needed.

A command consists of the command name, possibly optional arguments in [], and possibly required arguments in {}. Command names begin with a backslash \ , like \section. Command arguments follow the command in curly braces {} or square brackets []: braces for required arguments (most arguments are required) and brackets for optional arguments.

\section{Heading for this section}

LATEX elides the whitespace following a command. If you need to retain whitespace after a command, either enclose the entire command (arguments and all) in braces { } or use the command (backslash followed by space).

An environment is set off with \begin and \end commands, each with the environment name as an argument. An environment named E consists of \begin{E} followed by the contents of the environment and terminated with \end{E}.

\begin{figure} … \end{figure}

The document itself is enclosed in a document environment:

\begin{document} … \end{document}.

# Kinds of text presentations

## Characters

### Character formatting

Put text to emphasize in the argument of an \emph command: \emph{emphasized}. It will be handled appropriately in all contexts (for example, in text that's already italicized).

• \emph{emphasized}

LATEX supports character formatting independent of structure; it is preferable to use \emph for emphasis rather than any of these. Some examples are:

• \textit{italic}
• \textbf{bold}
• \textsc{small-caps}
• \textsf{sans-serif}

### Text symbols

LATEX has a vast range of accented character commands, with examples here using o, and other letter symbols:

 ò \{o} ó \'{o} ô \^{o} ö \"{o} õ \~{o} ō \={o} ȯ \.{o} o̮ \b{o} ȩ \c{e} ọ \d{o} ő \H{o} o͡o \t{oo} ŏ \u{o} ǒ \v{o}
 å, Å {\aa}, {\AA} æ, Æ {\ae}, {\AE} ç, Ç \c{c}, \c{C} ł, Ł {\l}, {\L} ø, Ø {\o}, {\O} œ, Œ {\oe}, {\OE} ß {\ss}

Some common text symbols are:

 # \# $\$ % \% & \& ^ {\textasciicircum} _ {\textunderscore} ~ {\textasciitilde} \\ {\textbackslash} { \{ } \}
 … {\dots} < {\textless} > {\textgreater} § {\S} ¶ {\P} © {\copyright} … {\ldots}

(LATEX also has many, many mathematical symbols not discussed here.)

## Paragraphs

A blank line starts a new paragraph.

If you have to start a paragraph with a command, it's \par — that's the command a blank line executes.

## Itemized lists (bulleted)

An itemized list is an itemize environment with \item commands, one per list item.

\begin{itemize}
\item The first item.
\item The second item.
\end{itemize}


## Enumerated lists (numbered)

An enumerated list is an enumerate environment with \item commands, one per list item.

\begin{enumerate}
\item The first item.
\item The second item.
\end{enumerate}


## Block quotes

Long quotes are traditionally typeset as a separate block of text, with extra space on all four sides. In LATEX this is done by enclosing the quote in a quote or quotation environment. quotation is only for multi-paragraph quotes.

\begin{quote} A long quote … \end{quote} formats as

A long quote …

Sections are marked by their headings, but not otherwise distinguished. There is no environment for a section, for example.

The text of a section (subsection, subsubsection) heading is the argument of a \section (\subsection, \subsubsection) command:

\section{The Section's Heading}

Some text in the section.

This is text in the subsection.


The result is shown in Figure 1.

For unnumbered headings, use \section* (\subsection*, \subsubsection*).

# Figures and tables

Each figure (table) is enclosed in a figure (table) environment. Figures and tables are floated to an appropriate location by LATEX. The environments take an optional location argument that influences where the figure (table) will end up, consisting of any combination of h (here), t (top of a page), b (bottom of a page), p (on a separate page of figures and tables), and ! (tell LATEX to try harder).

There are more extreme measures for controlling floats; see Lamport.

In two-column mode, a double-column figure (table) is enclosed in a figure* (table*) environment.

You can put anything you like in a figure or table environment — it doesn't have to be an actual figure or table.

## Centering

Figures (tables) are not centered by default. Put the \centering command immediately after the \begin{figure} (\begin{table}) command to center the contents of the environment.

## Captions

A figure or table's caption is given by the \caption command with the caption text as its argument: \caption{Tex of the caption}.

LATEX automatically numbers captioned figures and tables (separately) for you, and puts the number in the caption, so don't you do so too.

To reference a figure (table) number elsewhere, use the \label command after the caption to set an internal label at the figure's (table's) location, and the \ref command to refer to that label. It has to be after the caption, as LATEX increments the figure (table) number only when it sees the caption.

## Making tables

LATEX has the tabular environment for making tables. See Lamport for more details. I don't recommend it. Make your tables in HTML, OmniGraffle, or some other more convenient tool, save them as images, and use the image as your table.

## Images

The best way to handle images is to use the graphicx package and include the image using the \includegraphics command. The name of the image file is a required argument, and the size or various other possibilities are optional arguments.

\usepackage{graphicx}  % after the \documentclass command
…
\includegraphics[width=2in]{image.pdf}


PDF images work best (they are vector images and scale perfectly) if you are using pdflatex as the formatting engine. You can specify the width or height in inches, centimeters (cm), millimeters (mm), points (pt), or various other units.

# Labels and references

The label command \label defines a label for its location whose name is given in its required argument, and the ref command \ref shows the value of the label in its required argument.

Figure~\ref{fig:process} shows our process diagram.

\begin{figure}\centering
\includegraphics[width=3cm]{Process.pdf}
\caption{The process diagram}
\label{fig:process}
\end{figure}


The convention is that figure labels begin with fig:, table labels with tbl:, section labels with sec:, and equation labels with eq:.

# Mathematics

LATEX is great at typesetting mathematics. Mathematics typesetting is done in math mode, and LATEX uses $to get in and out of math mode, so escape your dollar signs with backslashes if you just want to put a$ in your document.

The \$'s are a shorthand for beginning and ending the math environment. It is slightly less convenient, but much safer, to use  to surround inline mathematics to typeset.

There is also the displaymath environment for displayed mathematics (in a separate centered paragraph), and the  shortcuts for it.

See Lamport for more information. There are lots of possibilities and they all work wonderfully, but it's too much to discuss here.

# The title and related material

LATEX collects the title, author names, etc. (from \title{title}, \author{author}, \date{date} (or \date{} for no date), etc. commands in the preamble). The title material actually appears later where the \maketitle command appears.

Workshops, conferences, and journals often redefine the \maketitle command to fit their special formats, and sometimes add additional information commands like \abstract.

Basic LATEX has commands for dealing with these but they aren't very helpful.

The fancyhdr package does a much better job. It has commands \lhead{left-header}, \chead{center-header}, \rhead{right-header}, \lfoot{left-footer}, \cfoot{center-footer}, and \rfoot{right-footer} for specifying text to appear in those places. Use the LATEX \thepage command wherever you need the page number. You may need to adjust the page layout parameters to set up space for headers and footers — this is not a trivial task. See Goossens for more information.

# References and the bibliography

LATEX and BibTEX together do a great job of handling citations and references.

In my experience the most accurate source is the ACM portal, which has relatively accurate BibTEX entries for many papers from the ACM and from other sources. The public CCSB has a vast collection of BibTEX entries (including all of mine), but you will need to verify them as the accuracy varies widely depending on where the entry came from. Most of the entries in my personal bibliography (online at CCSB) have been checked and verified against the source document and venue, and are accurate.

Each BibTEX entry has a key on its first line by which you can reference it in a \cite command (not to be confused with the key = field whose value is used for sorting the references and which need not match the entry key):

/* in the bib file */
author =       {Winbladh, Kristina and Alspaugh, Thomas A. and Ziv,
title =        {An Automated Approach for Goal-driven,
Specification-based Testing},
booktitle =    {21th International Conference on Automated Software
Engineering (ASE 2006)},
year =         {2006},
pages =        {289--292},
month =        sep,
)

% in the LATEX file


Then LATEX and BibTEX collect and format the references into a bibliography for your document, using the right number or alphanumeric key for each citation.

Here's how to make this work:

1. Collect all the bib entries you need in one or more files.
2. Put a \bibliography{filename} command in your LATEX file where you want the References section to appear.
3. Put a \bibliographystyle{STYLE} command in front of it, to control the style of the references. The standard styles are
 abbrv plain with first names, month names, and journal names abbreviated alpha plain with Aut04 labels rather than numbers. plain van Leunen format entries, numbered. unsrt plain but entries appear in order of first citation rather than alphabetically
4. Run latex to put all the citation keys in the .aux file.
5. Run BibTEX to put all the reference information for them in the .bbl file.
6. Run latex to get all the references from the .bbl file and construct the References section.
7. You may have to run latex once again to get all the citations in the text set up (if this is necessary, there will be a message about "Some references were undefined …" in latex`'s output).

There are several bib styles besides alpha; see Lamport.

It's also possible to include an uncited reference in the reference list: give its key in a \nocite command. \nocite{*} adds all the references in your \bibliography file(s) to the reference list.

# How the document presentation is controlled

It's all controlled in the class file, and then to a lesser extent in package style files, and then to a small extent in your document files. Changing the presentation in the class or package styles is nontrivial, and not discussed here. How to make a style or class file …