LaTeX Example Snippets

A LaTeX document consists of two parts the preamble and document body. The preamble starts always whit the command \documentclass[]{}. Between the square brackets “[]” follows a comma-separated list of optional arguments to configure the document class properties. The document class is given between the curly brackets “{}”. It is a so called expected 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 a single space between words is created by a single or multiple space.

  • For a space between words without a line break use the tilde “~” character.

  • For more than one space, escape spaces be 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

Todo

Describe paragraph \par and double newline

Forced Line-break

Todo

Describe newline by using \\ and \newline

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 ]

\begin{minipage}{175mm}
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.
\end{minipage}

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 ]

\begin{minipage}{175mm}
\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}
\end{minipage}

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 ]

\begin{minipage}{175mm}
 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.
\end{minipage}

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 ]

\begin{minipage}{175mm}
\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.$^1$\\
\rule{8mm}{1pt}\\
\footnotesize{1\,:\,https://en.wikipedia.org/wiki/Franz\_Kafka} 
\end{quote}
\end{minipage}

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

Document Title and Author

\title{Some Document Tile}
\author{Martin Muster}
\date{\today}
\maketitle

Formulas / Math Environment

An example for an inline equation.

Es gilt $a^2 + b^2 = c^2$.

[ OUTPUT ]

\begin{minipage}{175mm}
 Es gilt $a^2 + b^2 = c^2$.
\end{minipage}

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{minipage}{175mm}
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 \]
\end{minipage}

  \begin{equation}
   E = mc^2
  \end{equation}

[ OUTPUT ]

\begin{minipage}{175mm}
\begin{equation}
 E = mc^2
\end{equation}
\end{minipage}

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 ]

\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

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 ]

\begin{minipage}{175mm}
\begin{enumerate}
\item Some item 
\item Another item
  \begin{enumerate}
  \item Sub item 
  \item Another sub item
  \end{enumerate}
\end{enumerate}
\end{minipage}

An item list or bullet list in LaTeX.

\begin{itemize}
\item First item
\item Second item
\item ...
\end{itemize}

[ OUTPUT ]

\begin{minipage}{175mm}
\begin{itemize}
\item First item
\item Second item
\item ...
\end{itemize}
\end{minipage}

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 ]

\begin{minipage}{175mm}
\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}
\end{minipage}

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 ]

\begin{minipage}{175mm}
\begin{tabular}{l|ll}
\textbf{year}&\textbf{D}&\textbf{F}\\\hline
1990 & 79 & 58\\
2000 & 82 & 61\\
2010 & 82 & 65
\end{tabular}
\end{minipage}

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 a EPS into a PDF use the package and configuration given below. Put it in your documents preamble.

\usepackage{epstopdf}
\epstopdfsetup{update} % only regenerate pdf files when eps file is newer

For on-the-fly conversion of a SVG into a PDF use the package and configuration given below. Put the configuration in your documents 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 shows the syntax and gives an example of how to use.

 1The proposistion~\ref{lemma:easy} says.
 2
 3\begin{Lemma}
 4\label{lemma:easy}
 5\LaTeX\ is easy.
 6\end{Lemma}
 7
 8\begin{proof}
 9If you RTFM then you will learn it.
10\end{proof}
11
12\begin{Lemma}
13  \label{lemma:control}
14  \LaTeX\ gives 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}