A Quick Introduction to LaTeX
Pascal Getreuer
Introduction
What is LaTeX?
LaTeX, pronounced “lah-tech” or “lay-tech” (and name formatted as \(\LaTeX\)), 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 is fine. There are editors specifically designed for LaTeX, if you prefer, see for instance TeXworks or Texmaker—both are available on Windows, MacOs, and Linux.
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.
\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
\textbf{Please refrigerate} the half \& half!
compiles to
Please refrigerate the half & half!
Hello, LaTeX World!
The best learning is through experience. Get started with this exercise:
demo.tex
\documentclass[12pt]{article}
\begin{document}
\#1 story is that this article was compiled today (\today) with
The \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
These commands may be nested: \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 \\
.
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.
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
:
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.
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 $$
… $$
.
Basic Symbols
Many math symbols are intuitively represented in the formula text.
For example, $|f(x)| > 2M$
produces the formula \(|f(x)| >
2M\).
- 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^2 + y^2\). 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
\[ \frac{1}{x^2 + y^2} \]
Sums, Products, Limits, and Integrals
Sums, products, limits, and integrals are produced with commands
\sum
, \prod
, \lim
, and
\int
. They may have upper and lower limits, which are added
like superscripts and subscripts. For example, \[ \sum_{k=0}^N \]
for
\[ \sum_{k=0}^N \]
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
\[ \exp( -\frac{x^2}{2} ) \]
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,
\[ \exp\left( -\frac{x^2}{2} \right) \]
has output
\[ \exp\left( -\frac{x^2}{2} \right) \]
Notes:
\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 has an extensive set of math symbols. Symbols are represented
with \
followed by the name:
α | \alpha | κ | \kappa | υ | \upsilon | Ξ | \Xi |
β | \beta | λ | \lambda | φ | \phi | Π | \Pi |
γ | \gamma | μ | \mu | χ | \chi | Σ | \Sigma |
δ | \delta | ν | \nu | ψ | \psi | Υ | \Upsilon |
ε | \epsilon | ξ | \xi | ω | \omega | Ψ | \Psi |
ζ | \zeta | π | \pi | Γ | \Gamma | Φ | \Phi |
η | \eta | ρ | \rho | Δ | \Delta | Ω | \Omega |
θ | \theta | σ | \sigma | Θ | \Theta | ||
ι | \iota | τ | \tau | Λ | \Lambda | ||
± | \pm | → | \rightarrow | ≠ | \ne | ∃ | \exists |
∞ | \infty | ⇒ | \Rightarrow | ≡ | \equiv | ∀ | \forall |
≤ | \le | ⇔ | \Leftrightarrow | ≈ | \approx | ∈ | \in |
≥ | \ge | ∴ | \therefore | ∼ | \sim | ∉ | \not\in |
And 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:
\quad
|
one quad |
\qquad
|
two quads |
\,
|
3/18 quad |
\:
|
4/18 quad |
\;
|
5/18 quad |
\!
|
−3/18 quad |
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) = f(y) \quad
\mbox{if and only if} \quad
x = y \]
produces
\[ f(x) = f(y) \quad \mbox{if and only if} \quad x = y \]
Math Fonts
It is sometimes useful for notation to use another font, say, writing vectors in boldface. The following commands change the font of the math they enclose
Roman | \mathrm{…} | Bold | \mathbf{…} |
Sans serif | \mathsf{…} | Italic | \mathit{…} |
Typewriter | \mathtt{…} | Calligraphic | \mathcal{…} |
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 \(\int \phi(x)\, dx\) rather
than \(\int \phi(x) dx\).
Exercise
Reproduce the following formula:
\[ \sum_{k=-\infty}^{+\infty} \frac{1}{2\pi} \int_{-\pi}^\pi f(\xi) \mathrm{e}^{-ik\xi} \, d\xi \, \mathrm{e}^{ikx} \]
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
\begin{thebibliography}{samplelabel}
\bibitem{key 1}entry text 1
\bibitem{key 2}entry text 2
...\end{thebibliography}
Each \bibitem
command adds an entry to the bibliography.
The entry text is the author, title, and other bibliographic info. The
key argument is a keyword to refer to the entry. Use the command \cite{key}
within the text to cite it. The samplelabel argument is simply for 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}{99}
\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 code
\cite{Do}. For background, see
produces
For background, 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 features you may want are
- Importing images
- AmS-LaTeX math extensions
- Listing code with syntax highlighting
- Hyperlinks
- Tables, lists, figures
-
→ see the tabular, itemize, and figure environments