LaTeX Example Snippets
A LaTeX document consists of two parts: the preamble and the document body.
The preamble always starts with the command \documentclass[]{}.
A comma-separated list of optional arguments to configure the document class properties follows between the square brackets “[]”.
The document class is given between the curly brackets “{}”.
It is a so called mandatory argument.
In the preamble, we define document properties, customize default configurations, and include LaTeX packages that provide additional properties and macros inside the document.
The second part of a LaTeX document is the so called document body.
This part is framed in the \begin{document} and \end{document} directive.
\documentclass[11pt]{scrartcl}
\begin{document}
\end{document}
Note
To see what a given command does, create a file called “example.tex”.
Copy the snippet given above in this file.
Next, copy the command in the document body and compile it.
To do so, use TeXstudio or the command line.
On the command line (this works on Windows PowerShell too), use the command latexmk -pdf example.tex.
To see the result open the PDF file with your favorite PDF viewer.
Blanks/Spaces
In the content part, one or more consecutive spaces between words are always collapsed into a single space in the output.
For a space between words without a line break use the tilde “~” character.
For more than one space, escape each extra space with a backslash followed by a space “\ “.
Note
Do not align content with escaped spaces.
For vertical alignment use macros such as \vspace, \vskip, \vfill, etc. instead.
Paragraphs
A new paragraph is started with a blank line (a double newline) in the source.
LaTeX collapses any single newlines within a paragraph and simply reflows the
text. The blank line is what tells LaTeX where one paragraph ends and the
next begins. You can also end a paragraph explicitly with \par, which has
the same effect as a blank line, though a blank line is more common and easier
to read in the source.
This is the first paragraph. It can span
multiple lines in the source. They will all
be joined into a single block of text.
This is the second paragraph, started after
a blank line above.
[ OUTPUT ]
Forced Line-break
Sometimes you want to break a line without starting a new paragraph (no extra
vertical spacing, no new indentation). Use \\ or the equivalent
\newline at the point where the line should break.
Roses are red, \\
Violets are blue.
[ OUTPUT ]
Note
Prefer starting a new paragraph (a blank line) over a forced line-break for
separating distinct ideas. Reserve \\/\newline for cases like
addresses, or tabular-style layouts where a true new paragraph isn’t
appropriate.
Emphasized Text
Note
In the following we will apply colors to a text passage, for this the package xcolor is needed. Import the package xcolor (\usepackage{xcolor}).
One morning, when \textbf{Gregor Samsa} woke from troubled dreams, he found himself
transformed in his bed into a \emph{horrible vermin}. He lay on his \textsc{armour-like}
back, and if he lifted his head a little he could see his \textcolor{orange}{brown belly},
slightly domed and divided by arches into stiff sections.
[ OUTPUT ]
Center
\begin{center}
One morning, when Gregor Samsa woke from troubled dreams, he found himself
transformed in his bed into a horrible vermin. He lay on his armour-like back,
and if he lifted his head a little he could see his brown belly, slightly domed
and divided by arches into stiff sections. The bedding was hardly able to cover
it and seemed ready to slide off any moment. His many legs, pitifully thin
compared with the size of the rest of him, waved about helplessly as he looked.
"What's happened to me?" he thought.
\end{center}
[ OUTPUT ]
Quote
Add the package \usepackage[babel,german=quotes]{csquotes} to the document preamble.
His many legs, pitifully thin compared with the size of the rest of him, waved
about helplessly as he looked. \enquote{What's happened to me?} he thought.
[ OUTPUT ]
Footnote
\begin{quote}
It wasn't a dream. His room, a proper human room although a little too small,
lay peacefully between its four familiar walls. A collection of textile samples
lay spread out on the table - Samsa was a travelling salesman - and above it
there hung a picture that he had recently cut out of an illustrated magazine
and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat
and fur boa who sat upright, raising a heavy fur muff that covered the whole
of her lower arm towards the viewer. Gregor then turned to look out the window
at the dull weather.\footnote{https://en.wikipedia.org/wiki/Franz_Kafka}
\end{quote}
[ OUTPUT ]
Section Commands
Available in all LaTeX classes.
\section{Some Title}
\subsection{Some Sub Title}
\section*{Title Without TOC Entry}
\section[Short Title]{Very Long Title That Looks Strange in the Table of Content (TOC)}
Create Table of Contents
\tableofcontents
Formulas / Math Environment
An example for an inline equation.
Es gilt $a^2 + b^2 = c^2$.
[ OUTPUT ]
To print an equation in display mode use one of these delimiters: \[ \], \begin{displaymath} \end{displaymath} or \begin{equation} \end{equation}.
Note
equation* environment is provided by an external package, consult the amsmath article.
The well known Pythagorean theorem \(x^2 + y^2 = z^2\) was proved to be
invalid for other exponents. Meaning the next equation has no integer
solutions:
\[ x^n + y^n = z^n \]
[ OUTPUT ]
\begin{equation}
E = mc^2
\end{equation}
[ OUTPUT ]
For an alignment of multiple lines use the align environment.
\begin{align*}
\sqrt{x^4}&= x\cdot x\\
\lim_{n\to\infty}\frac 1{n^2}&= 0\\
\int_{-1}^2 x\, \mathrm{d}x &= \left[ \frac12 x^2 \right]_1^2
\end{align*}
[ OUTPUT ]
Lists
An enumerated list in LaTeX.
\begin{enumerate}
\item Some item
\item Another item
\begin{enumerate}
\item Sub item
\item Another sub item
\end{enumerate}
\end{enumerate}
[ OUTPUT ]
An item list or bullet list in LaTeX.
\begin{itemize}
\item First item
\item Second item
\item ...
\end{itemize}
[ OUTPUT ]
A list of definitions.
\begin{description}
\item[Some Key] Some description of the key...
\item[Text] Other text.
\item[Other Text] \hfill\\Other text on a new line.
\end{description}
[ OUTPUT ]
Tables
A simple table composed of three columns a head row and three ordinary rows.
\begin{tabular}{l|ll}
\textbf{year}&\textbf{D}&\textbf{F}\\\hline
1990 & 79 & 58\\
2000 & 82 & 61\\
2010 & 82 & 65
\end{tabular}
[ OUTPUT ]
A table in a float environment. This allows the TeX compiler to position the table where it fits best in the text. Furthermore, you can decorate the table by a caption and add a reference mark “label”.
\begin{table}
\centering
\begin{tabular}{l|ll}
\textbf{Year}&\textbf{D}&\textbf{F}\\\hline
1990 & 79 & 58\\
2000 & 82 & 61\\
2010 & 82 & 65
\end{tabular}
\caption{A table caption.}
\label{tab:someRefKey}
\end{table}
With the added label you can reference this table.
For a brief overview see~\autoref{tab:someRefKey}.
Graphics
Display a graphic in your document.
\includegraphics[width=6cm]{flower}
Supported formats for graphics are “JPEG”, “PNG” and “PDF”. If you have an “EPS” or an “SVG” graphic an additional package will be needed for on-the-fly conversion.
For on-the-fly conversion of an EPS into a PDF use the package and configuration given below. Put it in your document’s preamble.
\usepackage{epstopdf}
\epstopdfsetup{update} % only regenerate pdf files when eps file is newer
For on-the-fly conversion of an SVG into a PDF use the package and configuration given below. Put the configuration in your document’s preamble. Further information about the package is available in the documentation.
\usepackage{svg}
...
\includesvg{img/a}
A graphic in a float environment as described for the table environment.
\begin{figure}
\centering
\includegraphics[width=.8\textwidth]{flower}
\caption{Some really nice flower}
\label{fig:flower}
\end{figure}
With the added label you can reference this graphic.
There is this really nice flower in~\autoref{fig:flower}.
Theorems
If you include propositions, theorems and proofs in a document, use the particular math environment. The examples below show the syntax and give an example of how to use them.
1The proposition~\ref{lemma:easy} says.
2
3\begin{Lemma}
4\label{lemma:easy}
5\LaTeX\ is easy.
6\end{Lemma}
7
8\begin{proof}
9Refer to the LaTeX documentation to learn more.
10\end{proof}
11
12\begin{Lemma}
13 \label{lemma:control}
14 \LaTeX\ provides full control over a document.
15\end{Lemma}
16
17\begin{proof}
18 \emph{It saves time and is fun!}
19\end{proof}
20
21Based on given propositions we can derive the following main clause.
22
23\begin{Theorem}
24 \label{thm-love}
25 Everybody loves \LaTeX.
26\end{Theorem}
27
28\begin{proof}
29 Derived from \vref{lemma:easy}
30 and \vref{lemma:control}.
31\end{proof}
An Example Preamble
1\documentclass{scrartcl}
2
3% input encoding
4\usepackage[utf8]{inputenc}
5
6% new german spelling (main), secondary language french and english
7\usepackage[main=ngerman,french,english]{babel}
8
9% Document font property
10\usepackage[T1]{fontenc}
11\usepackage{lmodern}
12
13% KOMA-Script options
14\KOMAoptions{%
15 parskip=full,%
16 fontsize=10.5pt,%
17 DIV=calc}
18
19% color and images
20\usepackage{xcolor}
21\usepackage{graphicx}
22
23% quotes
24\usepackage[german=guillemets]{csquotes}
25
26% math
27\usepackage{amsmath}
28\usepackage{amssymb}
29
30% set special behaviour for hyperlinks in pdfs
31\usepackage[breaklinks=true]{hyperref}
32
33\includeonly{title}