This lesson is in the early stages of development (Alpha version)

Introduction to LaTeX Typesetting

Introduction

Overview

Teaching: 10 min
Exercises: 5 min
Questions
  • What is LaTeX used for?

  • How can I create a new typeset document?

Objectives
  • How to create a new LaTeX project in Overleaf.

  • Become familiar with the Overleaf ecosystem.

  • Learn what the minimum requirements for a LaTeX document are.

  • Be able to identify an element’s role in a LaTeX document.

What is LaTeX

LaTeX is a typesetting language, useful for combining text with mathematical equations, figures, tables, and citations, among other things.

LaTeX is written like a mark-up language, meaning that formatting (including bold text, bullet points, and changes in font size) is indicated by the use of commands, special characters, or environments.

In order to produce the actual document, this mark-up text must be compiled. Errors in the mark-up can either be non-fatal, meaning the document will compile with some warnings; or fatal, meaning the document will fail to compile.

How do you say it?

Not like the rubbery substance - LAY-teks - it’s usually pronounced LAH-tekh or LAY-tekh.

Our first LaTeX document

We’ll start by creating a new, ‘blank’, file in Overleaf. You’ll notice that its idea of a blank file is not actually an empty one.

  1. Sign in to Overleaf.com.
  2. Go to New Project > and select “Blank Project.”
  3. Name your project “LaTeX workshop”.

Overleaf fills in a lot of things by default, which is one of the benefits to using it; it makes it very easy to get up-and-running in LaTeX.

There are three parts to the Overleaf window: the file navigator, the text editor, and the preview pane.

In the text editor for our ‘blank’ document, we can see all of the elements necessary to create a minimally-compilable document in LaTeX (and then some).

\documentclass{article}
\usepackage{graphicx} % Required for inserting images

\title{LaTeX Workshop}
\author{Your Name}
\date{August 2022}

\begin{document}

\maketitle

\section{Introduction}

\end{document}

Commands in LaTeX are distinguished from regular text through use of a backslash \. Some commands take options and/or arguments. Arguments are placed inside curly braces {}. Options go in square brackets [].

The \begin{} and \end{} commands delineate an environment. We’ll see more about environments in later episodes; for now, I’ll say that environments each have their own set of formatting rules and purpose.

Looking at our document

If we look at the different parts of this document, starting at the top, we see:

  1. The document class declaration. This document is an article.
  2. The preamble
    • A package import statement. The graphicx package is used to allow us to add images to our document, which the comment (beginning with the % symbol) tells us.
    • Some metadata. This includes the title and author.
  3. The document body
    • The document environment \begin{} statement
    • The \maketitle command
    • A section heading
    • The document environment \end{} statement

Some of these are absolutely necessary:

Without these, the document will not compile.

Let’s make this document a bit more interesting

Now we’re going to add in some dummy text to get a sense of what a more-complete document would look like.

We’ll do this using the lipsum package, which provides sample text blocks. We’ll need to add a new \usepackage{} command to the document preamble and the \lipsum command underneath our section title.

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{lipsum}

\title{Let's do Science!}
\author{Your Name}

\begin{document}

\maketitle

\section{Abstract}
\lipsum

\section{Introduction}
\lipsum

\end{document}

Once you’ve updated the main.tex file, select the “Recompile” button in Overleaf to update the preview of your file.

Challenge 1

Look at the following LaTeX code and decide if it will compile as written. If you say no, decide why not.

\documentclass{article}
\usepackage{graphicx} % Required for inserting images

\title{The Playboy of the Western World}
\author{JM Synge}

\begin{document}

\maketitle

\section{Introduction}

Answer

This code will not compile as written (or, if it does, it will throw some warnings). It is missing the \end{document} statement.

Challenge 2

Which of these lines should not go in the document preamble?

  1. \maketitle
  2. \title{R.U.R.}
  3. \usepackage{graphicx}

Answer

\maketitle does not belong in the preamble. \title{R.U.R.} does, because this command stores metadata about the file.

Challenge 3

Which of these lines is optional?

  1. \begin{document}
  2. \maketitle
  3. \documentclass{book}

Answer

\maketitle is optional.

Key Points

  • Typesetting is used in many domains to convey information in a more readable format.

  • Creating a new, minimal document requires a document declaration, a preamble, and a document body.

  • LaTeX documents consist of commands, environments, and regular text. Commands may take arguments and/or options.


Text Formatting

Overview

Teaching: 25 min
Exercises: 10 min
Questions
  • How can I format text within a LaTeX document?

  • How can I use different styles and classes of documents?

  • How can I style mathematical equations in LaTeX?

Objectives
  • Create headings, sections, and subsections.

  • Learn to bold, italicize, and format text.

  • Create lists.

  • Create different classes of documents and use different styles with them.

  • Create equations and use mathematical notation.

Basic typesetting

We are now going to introduce some commands you can use to format your text. Let’s continue working with the main.tex file from the previous lesson.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}

\title{Let's do Science!}
\author{Your Name}

\begin{document}

\maketitle

\section{Abstract}
\lipsum

\section{Introduction}
\lipsum

\end{document}

Previously, we inserted some random text using lipsum, but because that is generated via a command, we couldn’t format the text itself. Let’s remove the lipsum command from our document and start adding some formattable text instead.

You might notice that the Abstract is being enumerated (1, 2, 3…) along with the other “sections” of the article, which isn’t usually how articles are formatted. In this case, LaTeX provides an environment we can use for our Abstract. Delete the \section{Abstract} command, and create a new Abstract environment instead:

\begin{abstract}
	The scientific method is really cool. I mean, seriously, it is awesome!
\end{abstract}

Let’s take a look at some of the commands we can use to format text. We can format words or phrases using commands for bold, italics, underline, subscripts, superscripts and more by passing the text we want emphasized as arguments to the command:

\begin{abstract}
	The \textbf{scientific method} is really cool. I mean, seriously, it is \textit{awesome}! It is the 1\textsuperscript{st} method that you should use when you want to do science. Can you imagine what technology & engineering would look like without the scientific method?
\end{abstract}

Notice that the ampersand (&) disappears from the text after you recompile the document! That’s because a number of characters are reserved in LaTeX, and if we want to include them in our text we first have to “escape” them with a backslash (). Some of those special characters are:

# $ % ^ & _ { } ~ .

To display the ampersand after compiling our doc, we should add a backslash as an “escape character” before the ampersand:

\begin{abstract}
	The \textbf{scientific method} is really cool. I mean, seriously, it is \textit{awesome}! It is the 1\textsuperscript{st} method that you should use when you want to do science. Can you imagine what technology \& engineering would look like without the scientific method?
\end{abstract}

Lists

Let’s add some of the reasons we love the scientific method to our abstract using the {itemize} environment:

\begin{itemize}
	\item It makes it less likely that bridges will collapse.
	\item It helps us to build airplanes so we can fly.
\end{itemize}

If we wanted to change it from an unordered list to a numbered/ordered list we could use the {enumerate} environment instead:

\begin{enumerate}
   	\item It makes it less likely that bridges will collapse.
	\item It helps us to build airplanes so we can fly.
\end{enumerate}

Document classes and sections

Articles are not the only kinds of documents we can create in LaTeX. We could also assign document classes such as:

  • \documentclass{minimal}
  • \documentclass{report}
  • \documentclass{slides}
  • \documentclass{letter}
  • \documentclass{book}

Go ahead and change your document class and recompile to see how the format changes. Some commands, like \section, won’t work in every document class.

We can also customize the document class using options [] to add styles across the entire document:

\documentclass[12pt,letter]{article} % use 12 point font on 8.5 x 11 inch (letter) paper

The code above also includes a comment - all of the text after the percentage symbol - which will be ignored when the document is compiled.

Sections & subsections

We’ve seen how we can organize an article with \section commands. We can also add different levels of headings and sub-headings. Let’s outline some of the pillars of science that we want to touch on in our Introduction section:


\section{Introduction}
	
\subsection{Literature Review}

\subsection{Methodology}

\subsubsection{Math}

\subsection{Results}



Quote marks

Adding quotations to your text in a LaTeX document can be challenging. Copying quote marks from other documents is likely to create errors, or show up as weird characters in your text. To add left and right quote marks in LaTeX it’s best to add two grave accents (to the left of the “1” key) as your left quote mark and two single quotes (to the left of the “Return/Enter” key) as your right quote:

\section{Introduction}
A ``quote'' in a phrase works like this.
Using "double quotes" will look bad.
``To add a `quote' in a quote you should add the double grave and single quotes outside of a single grave and single quote mark.''

Add regular quotes to your document and compile to see what they look like. Now try to add the grave accents and single quotes to compare.

Math typesetting

One of the primary advantages to using LaTeX, as opposed to other typesetting software, is its ability to format mathematical equations. You can use dollar signs to set apart formulas in your text, or use the equation environment to create larger displays. In both of the equations below we’ll use the carat symbol to add powers/superscripts:

\subsection{Math}
Einstein discovered $e=mc^2$.

We can also set aside a formula from the text:
\begin{equation}
a^2 + b^2 = c^2
\end{equation}

Many of the reserved characters we noted above can be used in LaTeX within the math environments. So these symbols will display correctly when set apart using the dollar signs ($ $) or {equation} environment:

\begin{equation}
+ - = ! / ( ) [ ] < > | ' : *
\end{equation}

Some other commonly used characters and tips for equations:

It’s also quite common to use commands that take values as parameters to present equation symbols. For example \sqrt{} for displaying square roots, \sum{} for the sum symbol, and \int{} for the integral symbol. We can space out the equations a bit by adding line breaks using double backslashes (\\):

$\sqrt{4}$
\\
\\
$\sum{P(i,j)}$
\\
\\
$\int_0^\infty$

There are a lot of potential formatting choices to make when building your formulas. Wikipedia’s LaTeX/Mathematics page is a good quick reference, while The American Mathematical Society’s Short Math Guide for LaTeX goes into a little more detail.

Challenge 1

Using Wikipedia’s quick guide, how would you render the fraction 5/8?

Answer

\frac{5}{8}

Challenge 2

How would you format the square root of a over b? equation for square root of a over b

Answer

\sqrt{\frac{a}{b}}

Key Points

  • Sections are delineated by \section and automatically numbered. Subsections can be added with the \subsection command.

  • The commands \textbf{} and \textit{} can be used to bold and italicize text by taking in text as an argument.

  • Lists can be created using the {itemize} environment, and numbered lists can be created using the {enumerate} environment.

  • Document types are specified by the \documentclass{}, and can be customized using options added as arguments in [].

  • Math typesetting can be used in-line with the text by surrounding formulas with dollar signs, or separately using the {equation} environment.


Columns, Tables, and Templates

Overview

Teaching: 30 min
Exercises: 0 min
Questions
  • How can I add tabular data to a LaTeX document?

  • How can I organize my text into columns?

  • How can I use existing LaTeX templates to style my document in accordance with publisher or organization guidelines?

Objectives
  • Create and customize a table in LaTeX.

  • Re-organize our LaTeX document into multiple columns.

  • Learn where to find and use LaTeX templates.

Tables & tabular data

Manually marking up tables in a LaTeX document can be a time-consuming task. It’s often a better idea to begin by exporting your tabular data from another source and then copying/pasting the LaTeX code that those tools generate into your LaTeX document. You can generate LaTeX tables using tools like:

But let’s create a simple table in LaTeX manually to better understand the general structure. First, we want to use the tabular environment in LaTeX, which we need to customize by adding a parameter that includes how many columns to include. When we’re adding our data to the table each ampersand (&) is a cell separator and a double-backslash (\\), which we’ve seen before gives us a line-break, denotes the end of a row.

\section{Results}
\begin{tabular}{ c c c }
cell 1a & cell 1b & cell 1c \\
cell 2a & cell 2b & cell 2c
\end{tabular}

We can modify the display of this table a bit. Add borders to the table using pipes (|) in between your column parameters and horizontal lines with \hline rules between the row content.

\begin{tabular}{| c | c | c |}
\hline \hline
cell 1a & cell 1b & cell 1c \\
\hline
cell 2a & cell 2b & cell 2c \\
\hline \hline
\end{tabular}

We can also add labels and and captions using the \label and \caption commands, but for those to display correctly we will also want to add a floatable table environment using \begin{table}. The h! option for the table environment overrides LaTeX table defaults and places the table ‘here’ in the text of your document. We can also center the whole table on the page using the \centering command.

\begin{table}[h!]
\centering

\begin{tabular}{| c | c | c |}
\hline \hline
cell 1a & cell 1b & cell 1c \\
\hline
cell 2a & cell 2b & cell 2c \\
\hline \hline
\end{tabular}

\caption{This table is highly scientific.}
\label{table:1}
\end{table}

Overleaf provides a more in-depth introduction to tables in LaTeX.

Columns

It’s relatively easy to create a two column LaTeX document, by adding a twocolumn parameter to your document class statement.

\documentclass[twocolumn]{article}

To work with more than two columns at a time, you can import the multicol package and use the multicols environment within your document. In curly brackets following the multicols environment \begin command you can enter the number of columns, and in square brackets you can add any column headers you want to include in between square brackets:


\documentclass{article}
\usepackage{multicol}

\begin{multicols}{3}
[
\section{Methodology}
]
\lipsum

\end{multicols}

We can add a \setlength command in the document preamble passing the \columnsep parameter with the amount of separation we’d like to see between the columns below. Here’s a list of lengths and units you can use in LaTeX.

\setlength{\columnsep}{.75cm}

OverLeaf’s Introduction to Multiple Columns goes into greater depth on the many ways to set up different column options in your document.

LaTeX templates

Many publishers share document templates for authors in various formats such as Word and, increasingly in the sciences, LaTeX. A great way to learn more about LaTeX is to actually import a LaTeX template in Overleaf and notice the packages and formatting that is built in to the document.

IEEE, for example, provides detailed instructions on how to use the IEEEtran LaTeX class to produce conference and journal submissions to IEEE.

IEEE Journal Template

Let’s use the IEEE Template Selector to choose a LaTeX template for a specific journal.

  1. Choose Transactions, Journals, and Letters
  2. Select the first publication from the list, Canadian Journal of Electrical and Computer Engineering.
  3. Choose Original Research and Brief
  4. Select the LaTeX file format, and Download the template.
  5. Unzip the folder, and upload the bare_jrnl_new_sample4.tex file into Overleaf.
  6. Select the file in Overleaf and choose the Recompile button to view the conference paper template.

If you scroll through the compiled document you’ll see many examples of formatted equations, tables, bullet lists, and much more. You can use the File outline in the bottom left corner of Overleaf to jump to different sections of the LaTeX document to see how different content in marked up to follow the IEEE journal style.

Note: The University of Minnesota doesn’t have an official LaTeX template for PhD thesis formatting. There is a user-created template on GitHub but it requires you to build the thesis locally on your command line or to use GitHub Actions to create a thesis PDF on GitHub from your LaTeX files. Unfortunately, this template wouldn’t work (without significant adaptation) on Overleaf or other LaTeX editors.

Key Points

  • Tables can be created in the {tabular} environment and there are many options for customization.

  • Documents can be arranged into two columns using the twocolumn parameter option in our document class, and the multicol package can provide further customization.

  • Many publishers and other entities create LaTeX templates that allow for easy formatting to meet journal, conference, and other requirements.


Citations and BibTex

Overview

Teaching: 20 min
Exercises: 5 min
Questions
  • How can I add footnotes and bibliographies to a LaTeX document?

  • How can I use BibTeX to organize citations for larger projects?

Objectives
  • Learn how to place references within the text of a scholarly article.

  • Learn how to associate references in the text with citations organized in a BibTex file.

  • Learn how to generate a bibliography from the citations in BibTex.

Footnotes

We can use the \footnote{} command to add citations as footnotes, where the argument will be the text of the footnote. First let’s start a Literature Review section (you can delete the \lipsum from the Introduction so it’s easier to see our lit review):

\section{Literature Review}
The scientific method was created a long time ago. One person who gets a lot of credit is Galileo Galilee. But others note that the scholar Ibn Al-Haytham developed similar thinking around 1000 CE in Basra and Baghdad. 

Now let’s add some footnotes to acknowledge our sources, using the \footnote{} command:

\section{Literature Review}
The scientific method was created a long time ago. One person who gets a lot of credit is Galileo Galilee\footnote{Finocchiaro, M. (2012). \textit{Galileo and the Art of Reasoning: Rhetorical Foundation of Logic and Scientific Method.} Springer}. But others note that the scholar Ibn Al-Haytham developed similar thinking around 1000 CE in Basra and Baghdad\footnote{Gorini R. Al-Haytham the Man of Experience: First Steps in the Science of Vision. \textit{J Inter Soc for the History of Islamic Medicine (JISHIM)} 2003;2(4):53–55.}. 

When you recompile the document now, you should see the formatted footnotes at the bottom of the page.

BibTeX

You can manually add citations and footnotes in this manner, but you can see that the citation styles above don’t match each other. It’s also time-consuming to manually add citations in the text over and over. Rather than formatting each citation on its own, we can use a tool and file format called BibTeX to format our citations and footnotes consistently across a scholarly work.

There are a number of different ways to use BibTeX on Overleaf. We’ll use the ‘biblatex’ package. First, you’ll want to add your references to a .bib (plain-text) file in the BibTex file format. Then we’ll import the biblatex package and use its \addbibresource{} command to refer to our .bib file. You can often generate citations in BibTex format directly from scholarly databases or you can export them from citation managers like Zotero or Mendeley.

  1. Download the refs.bib file which includes the citations from the example above in proper BibTex format.
  2. Upload the file to Overleaf.

The upload button in Overleaf is located above the file navigator and has a disk icon with an up arrow

  1. You should now see a refs.bib file and main.tex in your file navigator in Overleaf. Open the refs.bib file and take a look at the contents.
  2. To use these references in your LaTeX document you can import the biblatex package and also refer to the name of the refs.bib file with the \addbibresource{} command. Add the following to the header of the main.tex file:
    \usepackage{biblatex} 
    \addbibresource{refs.bib}
    

Then we can replace the footnotes with the \cite{} command using the citation-key for each reference as the cite command’s argument. The citation-key is the first item in the curly brackets following a BibTex entry. For example, ‘tbackhi_ibn_2007’ is the citation-key in the @article{tbakhi_ibn_2007} reference. You can change the citation-keys to be anything you want, but usually they consist of the author’s name and year.

\section{Literature Review}
The scientific method was created a long time ago. One person who gets a lot of credit is Galileo Galilee \cite{finocchiaro_galileo_1980}. But others note that the scholar Ibn Al-Haytham developed similar thinking around 1000 CE in Basra and Baghdad \cite{tbakhi_ibn_2007}.

You’ll notice that the numbered citation footnotes show up in the text, but the references don’t appear as footnotes at the bottom of the page anymore. To add our list of references, we can add the command \printbibliography at the end of the document:

\printbibliography
\end{document}

If you want the Bibliography to follow a specific citation style there are a variety of styles you can pass as an option in the \usepackage{biblatex} command via [style=...]. If you are submitting your paper for an IEEE publication, for example, you can add the following to the head of your document:

\usepackage[style=ieee]{biblatex} 

This is a nice feature since you can easily switch citation styles if, for example, a paper is rejected by one publisher and you want to submit it somewhere else.

Challenge 1

Can you change the style of your citations to use Chicago’s Author/Date formatting? It should look something like the examples below.

Note: do you have to add any other characters to the \cite{} commands in your text to make those citations appear correctly (with parentheses)? The scientific method was created a long time ago. One person who gets a lot of credit is
Galileo Galilee (Finocchiaro). But others note that the scholar Ibn Al-Haytham developed
similar thinking around 1000 CE in Basra and Baghdad (Tbakhi and Amr).
References
Finocchiaro, M. A. Galileo and the Art of Reasoning: Rhetorical Foundation of Logic and
Scientific Method [inlangen]. Springer Science & Business Media, Dec. 1980. isbn: 978-
94-009-9017-3.
Tbakhi, Abdelghani, and Samir S. Amr. “Ibn Al-Haytham: Father of Modern Optics”. An-
nals of Saudi Medicine 27, no. 6 (2007): 464–467. issn: 0256-4947, visited on 08/23/2022.
https://doi.org/10.5144/0256-4947.2007.464. https://www.ncbi.nlm.nih.gov/
pmc/articles/PMC6074172/.
1

Answer

Add the chicago-authordate style to your \usepackage options, and add parentheses around the \cite{} commands in your text:

\usepackage[style=chicago-authordate]{biblatex} 
...
The scientific method was created a long time ago. One person who gets a lot of credit is Galileo Galilee (\cite{finocchiaro_galileo_1980}). But others note that the scholar Ibn Al-Haytham developed similar thinking around 1000 CE in Basra and Baghdad (\cite{tbakhi_ibn_2007}).

Note that there are also additional commands one can use with biblatex that will modify the formatting of the citations. In this case, \parencite{} will add the parentheses automatically:

\usepackage[style=chicago-authordate]{biblatex} 
...
The scientific method was created a long time ago. One person who gets a lot of credit is Galileo Galilee \parencite{finocchiaro_galileo_1980}. But others note that the scholar Ibn Al-Haytham developed similar thinking around 1000 CE in Basra and Baghdad \parencite{tbakhi_ibn_2007}.

Key Points

  • You can manually add footnotes inline with the text using the \footnote{} command.

  • BibTeX is a file format that allows you to keep track of citation data separately from your main LaTeX document.

  • You can reference citations in a BibTeX file using the biblatex package.

  • You can change the citation style used in biblatex by passing the [style=...] option.