LaTeX basics
06 Feb 2024This 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
- Images
- Captions, labels, and references
- Citing sources
- Quotes
- Lists
- Accents
- Paragraphs and newlines
- Unbreakable space
- Listings
- Math
- Special characters
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:
- The caption is the description that appears under (or above) an image.
- The label is an unique id that can be used to reference that image with
\ref
.
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
- Always specify what you are referencing (Figure, Section, Equation, etc.) and use a capital letter.
- Always use an unbreakable space before using
\ref
.
Citing sources
The rules are similar to the previous section:
- You use
\cite
and not\ref
. - Always use an unbreakable space before using
\cite
. - You do not need to specify that it is a citation.
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
- After an abbreviation, e.g.,
Prof.~John Smith
. - Before a citation
as highlighted in~\cite{cit-id}
. - Before a reference
as in Figure~\ref{fig:id}
.
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:
- Use
$ ... $
or\( ... \)
for inline math. - Use
$$ ... $$
or\[ ... \]
for display math (whole paragraph). - Use the
equation
environment for an equation. - Use the
align
environment to insert align points (&
) and multi-line math (with\\
).
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.
Character | Command |
---|---|
~ (low) | $\sim$ or \texttildelow |
~ (high) | \~{}$ or \textasciitilde |
_ | \_ or \textunderscore |
{ | \{ |
} | \} |
\ | \textbackslash |