Back

LaTeX basics

This is a collection of how to format simple things in LaTeX, mostly stuff that a novice may miss. It's to be used mostly as a simple guide or reference, especially when getting started.

Contents:

Basic formatting

Example:

\textit{italic}
\emph{emphasized}
\textbf{bold}
\texttt{monospace}
\underline{underlined}

Result

Note that emphasized and italic are often the same, but there can be differences.

Images

Use the following snippet to insert images

\begin{figure}
  \centering
  \includegraphics{path/to/figure.jpg}
  \caption{Description of the image}
  \label{fig:unique-name}
\end{figure}

Captions, labels, and references

Note the use of \caption and \label in the previous snippet. Remember this:

Labels can also be given to code listings, sections, subsections, tables, and so on.

Example:

\section{Lorem ipsum}\label{sec:lorem-ipsum}

As seen in Figure~\ref{fig:figure-x} and as described
in Section~\ref{sec:lorem-ipsum}.

Some rules on how to reference things

Citing sources

The rules are similar to the previous section:

If you have a bib file like this

@article{article-id,
  title = {Article title},
  author = {John Smith},
  year = {2013},
}

Then you can cite it like

\cite{article-id}

Quotes

LaTeX uses ` for opening quotes and ' for closing quotes. For example:

% For single quotes
`text here'

% For double quotes
``text here''

% Do NOT do
'wrong single quotes'
"wrong double quotes"

The result is:

Note: you can use 'text' and "text" inside listings, but remember to set upquotes. See later section.

Lists

Choose between numbered and non-numbered lists.

% Non-numbered lists
\begin{itemize}
  \item Item 1
  \item Item 2
  \item Item 3
\end{itemize}

% Numbered lists
\begin{enumerate}
  \item Item 1
  \item Item 2
  \item Item 3
\end{enumerate}

Do not use - or * to mark lists.

Accents

If the document is in UTF-8, use directly the normal accents, e.g., È, é and so on.

Otherwise use \ followed by the mark and the letter. Examples:

\`e \.e \~e
\'e \"e \=e

Result:

Paragraphs and newlines

Use a double newline to introduce a new paragraph. The new paragraph is often indented. Use \\ to force a newline inside a paragraph.

Note that a single newline behaves the same as a space.

This is the first paragraph.

This is the second paragraph, which also
has \\ a forced newline.

Unbreakable space

The character ~ is an unbreakable space. This means that a~b appears the same as a b, but the two won't break over different lines.

You have to use ~ in some cases to prevent ugly results, such as

Listings

Listings are used to show code. You have to use the listings package and the lstlisting environment.

A simple example, which also enables the correct quotes to use

\usepackage[T1]{fontenc}
\usepackage{listings}

\lstset{
  upquote=true, % fix quotes
  showstringspaces=false, % show ' ' for spaces in strings
}

\begin{document}
\begin{lstlisting}[language=Python]
print('Hello World')
\end{lstlisting}
\end{document}

Result:

You can use caption and label as parameters in the square brackets.

Math

To insert math you have to go into math mode. This can be done in various ways:

The environments are numbered, and their variants with * are not (equation*, align*).

Once in math mode, you can do a lot of things. Check here for a complete list of symbols available.

Special characters

Here how to insert some tricky characters.

CharacterCommand
~ (low)$\sim$ or \texttildelow
~ (high)\~{}$ or \textasciitilde
_\_ or \textunderscore
{\{
}\}
\\textbackslash