LaTeX 101

An Introduction to Formatting Documents With Code

Jens Brinkmann, Nelis Drost

The University of Auckland

Tuesday, July 9, 2024

What is this session about

Housekeeping šŸ§¹

  • Cameras on, please šŸ“ø

  • Please mute your microphone šŸŽ¤

  • Questions: Zoom chat šŸ’¬

  • This session wonā€™t be recorded šŸ“¹

  • Be kind šŸ˜Š

    • all details of the Code of Conduct šŸ—žļø can be found here

What is \(\LaTeX\)

  • Pronounciation: /ĖˆlɑĖtɛk/ or /ĖˆleÉŖtɛk/
  • \(\LaTeX\): a typesetting system; since 1984
  • Recent advances make it more user-friendly and approchable
  • Writing context and formatting are separated
  • To see an output (or download a PDF) you often need to compile the code

Why \(\LaTeX\)?

First an foremost (my opnion):

  • because the output looks very professional (often you can tell at a glance that it is a \(\LaTeX\) document)
  • there is less risk of breaking the whole document (as it is plain text)

ā€¦ also good because:

  • free and open-source
  • is cross-platform
  • standard for many scientific publications (for some journals mandatory)
  • great for writing mathematical equations
  • great for writing code
  • (parts) can be automated
  • it decides where to place images, tables, etc. (you can also specify this), especially good if you decide to add more and donā€™t want them to be placed in awkward places
  • you can have in-line-comments (for future you, for your supervisor, to toggle things on/off)
  • add references while you write (\cite{} and done)

Is \(\LaTeX\) scary?

  • Initially, it might be, because it is not wysiwyg
  • But it has come a long way, you can use it in your browser: Overleaf
  • There areā€¦
    • shortcuts
    • workarounds
    • a cheat sheet
    • we will provide you with PhD thesis template

Note

You can keep all this offline/local/no-cloud, which is good for Ethics Approvals and sensitive data

even if you donā€™t have the software to compile a document, you can still extract the content (because you can read/extract the meaning from the plain text files)

Warning

You can waste an almost infinite amount of time on fiddling with layout details

Cost-benefit Tradeoff

Warning

  • trade-off: time invested upfront learning \(\LaTeX\) vs. 
  • time saved in the long-run
  • But then also last-minute PhD-thesis or other submission deadlines: Do you have the time by then? horror stories
  • Many collaborators/supervisors are familiar with Word and donā€™t want to deviate from it
  • While GitHub is great; Track Changes has one advantage (in my opinion)
    • it knows where cut-pasted things went!
  • plugins such as Grammarly can be finicky

Alternatives

To recap:

  • \(\LaTeX\) separates content from formatting, letting you focus on writing and fiddling šŸ„ø
  • Cross-platform compatibility (, , , ā€¦)
  • There is a wide community and often several ways to get things done.
    • It is rare that you are the first to ask a question.
  • Most of the (good) books, journal articles, etc. are created using \(\LaTeX\) for a reason!
  • it is open source and uses plain text files
  • ā€¦just to name a few

Note

You specify structure by inserting e.g.: \section, \begin{figure}, \begin{tabular}, etc.

You donā€™t need to worry about how those will look until later. (e.g.: \(1a, 1b \rightarrow 1.1, 1.2\))

What is it for

Articles

Theses

Books

What is it for?

Reports

Forms

Repeatable layouts & auto-generated content

What is it for?

Beamer template for presentations

Note

this website/presentation was built using Quarto and its RevealJS integration

Visualisations in TikZ

Example showing the power of the TikZ package

Visualisations in TikZ -1

Focused ion beam system, Author: Clemens Helfmeier Source

Visualisations in TikZ -2

2D Visualisation in TikZ Source

Visualisations in TikZ -3

3D Visualisation in TikZ Source

Visualisations in TikZ -4

(Neuronal) Network Visualisation in TikZ Source

Easily go from one to two column layouts

\documentclass[10pt,a4paper,onesided]{article} 
\usepackage{lipsum}
\begin{document}
  \section{My Title}
  \subsection{Sub-Section}
  \textbf{Printing 1 to 3 paragraphs}\\
  \lipsum[1-3]
  \subsection{Sub-Section}
  \lipsum[1]
\end{document}

Note that: ā€¦onesidedā€¦ One Column

#| \documentclass[10pt,a4paper,twocolumn]{article} 
\usepackage{lipsum}
\begin{document}
  \section{My Title}
  \subsection{Sub-Section}
  \textbf{Printing 1 to 3 paragraphs}\\
  \lipsum[1-3]
  \subsection{Sub-Section}
  \lipsum[1]
\end{document}

Note that: ā€¦twocolumnā€¦ Two Column

Maths/Formulae

External Files

  • External files are referenced
    • e.g. an Images folder.
    • updating files (same file name) come into effect automatically/quickly (after recompiling)
    • While doint that, consider a version control mechanism such as GitHub to avoid data loss
  • Similarly: imported data (often in the csv file format) which you can get to automatically update tables
  • Even layouts can be changed plug and play

Warning

How would you do this is a MS Word document? How can you make sure that you updated all and didnā€™t miss one? There are workardounds, but they are not as elegant as in \(\LaTeX\)

How does creating a document in \(\LaTeX\) work?

While our current session is just a taster Session, some pointers at important elements follow below.

To get hands-on experience, I recommend (again) the advanced session: How to Create a LaTeX Report Without Losing Hair - Thu 1-3pm

Preamble

  • similar to coding:
    • you start with importing relevant libraries (things others did to make your life easier)
    • and definitions (for example, colours)
\documentclass[twoside,openright,a4paper]

\usepackage[usenames, dvipsnames, table]{xcolor}

\addbibresource{references.bib}

\definecolor{uoadarkblue}{RGB}{0, 70, 127}

Main body

You can either

  • write all your document in one text file (with the file extension *.tex)
    • often better practice: \include command to include chapters
  • \(\LaTeX\) commands usually start with the backslash character \...
  • as in all coding: Details matter: close brackets you opened, etc.
  • \(\LaTeX\) is mostely whitespace-insensitive, this means you can write your .tex document without having to worry about empty lines, etc. This also means, if you want to include empty lines, you have to enforce them
    • \\ (two backslashes)
    • \newline
    • \hfill \break
    • see also
\include{Titlepage}
\part{Background}
\include{Chapters/Intro}

References, Appendices, etc.

  • all of these can be ingested (reference to external files) in the preamble
  • Itā€™s fair to consider this a strong-suit of \(\LaTeX\)
\section{Test}\label{sec:test}
Some text
\begin{equation}
  e = m c^2
  \label{eqn:emc}
\end{equation}

\subsection{Discussion}
As we can see in Equation \ref{eqn:emc} in Section \ref{sec:test}...

How to get \(\LaTeX\)

If you are eager to get started, you might wonder how to get \(\LaTeX\) on your machine:

  • Perspectives:
    • from local machine to cloud-based
    • open-source to paid subscription

We will look at

  • ā€¦ the core
  • ā€¦ special editors
  • ā€¦ in the browser
  • ā€¦ the meta version

How to get \(\LaTeX\) - the core

Tip

Idea: As software-agnostic as possible

  • Download a \(\LaTeX\) distribution, for example MiKTeX
  • install pandoc
  • use any texteditor TextEdit, Notepad,, various
  • a generic IDE, such as VSCode
  • on the commandline: type pandoc -i ~/Desktop/myWritings.tex -o ~/Desktop/MyRenderedLaTeX.pdf
  • inspect your neatly renedered document

How to get \(\LaTeX\) - special editors

Tip

Idea: local installation and a specialised editor

How to get \(\LaTeX\) - In the browser

  • Overleaf is quote famous for ā€œbringing \(\LaTeX\) to the massesā€
  • A pro and con at the same time: It runs in the cloud, you donā€™t need to install a \(\LaTeX\) distribution, special editor, etc.
  • Recently, they added ā€œOverleaf On-Premisesā€ to circumvent privacy challenges
  • there is a free plan which might be a good fit for beginners or if it is just you wokring on a dcument
  • there are paid plans, including student plans which are currently NZD109/year
    • The main advantages:
      • Invite collaborators, so work on the same document at the same time
      • Have a similar tracking mechanism as MS Wordā€™s Track Changes
      • Sync to Dropbox, GitHub, etc.
  • University of Auckland ABI staff/students get Overleaf premium free

How to get \(\LaTeX\) - The meta version

Quarto can be considered one level above in terms of abstraction text

  • \(\LaTeX\) is just one of its output, the other benefit is a direct integration with Python, R, Julia, Observable
  • you can write in markdown syntax (.md) so for example **my text** (md notation) as oppsed to \textbf{my text}
  • the integration with code means you wonā€™t have to copy-paste results back and forth
    • this point canā€™t be stressed enough! You are so likely to forget updating a plot
    • you also get reproducability, because it isnā€™t just a screen shot that is integrated into your document <!ā€“ (There are approaches where you can get some (rudimentary) Python code into LaTeX, even into Overleaf, but these are mainly for handling several files (batch processing) keeping track of Python version- Coding colaborators can contribute via a Git workflow
  • Communicate results to non-coding collaborators using git (e.g., GitHub)

Trying to make your \(\LaTeX\) journey easier

  • Template
    • Resulting in a neat design (including page numbers in roman, arabic or intentionally omitted)
    • Shows how to get certain things done (Part, Section, Sub-Section, labels,ā€¦)
    • There are other templates of Overleaf by the UoA
  • TablesGenerator and or LaTeXTables
  • Mathpix Snip
  • LaTeXIt (to quickly create equations, only)
  • You can write in Markdown notation and use Pandoc to convert to \(\LaTeX\) or MSWord *.docx (which you can easily pass to strict Word users)
  • As \(\LaTeX\) is based on plain text files, you can use diff commands (see Git) or alternatively use latexdiff which is better with interpreting the syntax and can highlight changes which are then output to a PDF

Tables

  • A simple table in \(\LaTeX\) syntax:
\begin{table}[]
\caption{My Example Table}
\label{tab:my-table}
\begin{tabular}{|l|l|l|l|l|}
\hline
country     & 1999   & 2000   &  &  \\ \hline
Afghanistan & 745    & 2666   &  &  \\ \hline
Brazil      & 37737  & 80488  &  &  \\ \hline
China       & 212258 & 213766 &  &  \\ \hline
\end{tabular}
\end{table}
  • Alternatively using markdown syntax:
| country     | 1999   | 2000   |   |   |
|-------------|--------|--------|---|---|
| Afghanistan | 745    | 2666   |   |   |
| Brazil      | 37737  | 80488  |   |   |
| China       | 212258 | 213766 |   |   |
  • To be honest:
    • Both are a pain to format by hand
    • You shouldnā€™t work with your data in these. These are for publishing only
  • TablesGenerator and or LaTeXTables can help you with that
  • If applicable(!!!): ChatGPT etc. do a pretty decent job; make sure you are allowed (multi-stakeholder) to use them

\(\LaTeX\) guides and references

The Not So Short Introduction To Latex

Overleaf tutorials

Latex Reference (also as a website)

Wiki-book

Time for questions

?

ResBaz 2024 | LaTeX 101