Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 72 additions & 7 deletions _2023/lecx_latex.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ ready: false

## What is LaTeX?

LaTeX is a *document preparation system* which is able to produce high-quality output for various typesetting goals, and is often used in technical setting because of its extensibility and handling of typesetting of math formulae.

It is build on a typesetting system called *TeX* designed by Donald Knuth to typeset 'The Art of Computer Programming' first released in 1978. TeX is a token based programming language, and is the engine behind the extensibility of LaTeX.

LaTeX and its many packages are an extension to TeX. The goal of LaTeX is to simplify preparation of documents, and split the document structure and content from the design.


## When to and not to use LaTeX

# LaTeX with Overleaf

Overleaf is one of many LaTeX editors available to use. To quote their website, Overleaf is "The easy to use, online, collaborative LaTeX editor".

While not FOSS software, Overleaf is free to access for all University of Birmingham students. To get started, head to [https://www.overleaf.com/login](https://www.overleaf.com/login) and log in through your institution. We will discuss FOSS alternatives to Overleaf at the end of the lesson.
# LaTeX with Overleaf

Overleaf is one of many LaTeX editors available to use. To quote their website, Overleaf is "The easy to use, online, collaborative LaTeX editor". It is a freely available browser application, and it has a paid Premium version which is free to access for all University of Birmingham students and staff.
To get started, head to [https://www.overleaf.com/login](https://www.overleaf.com/login) and log in through your institution. We will discuss FOSS alternatives to Overleaf at the end of the lesson.

![overleaf_login](/2023/files/latex/overleaf_login.png "Login page of overleaf.com.")

Expand All @@ -25,13 +33,60 @@ Let's create a new blank and explore what Overleaf has to offer.

# The basics

## Document structure and a simple example

```latex
% TOPMATTER AND METADATA
\documentclass{article}
\usepackage{amssymb}

\author{A.M. Turing}
\date{28 May, 1936}
\title{On Computable Numbers, with an Application to the Entscheidungsproblem}

% CONTENT
\begin{document}

\maketitle

...

\section{Computing machines}

We have said that the computable numbers are those whose decimals are calculable by finite means. This requires rather more explicit definition. No real attempt will be made to justify the definitions given until we reach Section~\ref{section:extent}. For the present I shall only say that the justification lies in the fact that the human memory is necessarily limited.

We may compare a man in the process of computing a real number to a machine which is only capable of a finite number of conditions $q_1, q_2, \dots, q_R$ which will be called ``$m$-configurations''. The machine is supplied with a ``tape'', (the analogue of paper) running through it, and divided into sections (called ``squares'') each capable of bearing a ``symbol''. At any moment there is just one square, say the $s$-th, bearing the symbol $\mathfrak{S}(r)$ which is ``in the machine''. We may call this square the ``scanned square''. [...]

[...]

\end{document}
```

## Syntax

(La)TeX has several special symbols that are used to invoke commands (e.g., `\maketitle`), environments (e.g., `\begin{document} ... \end{document}`), etc.

*Commands* are entered as backslash followed by a command name which is composed of ASCII letters, e.g., `\command`. Often commands have parameters, and optional parameters which are (usually) entered in curly, and square braces, e.g., `\usepackage[margin=1in]{geometry}`. Square braces denote optional parameters.

*Environments* start with the command `\begin{name}` and end with a matching `\end{name}`. They are used for elements with substantial content (e.g., tables, lists, abstract, figures, etc.). They may also have required and optional parameters which follows the `\begin{name}` command.

*Comments* start with `%` and end with an end of line. Multiline comments must include `%` on the start of each line.

There are several special symbols, e.g., `%`, `#`, `$`, `&`, `\`, `{`, `}`, that have a reserved meaning in the language, and must be replaced by commands in text, e.g., the symbols `$`, `%`, and `&` are typeset by escaping them with a backslash (`\$`, `\#`, and `\&`), `\` is typeset by `\backslash`, `{` and `}` are typeset as `\{` and `\}`.


# Document classes and packages

Every document has a *class* which is selected by the command `\documentclass{...}'. In the above example, the class is a LaTeX standard class `article'.

*Packages* are loaded using the command `\usepackage{...}`. There are uncountably many packages.
Several packages can be loaded at one, e.g., `\usepackage{amsmath,amssymb,amsthm}` loads the packages `amsmath`, `amssymb`, and `amsthm`. Some packages allow parameters which are entered in square brackets, e.g., `\usepackage[margin=2cm]{geometry}` (in this case, only one package can be loaded with a single invocation of `\usepackage`).

# Typesetting more exciting stuff

## Mathematical expressions

LaTeX only really starts to shine when used to typeset complex content that cannot be easily formatted in a WYSIWYG editor. A good example of this is mathematical expressions and equations. While this is not supported by LaTeX entirely out-of-the-box, mathematical statements can be typeset using the `amsmath` package with symbols from the `amssymb` package.
LaTeX really starts to shine when used to typeset complex content that cannot be easily formatted in a WYSIWYG editor. A good example of this is mathematical expressions and equations. While this is not supported by LaTeX entirely out-of-the-box, mathematical statements can be typeset using the `amsmath` package with symbols from the `amssymb` package.

Below is a simple equation typeset inline with other text. Observe how the expression is enclosed in `$` signs.

Expand Down Expand Up @@ -69,9 +124,17 @@ $\frac{5x}{10} = \frac{x}{2}$

![output_amsmath_frac](/2023/files/latex/output_amsmath_frac.png "An equation with fractions.")

### Equations
### Equations and displayed math

So far, we have only learned how to inline our maths expressions. Some use cases may require a full-width expression, say a theorem we might wish to introduce in our document. In plain LaTeX, this is done by enclosing the mathematical expression in `\[` and `\]`, or double dollars (which is discouraged).

```latex
\[
c^2 = b^2 + a^2
\]
```

So far, we have only learned how to inline our maths expressions. Some use cases may require a full-width expression, say a theorem we might wish to introduce in our document. `amsmath` provides an `equation` environment that allows for this, which we use below to typeset the Pythagorean theorem.
The package `amsmath` provides an `equation` environment that numbers such equations throughout the whole document.

```latex
\begin{equation}
Expand All @@ -93,7 +156,7 @@ To typeset and align multiline equations, we use the `align` environment:

![output_amsmath_align](/2023/files/latex/output_amsmath_align.png "A multi-line equation.")

The output can be thought of as a table where columns are demarcated by `&` and rows by `\\`. In other words, `&` denotes the end of a column and `\\` denotes the end of a line. Using this information, `amsmath` typesets the equation such that all `&` are aligned one below the other. Observe how the two lines of differing length are aligned to the `=`symbol.
The output can be thought of as a table where columns are demarcated by `&` and rows by `\\`. In other words, `&` denotes the end of a column and `\\` denotes the end of a line. Using this information, `amsmath` typesets the equation such that all `&` are aligned one below the other. Observe how the two lines of differing length are aligned to the `=` symbol.

A handy tip to note is that automatic line numbering can be switched off by suffixing `*` to `amsmath` environments, e.g. `\begin{align*}` and `\end{align*}`. Alternatively, numbering can be turned off for specific lines by starting them with `\nonumber`. For example,

Expand Down Expand Up @@ -145,7 +208,9 @@ def add(x, y):

A comprehensive list of what `minted` is capable of can be found [here](https://tug.ctan.org/macros/latex/contrib/minted/minted.pdf).

<!--
### Using `listings`
-->

## Images

Expand Down