LaTeX

Learning LaTeX is as easy as building a sandwich.

Caleb Scutt gave us an excellent tutorial on what LaTeX is all about and how to use it. It’s all in his excellent repo here that you can run easily in overleaf (if you don’t want to install it) or on your own machine (after installing MikTeX).

How to make a sandwich (and learn LaTeX at the same time)

I believe that learning LaTeX is as easy as building a sandwich. By the end of this Macrofun tutorial you will have designed a recipe for your favourite ‘wich using LaTeX and be able to use the program for writing other more boring things like manuscripts etc.


Firstly… what is LaTeX

LaTeX (pronounced “Lay-Tek”) is used as a typesetting tool for documents. From the outset, it may appear quite scary - it is very different to Microsoft Word/Google Docs, which are known as WYSIWYG (what you see is what you get) software. Instead, LaTeX operates using commands and written essentially like scripting, so for the R-versed of us it may come relatively naturally. The LaTeX ‘script’ is processed by a TeX engine, which uses the commands to control the typesetting process, rendering the document into a PDF exactly how you want it to appear.

I personally enjoy this way of writing, since I am not distracted by visual appearance and LaTeX takes care of the formatting. I am able to focus solely on the content of what I am writing and the programming-style nature of it is also much more stimulating for me, who usually struggles to sit in front of a Word document for longer than an hour. There are lots more advantages too, all will be revealed in due course…


Step 1: Setting Up the Document

So to begin, you want to use your chosen text editor and create a .tex file (I use VS Code and the LaTeX integration is super good with the plugin ‘LaTeX Workshop’, but you can also use Sublime or even RStudio). If you installed MiKTeX, it comes with TeXworks which is a TeX frontend which you can use to edit and preview LaTeX documents simultaneously. Overleaf is a cloud-based LaTeX editor that also allows you to preview your document.

Create a folder where you will keep your .tex document. In this folder you can also put the couple of files that I shared on the github repo.

A simple LaTeX document has the following structure. All commands are prefixed with a \. Just like the hash key is used for commenting out code in R, the % key can be used in the same way in LaTeX.

\documentclass[12pt]{article}
\begin{document}

\title{How to make a sandwich}
\author{Your name}
\date{\today}

\maketitle

% this is where you write your recipe.

\end{document}

As you can see, we have included commands to specify the class of the document (meaning overall style), the title, author and date. This is known as the preamble, which is essentially the setup of the document. Additional parameters for a command can be found in the square brackets (such as font size in this case).

To compile your document, follow these steps:

  • Step 1: Open your terminal

  • Step 2: Change your directory to the folder which contains the .tex file: cd path/to/.tex

  • Step 3: Run this command: pdflatex filename.tex


Step 2: Organising the ingredients

A crucial part of any recipe is the ingredients list. To make this clear for our sandwich-maker, we need a heading and bullet list. LaTeX automatically creates headings when you split your document into sections and they enable automatic numbering, cross-referencing, and table of contents generation. To create a bullet list in LaTeX, you can use the \begin{itemize} command.

\documentclass[12pt]{article}
\begin{document}

\title{How to make a BLT}
\author{Caleb}
\date{\today}

\maketitle
\tableofcontents

\section{Ingredients}
Here’s what you need to make an almighty \textbf{BLT}.
\begin{itemize}
    \item Bread
    \item Butter
    \item Bacon
    \item Lettuce
    \item Tomato
    \item Mayo
\end{itemize}

\end{document}

As you can see, we’ve also added a table of contents. LaTeX will automatically page-number your sections according to you specifying the sections with \section. To make bold text, you can use the \textbf{} command.


Step 3: Writing equations in LaTeX

LaTeX is also useful for writing mathematical expressions. Stats nerds might prefer to write their ingredients lists as an equation. This can be done inline, meaning within the chunk of text, or display, where the equation is on a separate line. In this case we will use the display mode (inline can be done using \begin{math} ... \end{math}).

\documentclass[12pt]{article}

\title{How to make a BLT}
\author{Caleb}
\date{\today}

\begin{document}

\maketitle
\tableofcontents

\section{Ingredients}
Here’s what you need to make an almighty \textbf{BLT}.
\begin{itemize}
    \item Bread
    \item Butter
    \item Bacon
    \item Lettuce
    \item Tomato
\end{itemize}

\section{A Simple Formula}

\begin{equation}
    S = B + \sum_{i=1}^{n} I_i 
\end{equation}

Where:
\begin{itemize}
    \item \( S \) is the BLT
    \item \( B \) is the bread
    \item \( I_i \) is the sum of your ingredients 
\end{itemize}


\end{document}

Step 4: Writing instructions and creating tables

A numerical form of list can be created using the command \begin{enumerate} \item... \end{enumerate}. This is perfect for writing the instructions for making that tasty sandwich. If you want to create a table, you can use the \begin{tabular} command, where {ccc} specifies columns and \hline creates horizontal borders. The \\ creates a new row, but is also used for line breaks in paragraphs or equations.


\documentclass[12pt]{article}

\title{How to make a BLT}
\author{Caleb}
\date{\today}

\begin{document}

\maketitle
\tableofcontents

\section{Ingredients}
Here’s what you need to make an almighty \textbf{BLT}.
\begin{itemize}
    \item Bread
    \item Butter
    \item Bacon
    \item Lettuce
    \item Tomato
\end{itemize}

\section{A Simple Formula}

\begin{equation}
    S = B + \sum_{i=1}^{n} I_i 
\end{equation}

Where:
\begin{itemize}
    \item \( S \) is the BLT
    \item \( B \) is the bread
    \item \( I_i \) is the sum of your ingredients 
\end{itemize}

\section{Instructions}
\begin{enumerate}
    \item Take two slices of bread
    \item Spread butter on each slice
    \item Lay your lettuce, tomato and bacon on one slice of bread
    \item Dollop a generous amount of mayo
    \item Season with pepper
    \item Top sandwich with the other slice of bread.
    \item Nom nom nom
\end{enumerate}

\section{Tabular BLT}

\begin{center}
    \begin{tabular}{ccc} 
     \hline
      & bread &  \\ 
     \hline
     lettuce & bacon & tomato \\ 
     \hline
     & bread &  \\ 
     \hline
    \end{tabular}
\end{center}


\end{document}


Step 5: Visualising your sandwich

Now that we’ve made our sandwich, we want to be able to look at it! To add an image in LaTeX, there are a couple of steps. Firstly, we need to use a package called graphicx. This should come pre-installed with your distribution. To use a package in your document, you command \usepackage{package name}. This goes in the preamble. To add an image (you can use the BLT image I shared, or a different sandwich image), ensure it is located in the same directory as your .tex file. You use the command \includegraphics[width=0.5\textwidth]{}. The [] can again be used for additional parameters, such as the dimensions of the image, using height, width and scale. No more fussing around with wrap text, in this case I used \begin{center} to position the image in the middle of the page.


\documentclass[12pt]{article}
\usepackage{graphicx}

\title{How to make a BLT}
\author{Caleb}
\date{\today}

\begin{document}

\maketitle
\tableofcontents

\section{Ingredients}
Here’s what you need to make an almighty \textbf{BLT}.
\begin{itemize}
    \item Bread
    \item Butter
    \item Bacon
    \item Lettuce
    \item Tomato
\end{itemize}

\section{A Simple Formula}

\begin{equation}
    S = B + \sum_{i=1}^{n} I_i 
\end{equation}

Where:
\begin{itemize}
    \item \( S \) is the BLT
    \item \( B \) is the bread
    \item \( I_i \) is the sum of your ingredients 
\end{itemize}

\section{Instructions}
\begin{enumerate}
    \item Take two slices of bread
    \item Spread butter on each slice
    \item Lay your lettuce, tomato and bacon on one slice of bread
    \item Dollop a generous amount of mayo
    \item Season with pepper
    \item Top sandwich with the other slice of bread.
    \item Nom nom nom
\end{enumerate}

\section{Tabular BLT}

\begin{center}
    \begin{tabular}{ccc} 
     \hline
      & bread &  \\ 
     \hline
     lettuce & bacon & tomato \\ 
     \hline
     & bread &  \\ 
     \hline
    \end{tabular}
\end{center}

\section{Final product}
Your BLT should look something like this:

\begin{figure}[h]
    \begin{center}
    \includegraphics[width=0.5\textwidth]{blt.jpg} % Replace with your image file
    \end{center}
\end{figure}


\end{document}


Citing your BLT

Perhaps more useful for us, LaTeX allows for easy referencing. This includes referencing figures as well as citations. This avoids the need for manually changing such things. For example, if we want to reference a figure, it is super easy. Firstly, we treat the image as a figure using \begin{figure}. Then we can assign it a name using \label{fig:blt}. If we want to reference this figure in later text, we use \ref{fig:blt}. You can use the package hyperref which makes references clickable, which is super useful for accessing figures easily when reading long documents.

For citing references, there are a few steps. Firstly, you need to create a references.bib file, which will become your bibliography. LaTeX is able to read BibTeX. This file format basically stores all the meta-data for your references, which LaTeX can extract and create references from in any style you choose. It is super easy to access BibTeX for papers through google scholar by clicking on the cite link on the article of choice and clicking BibTeX. This will give you the plain text BibTeX file which can be copy and pasted into your .bib file. This file should be placed in the same directory as your .tex file.

The natbib package (included in most distributions) is widely used for organising references. The commands \cite{} and \citep{} can be used for in-text citations of the references included in your bibliography. To add a reference list, you can add \bibliographystyle{} and \bibliography{references.bib} at the bottom of your script before \end{document}.

In the BLT recipe, I’ve added a short background to the history of the BLT using a fake reference, as well as adding a figure reference to the BLT image.


\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{natbib}
\usepackage{hyperref}


\title{How to make a BLT}
\author{Caleb}
\date{\today}

\begin{document}

\maketitle
\tableofcontents

\section{A history of BLT}
The BLT originated in the US in the early 1900s \citep{blt2023}.

\section{Ingredients}
Here’s what you need to make an almighty \textbf{BLT}.
\begin{itemize}
    \item Bread
    \item Butter
    \item Bacon
    \item Lettuce
    \item Tomato
\end{itemize}

\section{A Simple Formula}

\begin{equation}
    S = B + \sum_{i=1}^{n} I_i 
\end{equation}

Where:
\begin{itemize}
    \item \( S \) is the BLT
    \item \( B \) is the bread
    \item \( I_i \) is the sum of your ingredients 
\end{itemize}

\section{Instructions}
\begin{enumerate}
    \item Take two slices of bread
    \item Spread butter on each slice
    \item Lay your lettuce, tomato and bacon on one slice of bread
    \item Dollop a generous amount of mayo
    \item Season with pepper
    \item Top sandwich with the other slice of bread.
    \item Nom nom nom
\end{enumerate}

\section{Tabular BLT}

\begin{center}
    \begin{tabular}{ccc} 
     \hline
      & bread &  \\ 
     \hline
     lettuce & bacon & tomato \\ 
     \hline
     & bread &  \\ 
     \hline
    \end{tabular}
\end{center}


\section{Final product}
Your BLT should look something like this \ref{fig:blt}:

\begin{figure}[h]
\begin{center}
\includegraphics[width=0.5\textwidth]{blt.jpg} 
\label{fig:blt}
\end{center}
\end{figure}

\bibliographystyle{plain}
\bibliography{references.bib}


\end{document}

N.B. when compiling now, you must run bibtex filename ( without the .tex) after pdflatex filename.tex. Then run pdflatex filename.tex once more to compile the pdf.

That’s all. You’ve now made your first LaTeX document!

SESSION
workshop writing latex