LaTeX Bootcamp

Abstract

During my first year as a computer scientist at UCL I found myself using $\LaTeX$ quite a bit, and I have to be honest here - for someone with absolutely no prior knowledge in that area it wasn't an easy road and the learning curve was quite steep. Naturally, after some practice you begin to understand everything better but in case of $\LaTeX$, you'll be confused as hell reading through the first couple of tutorials (and probably for several more tutorials after that). The aim of this guide is to help you understand what it actually is and where is the best place to start learning it.

Introduction

I personally usually skip the introduction part and jump straight to the interesting stuff but doing so in this case would completely defeat the purpose of reading this guide. That said, keep in mind this guide was meant to be super basic so don't be surprised if you see stuff you already know.

So what the hell is $\LaTeX$?

$\LaTeX$, pronounced lah-tekh, lah-tek or lay-tek, is a document preparation system. The core idea to understand here is that in $\LaTeX$ editors you don't get fancy formatted text as you type, but instead, you type in plain text first and only after compilation do you get the formatted result. The best part is that you can use special markup to customise the formatting and the overall look and feel of your document however you please. For example, if you were to type then compile \sum_{i=1}^{n} \frac{2i + 5}{2^{i}} you would get something like this: $\sum_{i=1}^{n} \frac{2i + 5}{2^{i}}$. Amazing, isn't it?

Why would I ever want to use it?

$\LaTeX$ is arguably the best tool out there when it comes to producing any kind of scientific documentation and many other things, such as journal articles or books. Personally, I love it for how well-structured the final document is and how consistent markup can ensure the professional and extremely neat look of your documents. Moreover, I like how easy it is to load additional packages, such as those for pseudocode formatting or simple UML diagrams. As a quick example, consider this piece of coursework I had to write not so long ago:

COMP104P Algorithm Complexity Coursework

I won't lie, the markup is much more complicated than that in your average word processor, but this complexity really pays off later when you get a beautifully formatted compiled result.

How does it work?

As I said above, $\LaTeX$ markup has to be compiled before you can see the pretty formatted result. Which means, you need some kind of software that would do all of the compiling for you. This is where I can't really give you much advice because I've only used such software on Windows up until now, so I'd suggest you do a quick Google search. Out of the things I've tried so far I would recommend Overleaf, an online collaborative $\LaTeX$ editor with real time previews, as well as TeXstudio for Windows for pretty much the same reason, minus the collaborative writing feature.

The usual "development" pipeline with $\LaTeX$ is pretty straight-forward: You type up your document, format it using the appropriate markup, compile it, check out the result, fix any formatting issues and compile it again. I personally prefer compiling a bit more often than that to admire the progress I've made, but I guess that excitement will fade over time. Either way, the most efficient technique of writing is not the main focus of this guide.

Your first document

From here onwards I will assume you have installed a decent editor and you already know how to compile the document in the editor of your choice. Well, let's begin with a super simple $\LaTeX$ document. If you already have some other markup in your editor, remove it. Otherwise, try typing in the code you see below and then compiling. If nothing went wrong in the process, you should get the output similar to the one on the screenshot.

\documentclass{article}
\begin{document}

\section{A Section}
Some dummy text for a paragraph.

\end{document}

Your first LaTeX document

Breakdown

The first line, \documentclass{...} is one of the most important parts of your document. It tells your editor what kind of document you are writing and allows you to specify various options, such as the font size or whether you want a title page to be present or not. It is recommended to insert this tag in the very beginning of your document, and the format is \documentclass[options]{class} (note that square brackets denote optional parameters while curly brackets are compulsory). For the sake of this guide I'm going to stick to a simple article class, but if you'd want to you could customise the document class heavily and end up with something like \documentclass[twoside, openright, 11pt]{report} (read this for more information).

Lines 2 and 7, containing \begin{document} and \end{document} respectively, define the boundaries of your document. All of the content you want to be present in the compiled result goes between these 2 tags. In our case we only have one section title and a single sentence. Note how plain text doesn't require any formatting, while something not as ordinary, say, heading, needs to be wrapped in a special tag.

Basic content formatting

The formatting options available to you differ depending on the type of document class you've chosen so make sure to look up additional formatting tips for the document you are writing. In the examples below I will omit some tags necessary to compile the document (described above) so don't forget to add them before compiling.

Chapters, sections and subsections

To present your document in a neat, well-structured way you will definitely be using a lot of sections and subsections. Consider the example below.

\section{Section}
Pellentesque laoreet, ex ut dapibus volutpat, leo nisl sagittis ex, non hendrerit.

\subsection{Subsection}
Morbi vitae ipsum eu leo porta ultrices sed nec libero.

\subsubsection{Subsubsection}
Aliquam pellentesque fermentum felis id luctus.  

LaTeX sections

Note how $\LaTeX$ automatically enumerates the section headings for you. You can insert any amount of sections, subsections or subsubsections wherever you want and during compilation time your editor will automatically resolve any changes in the structure of the document. An amazing outcome of using sections in document is that you can easily generate a table of contents by simply inserting the \tableofcontents tag anywhere in your document:

Table of contents

Enumeration, bullet points and lists

Making lists is pretty straightforward. You have to use the familiar \begin{...} and \end{...} and place some \item elements in between. Replace ... with the list type, which can either be enumerate, itemize or description.

\section{Enumerated list}

\begin{enumerate}
\item Item 1
\item Item 2
\end{enumerate}

\section{Bullet point list}

\begin{itemize}
\item Item 1
\item Item 2
\end{itemize}

\section{Descriptive list}

\begin{description}
\item [Item 1] Description 1
\item [Item 2] Description 2
\end{description}

Enumerations

Nesting lists is as easy as placing on list inside another, as shown below. Note that the indentation is there purely for decorative purposes and is not actually required for your editor to render the list correctly.

\begin{enumerate}
    \item Item 1
    \item Item 2
    \begin{itemize}
        \item Item 1
        \item Item 2
        \begin{enumerate}
            \item Item 1
            \item Item 2
        \end{enumerate}
    \end{itemize}
\end{enumerate}

Nested lists

Page- and line-breaks

So, line-breaks. Line-breaks are pretty self-explanatory, you insert them when you want to explicitly your editor you want to jump to a new line. You can use \\ or \newline which have the same effect. Note how in the example below, there is no explicit line break between the 2 paragraphs, while there are two between paragraphs 2 and 3 and one between 3 and 4. I omitted the actual paragraph text to make the example clearer.

Paragraph 1 text . . .

Paragraph 2 text . . .  
\\
\\
Paragraph 3 text . . .  
\\
Paragraph 4 text . . .  

LaTeX linebreaks

Page-breaks push all of the content below them to a new page. Simply insert \pagebreak wherever you need it and you will achieve the effect similar to the one shown below. There I inserted a \pagebreak right before the second section.

LaTeX pagebreaks

Labels and references

Referencing various parts of your document becomes super easy with $\LaTeX$. The basic concept is that if you want to reference something, you label it using the \label{label-name} tag, and then reference it elsewhere using \ref{label-name}. Additionally, you can reference the number of the page the label is on using \pageref{label-name}, as shown in the example below.

\section{A Section}
For more information, refer to section \ref{subsection-2} on page \pageref{subsection-2}.

\section{Another Section}
\subsection{A subsection}
\subsection{Another subsection}\label{subsection-2}

References

Packages and comments

First of all, you can add text that will not be compiled by your editor using the % symbol. Simply insert it in the beginning of the line you want the editor to ignore and add whatever comments you want to the text. Now, moving on to packages - $\LaTeX$ relies heavily on additional packages which you can use if you need some additional functionality, such as some weird mathematical symbols or pseudocode highlighting, which makes $\LaTeX$ absolutely amazing. To load a package during compilation, you have to insert \usepackage{package-name}[optional-settings] right after the \documentclass{...} tag. Check out the example below for both comment and package usage.

\documentclass{article}

% Package for coloured text
\usepackage{color}

% Package for inserting graphics
\usepackage{graphicx}

\begin{document}

% Our content goes here

\end{document}

Mathematical expressions

This is a huge topic and there is just too much to mention for this guide to give you a good idea of how to compose mathematical equations in $\LaTeX$. If you're looking for some advanced tips, consider looking through this page. What I will do is give you several basic tips which you can find below.

First of all, tell your editor to use the amsmath package by inserting \usepackage{amsmath} in the beginning of the document. Now you can insert inline expressions using \( ... \) and block expressions using \[ ... \], as seen in the example. As I said, there is just too much different tags to cover so I suggest you read the link in the above paragraph to find out more.

\documentclass{article}
\usepackage{amsmath}
\begin{document}

Some text containing an inline \( \forall x \in X, \exists y \leq \epsilon \) expression.

\[ \sqrt[n]{1+x+x^2+x^3+\ldots}\quad or \quad\int_0^\infty \mathrm{e}^{-x}\,\mathrm{d}x \]

\end{document}

Mathematical expressions in LaTeX

Code and pseudocode

While this is something you're very unlikely to use (unless you study computer science) I feel the need to elaborate a bit more on this topic, because I study computer science. First of all, I must say that including code and/or pseudocode in your documents is a real pain in the ass and so far I wasn't able to do it in a way I'd like it.

I won't include any examples here to avoid confusing you even further, but I would suggest you start your search here for pseudocode and here for actual code.

Conclusion

Unfortunately this article only focuses on the very core principles behind $\LaTeX$ and I wasn't able to cover such major topics as bibliography or figures, but it must be mentioned that the very purpose of this guide was to give you a gentle introduction as opposed to being a complete $\LaTeX$ reference. If you have any suggestions or complaints feel free to comment below.

If you found this post useful, feel free to like and share:

Comments