LaTeX

How to write your documents in LaTeX

LaTeX is a markup language, it's mostly used in technical and scientific documentation. However LaTeX is not hard to learn and can be used after just a few minutes. This way your documents can reach professional levels without having to work hard.


First, let's compare it to Word. Word is a WYSIWYG (What You See Is What You Get) editor, meaning that how you edit the file is also the end result. In LaTeX you usually compile .tex files, these are written using command, arguments and the like to give the user full control over what goes where. We all know the joke that if you move an image in Word by a few millimeters that the whole document get's ruined, well in LaTeX this won't be the case since its location is properly defined. We will go further into detail later on in the article. Now back to the comparison, word is more friendly to non-technical people since it uses a UI with buttons for all functions. LaTeX uses its commands with arguments to define things.

Topics

Setup

To start using LaTeX you need an editor and compiler, the editor is where you edit the document and the compiler will use this edited file to make it into the document type desired. For most applications a PDF is wanted so I will only talk about this file type, do keep in mind there are other file types LaTeX can handle, like HTML.


First we want to choose an editor and compiler, for groups I suggest Overleaf. Overleaf is an online LaTeX platform where you can edit and compile your group's documents. You can also use Overleaf for personal use but I wouldn't suggest it, offline editors and compilers are far quicker and don't have any limits on file size. For Windows and other operating systems I suggest MiKTeX for the compiler and TeXMaker as the editor, together they are a good LaTeX solution.


For Overleaf, make an account and start a project, you can start typing a .tex file by making one and compiling it. For TeXMaker you need to install both TeXMaker and MiKTeX to be able to use it. Then type your document and go to the two right facing arrows in the top. There are two selection menus, the left one should be PDFLaTeX and the right one View PDF. To compile, click on the right arrow and to view the result, click on the left arrow.

If you want to include the bibliography package in your compilation, switch the left menu to Quick Build. Then check under Options > Configure TeXMaker > Quickbuild that the setting is set to: PDFLaTeX + Bib(La)TeX + PDFLaTeX (x2) + View PDF. And under Options > Configure TeXMaker > Commands, make sure the option Bib(la)tex is set to biber.exe %.


To start a LaTeX document we start with its documentclass, to tell LaTeX this we use the \documentclass[]{} command. An example is given below.

\documentclass[a4paper,11pt]{article}

This command will tell LaTeX that the type is an article, the size of the letters is 12pt and the page size is an A4. There are many more document types, as listed below. Just remember that in the square brackets the options are entered whilst in the curly brackets the actual document class is entered.


  • article: For articles, reports and other general use.
  • beamer: For presentations, like MS PowerPoint but better.
  • book: For books... I think you get the idea.
  • IEEEtran: Used for some research papers.
  • letter: For writing letters.
  • memoir: Like book but more flexibility.
  • proc: Proceedings, based on article.
  • report: For big reports with multiple chapters.
  • slides: For slides.

Now for the options, they're seperated with commas and are put in the square brackets. The options are:


  • 11pt: This can be any number as it represents the font size.
  • a4paper, letterpaper, a5paper: These state what page size you would like to use.
  • draft: LaTeX will skip over some details so you can quickly spot mistakes. Images will be skipped.
  • fleqn: Formulas are aligned on the left side of the document instead of centered.
  • landscape: Default is portrait.
  • leqno: Forumula lettering on the left instead of to right of the formulas.
  • openright, openany: Used for reports and books for when you want to tell it where to put the beginning of a chapter. openright means it will start a chapter only on right hand pages and openany opens them on the next page.
  • titlepage, notitlepage: If you want a titlepage or not.
  • twocolumn: If you want to columns of text on a page this is the option for you.
  • twoside, oneside: single or double paged documents, article and report are default single paged, while book is double sided by default.

Now we have that done we want to start typing our document. We start by telling LaTeX where it begins and where it ends:

\documentclass[a4paper,11pt]{article}

\begin{document}

\end{document}

In between the begin and end commands you type all of your document. You can start by using sections to divide up your document and give it order, or if you want to let LaTeX make you a basic titlepage, we can do that too.

Titlepage & Abstract

To make a titlepage we start with telling LaTeX what our document is about and more. We can tell LaTeX the author, title and date. This is done with the following commands:

\title{}
\author{}
\date{}

To print out the basic titlepage you use the \maketitle command:

\maketitle

If you want a dedicated titlepage you can do that too! We first tell LaTeX where it begins and ends using the begin and end command like so:

\begin{titlepage}

\maketitle

\end{titlepage}

For abstracts it is done in the same way, with a begin and end command, in here you type your abstract of your document and it will appear in front of the main body of text.

\begin{abstract}

"Abstract Text"

\end{abstract}

Document Management

When working in groups or with large documents, a way of organizing it into more manageable pieces is a must. Imagine working in a 50+ page document with ten or more people, it would be a nightmare to manage. With LaTeX you can divide the document up in subdocuments where we can work in. These subdocuments are then used when compiling the full document and looks like it was a single document from the start. This way you can more easily switch or remove a chapter when needed without screwing up the entire document, when done correctly all the references and numberings are updated automatically.

To divide up the documents we need a package called subfiles. To use it we first need to enable the package in our main document, this is the file where we call all subdocuments, this is also the file you need to compile in the end.

\usepackage{subfiles}

After this you are good to go to divide up your documents into neatly organized folders. I would suggest having at least a chapters and appendices folder. To call these subdocuments in your main file we need to call them. We use the \subfile{} command for this with the filepath towards the subdocument.

\subfile{./Chapters/Chapter1.tex}		% Location of file, beware of using / or \, this depends on your system.

Then to be able to compile the subfile on its own you need to write three lines of code. This enables you to compile your files faster to be able to check your work.

\documentclass[main.tex]{subfiles}		% Location of the main file, beware of using / or \, this depends on your system.
\begin{document}

% Text

\end{document}

Now you don't have to use the subfiles package, you can use the default \include{} and \input{} in your main file like you use \subfile{}. This won't give you the option to compile the subdocments separately. Play around and see what you like.

Sections

To print your table of contents we use the \tableofcontents command:

\tableofcontents

Sections, and paragraphs can be defined with commands, here are a few basic ones used the most in articles:

\part{}			% Only used in the book and report document type.
\chapter{}		% Only used in the book and report document type.
\section{}
\subsection{}
\subsubsection{}
\paragraph{}
\subparagraph{}

Using these commands is quite simple, first the command itself needs to be typed and within the curly brackets the name of the section should be given. The styling is done automatically and it can be automatically added to the table of contents (ToC). If the section is not meant for the ToC, then a * can be added between the command and the argument, an example for the section command:

\section*{}

If you only want to remove the numbering of the section without it being removed from the ToC, a * can still be added, but to add it back to the ToC a command needs to be issued. This command only adds the section to the ToC but will not add the automatic numbering.

\section*{} \addcontentsline{toc}{section}{Title of the section}

Customize sections

The look and function of the section commands can be altered using a package, the package is called titlesec and can be turned on using the usepackage command:

\usepackage{titlesec}

I won't go into detail in how this package is used but in a youtube video where Luke Smith made a detailed video about how to make your own resume. He explains the titlesec package and others.

Formatting

General Formatting

Formatting is used in documents to give the reader a clear view of the text. Text being too close together is seen as a word mess and won't attract any readers. We can use bold, italic or underlined text for highlighting our text. There is also an emphasize command, this works by default like italic but when used in a bold, italic or underlined piece of text, it will act reversed. The following command give you these options in your text:

\textbf{}		% Put text that needs to be bold in between the curly brackets.
\textit{}		% Put text that needs to be italic in between the curly brackets.
\underline{}	% Put text that needs to be underlined in between the curly brackets.
\emph{}			% Put text that needs to be emphasized in between the curly brackets.


To get blank lines in between paragraphs we use the \par or a blank line itself in the code to end the paragraph, a \\ also "works" to end paragraphs, but it is not the right way of doing things as it forcefully ends the paragraph. Now the vertical space itself and how the beginning of the next paragraph is done by the parskip package. We first have to load it into our LaTeX file. More on parskip can be found here.

\usepackage{parskip}									% Loading the parskip package
\usepackage[indent]{parskip}							% Loading parskip with indents as the default
\usepackage[indent,skip=2\baselineskip]{parskip}		% Loading parskip with indents and 2 blank lines in between paragraphs


To begin a new paragraph we use \noindent or use the parskip package without the indent feature. I will explain how to begin paragraphs with the indents turned on in the parskip package ( \usepackage[indent]{parskip} ). Below you see how to start a new paragraph with no indent. If you like having an indent you can use \noindent or remove the indent from the package settings like so: \usepackage[]{parskip}.

End of paragraph text.

\noindent Beginning of new paragraph text.

If you have related paragraphs where a blank line is not desired and having an indent at the beginning of the paragraph is needed, then the following can be done.

\newcommand{\noskip}{\vspace{-\parskip}}				% This is placed in your preamble (after \documentclass[]{} and before \begin{document})

Then with the newly made \noskip command we can remove the vertical space between paragraphs whenever needed while having an indent at the beginning of the new paragraph.

End of paragraph text.

\noskip Beginning of new paragraph text.

Lists

Sometimes you want a simple way of summing things up, so we itemize them. This is done in LaTeX with a begin and end itemize and \item as the item.

\begin{itemize}
	\item Potatoes
	\item Bananas
	\item Apples
\end{itemize}

If you want numbers instead of dots, we use enumerate instead of itemize.

\begin{enumerate}
	\item Golden Delicious
	\item Gala
	\item Fuji
\end{enumerate}

You can combine them by just beginning an itemize or enumerate within an itemize or enumeration. You can go even deeper by using more \begin and \end in the original itemize or enumeration. An example is:

\begin{itemize}
	\item Potatoes
	\item Bananas
	\item Apples
		\begin{enumerate}
			\item Golden Delicious
			\item Gala
			\item Fuji
		\end{enumerate}
\end{itemize}

Special Characters

See this page for a list of commands that let you use special characters more easily.


Special Characters

Graphics

Images and other graphics are almost always wanted in a document, they can break up text, say more than a thousand words or just look nice. Graphics are added with the graphicx package.

\usepackage{graphicx}

first you need to tell LaTeX where you stored your graphics, this can easily be done with the graphicspath command. Between the curly brackets is ./Images/, the dot means the root of the document you're currently in and Images is the folder you store your graphics. You can add more directories by adding more brackets within the first brackets, as seen in the second line.

\graphicspath{ {./Images/} }
\graphicspath{ {./Images1/} {./Images2/} }

The easiest way to include graphics is by using the includegraphics command, it is the most basic way of inserting graphics.

\includegraphics[]{}

within the curly brackets the file name is placed, within the square brackets transformations applied to the graphic must be issued. this can be the width, height, scale, rotation and more. They can be combined by putting a comma in between. Examples are given below.

\includegraphics[width=5cm]{example.png}
\includegraphics[height=5cm]{example.png}
\includegraphics[scale=2]{example.png}
\includegraphics[angle=90]{example.png}
\includegraphics[width=0.75\textwidth]{example.png}

\includegraphics[width=0.75\textwidth, angle=90]{example.png}

Environments

Now you can place images, however we want to add labels, captions, location and more to the image, it needs to be a new "environment", this means making it a piece that can be altered separately from the rest of the document. This is done with the \begin{} to indicate the beginning of a new environment and \end{} to end it.


\centering is used to put the figure in the middle of the page, however if it's left out, it will align to the left of the page.


\label{} is used to be able to reference the figure in the text. A "nickname" can be added, in the text the command \ref{} with the same nickname will automatically reference the image in the text, making it easy to move the image around without the numbering being wrong in the text, more detail is given in the referencing section.


\caption{} gives you the ability to enter a caption below the figure, just type the text you want within the curly brackets, if you desire to put the caption above the figure, put the \caption{} command above the \includegraphics[]{} command instead.

\begin{figure}[H]
    \centering
    \includegraphics[width=0.75\textwidth]{Figure1.png}
    \caption{Example Figure 1}
    \label{fig:Figure1}
\end{figure}

after \begin{} square brackets can be added with a parameter that decide the placement of the environment:

h				roughly the same spot as where you put the figure in the editor
t				top of the page
b				bottom of the page
p				special page for the figure
H				Precisely at the same spot as where you put the figure in the editor
!				Can be used to override LaTeX

Units

Multiple units can be used to size items, examples are given:

cm				centimeters
mm				millimeters
ex				height of the letter x
em				width of the letter m
mu				1/18th of em
pt				point: 0.3515 milimeters

\baselineskip	Vertical space between lines
\columnsep		Column distance
\columnwidth	Width of a column
\linewidth		Width of a line
\paperwidth		Width of the document
\paperheight	Height of the document
\parindent		The indentation of a paragraph
\parskip		Vertical space between paragraphs
\textheight		Height of the text
\textwidth		Width of the text

Bibliography

One of the best features of LaTeX is the bibliography system. It has a management system which manages your .bib file, this file contains your projects sources or your own personal sources. Because of the management system you could in theory keep the same bibliography file for your entire life as it only uses the sources you used in the document.

There are multiple packages and ways to set this up but at this moment it is one of the most up to date ways. We first need a package called BibLaTeX which will enable the package natbib to give the user options more to work with. Second of all we will use the management package called biber with BibLaTeX, this way we can use more First we need to enable the package, the stuff between the square brackets are our settings of the package and need to be changed if one desires different settings.

\usepackage[backend=biber,style=apa,url=true,citestyle=authoryear,natbib=true,sorting=anyvt]{biblatex}

This will tell BibLaTeX to do the following from left to right:


  • backend=biber BibLaTeX will use the management package called biber.
  • style=apa The bibliography will use the APA style.
  • url=true Enable URL support.
  • citestyle=authoryear BibLaTeX will use the author and the year of publication in the citation.
  • natbib=true Enables the natbib package within BibLaTeX.
  • sorting=anyvt Sorts the bibliography in an alphabetical order, then by the author name, year of publication, volume and title.

Now that the package is enabled we can create some rules for the package.

First of all, we use this command to tell BibLaTeX to use more space between the bibliography items, this can make it clearer for the reader.

\setlength\bibitemsep{1.5\itemsep}

Sometimes I have to write my reports in Dutch, so I tell BibLaTeX the translations of the English words used in APA-style. You can replace it with your own language if needed, skip this step if you are writing the file in English.

\DeclareLanguageMapping{dutch}{dutch-apa}
\NewBibliographyString{retrieved}
\NewBibliographyString{from}
\DefineBibliographyStrings{dutch}{%
   nodate = {z.d.},
   retrieved = {geraadpleegd op},
   from = {van},
}

At last we want to break up URLs that are too wide for LaTeX's normal URL placing. Otherwise we might end up with URLs that are wider than the linewidth in the document. Just give these values to BibLaTex and it will work.

\setcounter{biburllcpenalty}{7000}
\setcounter{biburlucpenalty}{8000}

After this we want to tell BibLaTeX where we put our sources, this is done using the \addbibresource{} command with the path to the .bib file where you store your sources in.

\addbibresource{Bibliography.bib}

At the end you probably want to place your bibliography, this is done using the \printbibliography command. This command is accompanied by a command talked about in Sections, it is the \addcontentsline command for adding the bibliography to the table of contents.

\printbibliography \addcontentsline{toc}{section}{Bibliography}

Using BibLaTeX

Now you know how to set up BibLaTeX in your LaTeX documents. Now we will go into further detail in how to use them.

Bibliography File

In the bibliography file you put all your sources, it will be a list of entries. BibLaTeX will sort them and only show the used ones so it will take away a lot of the management work. I will give one example here, some of the other source choices can be found here.

Biblography Types
@online{PotatoesJohnDoe2000,							% Put a 'nickname' of the source here, it will be used to reference it.
    author      = {John Doe},							% For an organisation, put "" around the brackets.
    year        = {2000},								% Year of publication.
    title       = {Potatoes & Me},						% Title of the source.
    url         = {https://juststephen.com/LaTeX},		% URL (link) of the source.
    urldate     = {2000-01-31}							% The date of when the source was found, the format is Year-Month-Day!
}

Don't forget to save the file containing all your sources in a file with the .bib extension, then in the LaTeX document use \addbibresource{} to link towards it.

Citing

Citing in text is quite easy at this point, we only have to use one of the \cite{} commands together with our source-nickname in between the brackets. Here is a list with \cite{} commands.

\cite{} 			% Normal citation.
\citep{}			% Citation between parenthesis.
\citep*{}			% Citation between parenthesis, but with all authors.
\citet{}			% Citation in textual style.
\citet*{}			% Citation in textual style, but with all authors.
\citeauthor{}		% Only shows the author names.
\citeyear{} 		% Only shows the year.

Code Highlighting

To highlight your code in your LaTeX document beautifully we need a package called minted. This package can highlight multiple programming languages. To use it you need to install some software, this is explained in the documentation of the package, if you use Overleaf, skip the installation step.

\usepackage{minted}

Blocks of code can be displaced as such:

\begin{minted}{language}		% Replace 'language' with a supported programming language you used in your code block.

% Code

\end{minted}

To import source code files, you can use the \inputminted{}{} command like so:

\inputminted{language}{file}	% Replace 'language' with a supported programming language you used in your source code.
								% Replace file with the filepath to your code.

You can use the same commands to label and caption your code blocks as explained in the Graphics section. This is done with a \begin{listing}[] and \end{listing}, in between these two you put your \begin{minted}{} and \end{minted}.

\begin{listing}[H]
\begin{minted}{language}		% Replace 'language' with a supported programming language you used in your code block.

% Code

\end{minted}
\caption{}						% See the Graphics section.
\label{}						% See the Graphics section.
\end{listing}


Single lines of code in text can be displaced using the \mint{}|| command.

\mint{language}| Code |			% Replace 'language' with a supported programming language you used in your code block.
								% Replace 'Code' with your code.

Equations & Formulas

In scientific reports you need to be able to display equations and formulas. LaTeX has a seperate mode for these expressions, since TeX, the predecessor of LaTeX, was originally designed with mathmetical expressions in mind. Generally we have two types places where we want our expressions, in the body of text or seperate to highlight it. In a body of text we call it inline mode, while the seperate expression is called display mode.


To start an expression we need to tell LaTeX where it begins and ends, there are multiple ways to start and end the inline mode:

$ $
\( \)
\begin{math} \end{math}	

To start and end the display mode we can use three others:

\[ \]
\begin{equation} \end{equation}	 	% amsmath package needed, use: \usepackage{amsmath}
\begin{displaymath} \end{displaymath}

In between the begin and closing delimiters we type our expressions. For inline mode I prefer the simple $-signs while for display mode I like the package called amsmath. Amsmath gives more options for our equations. Amsmath is used with the equation environment. so to start it we use it within the \begin{} and \end{} commands.

\usepackage{amsmath}

Now we can start writing our expressions. To use superscript and subscript we use ^ and _. I will show it in inline mode, but this will work in the display mode as well. If you want more than one letter in the sub- or superscript, put it between curly brackets as shown below.

$m^2$		% An example for a superscript is using it for units.
$m_{banana}$	% An example here is the mass of a banana.

Now we also want to use Greek letters as variables, integrals, summations and much more in our equations and formulas. Most of these commands are compiled the following page for easy access: here!


LaTeX Equation & Greek Symbols

Tables

For tables in LaTeX I would suggest starting with an online tool with UI. Tables Generator should suffice, do read the output carefully since it sometimes needs you to add packages to make the tables work. Check for the \usepackage{} command.


Tables Generator