# Quick Intro to LaTeX

## Introduction

### What is LaTeX?

LaTeX, pronounced “la-tek,” is a text formatter. Unlike word processors like Microsoft Word, LaTeX is not a “what you see is what you get” editor. Instead, the user writes LaTeX code representing the content and desired formatting. LaTeX then compiles this code to produce a formatted document. The typical writing and editing cycle with LaTeX is

- Write LaTeX code
- Compile
- View output
- Return to Step 1 for editing

### Why should I use it?

**Disadvantages:**

- Less intuitive than word processors
- Many commands to learn
- Time lost to debugging LaTeX code

**Advantages:**

- Automatic numbering of sections, citations
- Beautiful math formulas with fine control
- Fine control over spacing
- Packages and user-defined macros

LaTeX is a professional typesetting tool providing great control, particularly with math formulas. However, it does take patience to learn. If none of this is appealing, LaTeX is not for you.

## What tools does this require?

LaTeX code is plain text, so any text editor will suffice. To compile LaTeX code, a TeX system with LaTeX is necessary. Two free TeX systems available for download are MikTeX for Windows and TeXLive for Linux. Third, a viewer program is necessary to view the output. LaTeX typically outputs DVI, PS, or PDF format files. Adobe Reader can view PDF files and the Ghostscript + Ghostview system can view all three formats.

Although not strictly necessary, it is a good idea to use a spell checker. Two free spell checkers are Aspell and Ispell.

## LaTeX Basics

The structure of a LaTeX document is a preamble followed by a body:

`\documentclass[options]{class}`

**Preamble:** Global commands, settings, and macro definitions

`\begin{document}`

**Body:** Text and local commands

`\end{document}`

The `\documentclass`

line specifies the general options like paper size and default font size and the type or “class” of document. The preamble is used to include packages, set global parameters like margin widths, and define macros. The body is mostly plain text with occasional commands for special symbols, changing fonts, or other formatting.

- All characters may be used directly except # $ & ~ _ ^ % { } \, which have special meaning.
- The special characters # $ & ~ _ ^ % { } may be printed by prefixing
`\`

, for example,`\&`

to print &. - All commands are
`\`

followed by a sequence of letters, for example,`\textbf`

for writing in**bold face**.

For example, the body text

`You can get this \textbf{amazing product} for only \$49.99!`

compiles to

You can get this **amazing product** for only $49.99!

## Hello, LaTeX World!

The best learning is through experience. Get started with this exercise:

*demo.tex*

`\documentclass[12pt]{article}`

`\begin{document}`

`The \#1 story is that this article was compiled today (\today) with`

`\LaTeX. To try some commands, it includes \textit{a few fancy}`

`\textbf{fonts}.`

`\end{document}`

- Copy the code above into a text editor and save it as demo.tex.
- Compile by running the shell command
`pdflatex demo.tex`

(alternative:`latex demo.tex`

followed by`dvipdf demo.dvi`

). - View the resulting demo.pdf file.

## Fonts

As demonstrated above with `\textbf`

, text is written in another face by using a command with the text enclosed in curly braces `{ }`

.

`\textbf{...}`

**Bold face**

`\texttt{...}`

`Typewriter`

`\textit{...}`

*Italics*

`\textsl{...}`

*Slanted*

`\underline{...}`

Underlined

Font commands may be nested, for example, `\textbf{super \textit{stylish}}`

yields **super stylish**.

## Line, Paragraph, and Page Breaks

LaTeX ignores single line breaks in the code. To indicate a paragraph break, use two line breaks. To force a line break within a paragraph, use two backslashes `\\`

.

**Code:**

`In 1812, Bessel was elected to the Berlin Academy and won an award from the academy a few years later for estimations of precession and aberration constants. In 1825, he was elected as a Fellow of the Royal Society.`

`In 1830, Bessel published his calculations for the positions of 38 stars over the years 1750--1850. In 1838, he determined that the star Sirius has a companion star, Cygni, and calculated its position. The star was later observed in 1862, verifying his conjecture.`

**Output:**

In 1812, Bessel was elected to the Berlin Academy and won an award from the academy a few years later for estimations of precession and aberration constants. In 1825, he was elected as a Fellow of the Royal Society.

In 1830, Bessel published his calculations for the positions of 38 stars over the years 1750–1850. In 1838, he determined that the star Sirius has a companion star, Cygni, and calculated its position. The star was later observed in 1862, verifying his conjecture.

To add a blank line between two paragraphs, use `\bigskip`

:

**Code:**

`In 1812, Bessel was elected to the Berlin Academy and won an award from the academy a few years later for estimations of precession and aberration constants. In 1825, he was elected as a Fellow of the Royal Society.`

`\bigskip`

`In 1830, Bessel published his calculations for the positions of 38 stars over the years 1750--1850. In 1838, he determined that the star Sirius has a companion star, Cygni, and calculated its position. The star was later observed in 1862, verifying his conjecture.`

**Output:**

In 1812, Bessel was elected to the Berlin Academy and won an award from the academy a few years later for estimations of precession and aberration constants. In 1825, he was elected as a Fellow of the Royal Society.

In 1830, Bessel published his calculations for the positions of 38 stars over the years 1750–1850. In 1838, he determined that the star Sirius has a companion star, Cygni, and calculated its position. The star was later observed in 1862, verifying his conjecture.

LaTeX determines intelligent page breaking automatically (avoiding problems like orphan lines), but it is occasionally necessary to manually indicate page breaks. To force a page break, use the command `\pagebreak`

. To prevent a page break, use `\nopagebreak`

.

## Math in LaTeX

A math formula can be written within a line of text (text style), or apart on its own line (display style). Text style math is enclosed with dollar signs: `$ ... $`

. Display style math is enclosed with either `\[ ... \]`

or double dollar signs `$$ ... $$`

. The enclosed code is called the formula text.

### Basic Symbols

Many math symbols are intuitively represented in the formula text. For example, `$|f(x)| > 2M$`

produces the formula |*f*(*x*)| > 2*M*.

- Letters, numbers, and the symbols
`+ - / = < > : | ( ) [ ]`

work directly. - Spaces between these symbols has no effect.
`$ x + y $`

and`$x+y$`

have the same output. - To produce curly braces { }, use
`\{`

and`\}`

.

### Superscripts and Subscripts

Superscripts and subscripts are made using `^`

and `_`

. For example, `$x^2 + y^2$`

yields *x*² + *y*². A symbol can have both a superscript and a subscript: `$A_n^k$`

yields *A _{n}^{k}*.

- A superscript or subscript with multiple symbols must be enclosed with
`{ }`

. For example,`$Y_{2n}$`

to produce*Y*_{2n}. - If a superscript or subscript is enclosed with
`{ }`

, it can itself have superscripts and subscripts.

### Fractions

Graphical fractions are made with `\frac{ numerator }{ denominator }`

. The formula `\[ \frac{1}{x^2 + y^2} \]`

produces

### Sums, Products, Limits, and Integrals

Sums, products, limits, and integrals are produced with commands `\sum`

, `\prod`

, `\lim`

, and `\int`

. They usually have upper and lower limits, which are added like superscripts and subscripts. For example, `\[ \sum_{k=0}^N \]`

for

### Functions

LaTeX commands `\exp`

, `\log`

, `\cos`

, `\sin`

, `\arccos`

, `\min`

, `\max`

, `\inf`

, `\sup`

, and others write common functions, which are typeset in Roman font instead of italics.

For example, `$sin(x)$`

generates *sin*(*x*) while `$\sin(x)$`

generates sin(*x*). For other functions not in this list, force Roman font with `\mathrm{functionname}`

.

### Parenthesis

The formula text `\[ \exp( -\frac{x^2}{2} ) \]`

has output

To generate parenthesis with the correct size, use the `\left`

and `\right`

commands. The `\left`

command is placed in front of the opening parenthesis and the `\right`

command is placed with the matching closing parenthesis. Correcting the example formula, `\[ \exp\left( -\frac{x^2}{2} \right) \]`

has output

`\left`

and`\right`

must appear in pairs.- They may also be used with other bracket symbols, including [ ] { } |.
- The bracket symbols need not match, for instance
`\left\{ ... \right|`

is legal. - To produce a single opening or closing bracket, make the other bracket invisible with
`\left.`

or`\right.`

.

### Greek and Special Symbols

LaTeX provides an extensive set of math symbols. Symbols are represented with `\`

followed by the symbol name:

`\alpha `

α

`\beta `

β

`\gamma `

γ

`\delta `

δ

`\epsilon `

ϵ

`\le `

≤

`\ge `

≥

`\ne `

≠

`\approx `

≈

`\pm `

±

`\infty `

∞

`\rightarrow `

→

`\Rightarrow `

⇒

`\partial `

∂

`\exists `

∃

`\forall `

∀

`\in `

∈

`\not\in `

∉

And there are many more if you need them.

### Spacing

Sometimes formulas need manual adjustments to space symbols properly. A “quad” is the length equal to the font size, 1 quad = 11 pt with 11 pt font size. These commands add horizontal spacing:

`\, `

3/18 quad space

`\: `

4/18 quad space

`\; `

5/18 quad space

`\quad `

1 quad space

`\qquad `

2 quad space

`\! `

−3/18 quad negative space

Use the small space command `\,`

to adjust where symbols are a little too close. Use the negative space command `\!`

to squeeze together symbols that are otherwise too far apart. Multiple negative spaces `\!\!\!`

can be used to squeeze further. For larger spaces, use `\quad`

and `\qquad`

.

### Normal Text within a Formula

It is frequently necessary to write normal text within a math formula, particularly single words and short phrases like “if” and “such that.” This can be done with `\mbox{ normal text }`

. For example,

`\[ f(x_0) = f(x_1) \qquad \mbox{if and only if} \qquad x_0 = x_1 \]`

produces

### Math Fonts

It is sometimes useful for notation to use another font than the standard one, for example, writing vectors in boldface. The following commands change the font of the math they enclose

`\mathrm{...} `

Roman

`\mathbf{...} `

Bold

`\mathtt{...} `

Typewriter

`\mathsf{...} `

Sans serif

`\mathit{...} `

Italic

`\mathcal{...} `

Calligraphic

The normal math font is forced with `\mathnormal{...}`

.

### Typesetting differentials

When a differential precedes or follows other symbols, insert a small space with `\,`

. For example, `$\int \phi(x) \,dx$`

to produce ∫ f(x) dx rather than ∫ f(x)dx.

### Exercise

See if you can reproduce the following formula:

## Document Structure

LaTeX offers commands for creating title pages, numbered sections, bibliography, and other features necessary in a structured document.

### Title Page

To create a title page, write the following commands at the beginning of the body text:

`\title{title text}`

`\author{author \\ institute \\ address}`

`\maketitle`

### Abstract

Write an abstract with

`\begin{abstract} abstract text \end{abstract}`

### Sections

Sections headings are defined and automatically numbered with the commands

`\part \chapter \section \subsection \subsubsection`

Use for instance `\section{Methodology}`

to create a section with heading text "Methodology". Use `\section*{title}`

to omit numbering. For example, a research article might be structured as

`\section{Introduction} `

`...`

`\subsection{Previous Work}`

`...`

`\section{Background}`

`...`

`\section{Theory}`

`...`

`\subsection{Main Theorem}`

`...`

`\subsection{Implications}`

`...`

`\section{Experiments}`

`...`

`\section{Conclusion}`

`...`

### Table of Contents

The command `\tableofcontents`

produces a table of contents based on the section commands. When sections are added, change order, or if a section heading appears on a different page, the LaTeX code must be compiled twice to create the table of contents correctly.

### Bibliography and Citations

A bibliography is structured like

`\begin{thebibliography}{samplelabel}`

` \bibitem{key1}entry text 1`

` \bibitem{key2}entry text 2`

` ...`

`\end{thebibliography}`

Each `\bibitem`

command represents an entry in the bibliography. The entry text is the actual bibliographic information (author, title, publication, etc.). The key argument is a keyword for referring to the entry. To cite the entry within the text, use the command `\cite{key}`

. The `samplelabel`

argument is simply to indicate the number of digits in the largest label. If there are between 10 and 99 entries, use 99.

For example, the bibliography code

`\begin{thebibliography}{9}`

`\bibitem{Heijmans} H. Heijmans and J. Goutsias. ``Nonlinear Multiresolution Signal Decomposition Schemes--Part II: Morphological Wavelets.'' \textsl{IEEE Transactions on Image Processing}, 2000. `

`\bibitem{Do} M. Do, \textsl{Directional Multiresolution Image Representations}, Ph.D. Thesis, Department of Communication Systems, Swiss Federal Institute of Technology Lausanne, 2001. `

`\end{thebibliography}`

produces

**References**

[1] H. Heijmans and J. Goutsias. “Nonlinear Multiresolution Signal Decomposition Schemes–Part II: Morphological Wavelets.” *IEEE Transactions on Image Processing*, 2000.

[2] M. Do, *Directional Multiresolution Image Representations*, Ph.D. Thesis, Department of Communication Systems, Swiss Federal Institute of Technology Lausanne, 2001.

The text

`For background on contourlets, see \cite{Do}.`

produces

For background on contourlets, see [2].

## Closing

You now have the foundation to begin writing documents with LaTeX. This guide is only a quick introduction to some of the most useful features of LaTeX. Beyond this guide, other LaTeX features and package features you may want are

- Importing images → graphicx package
- AmS-LaTeX math extensions → amsmath package
- Listing code with syntax highlighting → listings package
- Hyperlinks → hyperref package
- Tables, lists, figures → see the tabular, itemize, and figure environments