We'll be using $\LaTeX$ (pronounced "LAH-tech") to typeset our proofs. It's the de facto standard of mathematical typesetting, used throughout computer science.

Installing $\LaTeX$

You'll need to either install TeX yourself (I use TeX live) or use an online service like Overleaf. There are pros and cons to each: having your own software setup means you know what's happening where and don't need internet access; using a cloud service means you have backups and don't have to worry about the details.

Homework will be distributed as .tex files with holes for you to fill in. Such texfiles are a mix of plain text and commands, which are typically the form \COMMAND[OPTARG]{ARG}.

Using $\LaTeX$

$\LaTeX$ is an old and complex system that takes some time to learn. Don't be afraid to copy or search for help! The TeX Stack Exchange is a useful place to look. One word to the wise: if you're going to search for help about a $\LaTeX$ command, well... check your phrasing or turn on SafeSearch. 😬

Outline of a TeXfile

In general, a .tex file has the following structure:

% CLASS
\documentclass{article}

% PREAMBLE
\usepackage{fullpage}
\usepackage{amsmath,amssymb}

% DOCUMENT
\begin{document}

This is a \emph{beautiful} document, with math that takes it to the $\lim_{x \rightarrow \infty}} x$.
% this is a comment
This is another sentence in the same paragraph.

This starts a new paragraph.
\end{document}

Any part of the line after % is a comment. If you need to write a percent sign, write \%.

Formatting

$\LaTeX$ is a powerful formatting engine even for ordinary text. By default, whatever text you write between \begin{document} and \end{document} will simply be displayed. One or more empty lines between text creates a new paragraph.

If you'd like other formatting our structure, you'll use a variety of commands to control things. As seen in the example above, $\LaTeX$ commands start with a backslash \. Some commands take arguments, written with curly braces, as in \COMMAND{arg}.

Punctuation

Most punctuation in $\LaTeX$ is ordinary: you can just write commas, periods, semicolons, etc., as usual. While the generally correct convention is to have only one space after a period, $\LaTeX$ doesn't really care how many spaces you put: whitespace like spaces, tabs, and newlines will be "collapsed" into what $\LaTeX$ thinks is best.

There are two special bits of punctuation that don't just 'work': quotes and dashes.

$\LaTeX$ makes you specify which is the "left" quote, ` and which is the "right quote", '. So a single quoted bit of text will look like `word', and a double-quoted one will look like ``Longer quote''.

To write dashes, just use multiple - characters. One - gives you a hyphen, two -- gives you an endash, and three gives you an emdash.

Text decoration

You can make text italic with \emph{...}, bold with \textbf{...}, and underline with \underline{...}.

Lists

You can write lists using various list environments. $\LaTeX$ environments are a general way of structuring parts of your text: you write \begin{...}, the contents of the environment, and then \end{...}. For example, the itemize environment is used for bullet lists, where each bullet is an \item. Here's an example:

\begin{itemize}
\item milk
\item eggs
\item Perigord truffles
\end{itemize}

Notice that the \item command takes no arguments. You'll get something like:

If you want a numbered list, enumerate:

\begin{enumerate}
\item Partridge in a pear tree.

\item Turtle doves.

\item French hens.

\item Calling birds.
\end{enumerate}

You'll get:

  1. Partridge in a pear tree.
  2. Turtle doves.
  3. French hens.
  4. Calling birds.

We use the enumerate environment to number the homework problems.

Code

The easiest way to typeset code---which can contain all kinds of special characters---is to use a verbatim environment.

\begin{verbatim}
Fixpoint half (n:nat) : nat :=
  match n with
  | O => O
  | S O => O
  | S (S n) => S (half n)
  end.
\end{verbatim}

will produce:

Fixpoint half (n:nat) : nat :=
  match n with
  | O => O
  | S O => O
  | S (S n) => S (half n)
  end.

Other commands

There are many other $\LaTeX$ formatting commands: the tabular environment for drawing tables, table and figure environments for floating figures, \includegraphics for adding in pictures, and many more. It's possible to define your own commands, too, and there are thousands of useful packages on CTAN.

When writing computer science research papers, $\LaTeX$ is typically paired with bibliographic tools like bibTeX. You'll learn that tooling for CS 190 Senior Seminar, if not earlier.

Beautiful Mathematics with $\LaTeX$

$\LaTeX$ excels at mathematical typesetting, making it (relatively) easy to write beautiful mathematics on your own computer. This is what we came for people. Mathematics! Math-e-mat-ics! Let's GO!

Inline math 🤑

You can write inline mathematics---that is, math in the middle of a sentence---between dollar signs, as in $math goes here$. This textbook uses MathJax to render $\LaTeX$ mathematics. You can right-click on any bit of mathematical typesetting, select "Show Math As > TeX Commands" to see a pop-up window the $\LaTeX$ source that produced that code.

Ordinary arithmetic doesn't require anything special. For example, you can say that $y = mx + b$ using conventional symbols. Notice that $mx$ gets typeset as a nice multiplication using just juxtaposition. There other ways to do it, though: $y = m \cdot x + b$ or $y = m \times x + b$. Notice that you must write a space after \cdot or \times, or else $\LaTeX$ will think you wrote \cdotx or something.

Math mode ignores whitespace. You can write inline math expressions that span several lines, or with lots of extra space.

Display math

Display mathematics---that is, math set out from the body of the paragraph and centered on the page---is written one of two ways. You can either write \[ math goes here \] or you can write $$ math goes here $$. I tend to prefer the former, since it's less likely to run into problems that are hard to debug, like writing $$ on one side and just $ on the other.

Beyond where the formulae are placed on the page, there are some minor differences in terms of how inline and display math are typeset.

Important math commands

We've defined many concepts in the course so far: logical operations, arithmetic operations, operations on lists and sets, and many others. We'll reintroduce notations as we go, but here are several important ones:

There are other conventions that you should follow. Function application is written $f(x)$ unlike Coq's f x. Even if you're talking about Coq code, you should use math notation in math.

You need to be careful with long function and constructor names. Write $S(n)$, not $S\ n$. Feel free to conflate $S(n)$ and $1+n$ and $n+1$: we all know what you mean.

For longer function and constructor names, you have to be more careful. Math mode assumes that juxtaposition is multiplication, so $half$ is kerned so that we can interpret it as $h \cdot a \cdot l \cdot f$. (Do you see the funny space around $f$?) If you want a long name in math mode, you should use a font command to force correct kerning. You have a variety of choices here:

All of these are good choices. I tend to use \mathit or \texttt, depending on whether I want to emphasize the "mathiness" of the name or the fact that it's written in code. I also like \mathsf. You should use what makes you happy, so long as it's clear, consistent, and looks clean.

One final note about names from Coq in $\LaTeX$: underscores are a special character, used for subscripts, as in $x_0$. If you want a literal underscore, write \_, as in $\texttt{plus_n_O}$.

Several sets have special names. For example, the type nat is written $\mathbb{N}$. (Write \mathbb{...} to make math your bb use "blackboard" font.) Other types we define in Coq are best written using \texttt, e.g., $\texttt{strand} = \texttt{list}
\texttt{base}$. Note that we have to force the space in math mode, writing \ between \texttt{list} and \texttt{base}. You can also write ~ to force a space, as in $\texttt{bt} ~ \mathbb{N}$.

Types should be assigned with $:$; it can also be fine to use $\in$, which is really set containment. Function types should be written with $\rightarrow$; functions should always be assigned types with $:$. So we might say $\forall n \in \mathbb{N}, n = n + 0$, or that: $$\forall f : \texttt{bool} \rightarrow \texttt{bool}, (\forall b \in \texttt{bool}, f(f(b)) = b) \Rightarrow \forall b \in \texttt{bool}, f(b) = b \vee f(b) = \neg b.$$ Notice how we use $:$ for $f$ and $\in$ for $b$. We could have written $b : \texttt{bool}$ just as well. Whatever you do, be (locally) consistent.

Every Greek letter is available in math mode. (Some capitals are not directly available, because \Alpha and regular old A are the same letter.) It's not uncommon to use $\phi$ or $\psi$ as formula names, but try not to go overboard with the Greek, as cool an alphabet as it may be. We'll also meet some Hebrew letters, but generally other alphabets/syllabaries/ideograms are not used in mathematics.

Useful math environments

There are two math environments that are particularly important: cases and array.

Cases

Mathematicians use cases to write pattern matches and other conditionals. For example, here's how we'd translate double from Coq to math:

Fixpoint double (n : nat) : nat :=
  match n with
  | O => O
  | S n' => S (S (double n'))
  end.
\[ 
  \mathit{double}(n) = 
  \begin{cases}
    0                         & n = 0 \\
    S(S(\mathit{double}(n'))) & n = S(n') \\
  \end{cases}
\]

Take a close look at the formatting. We say \begin{cases} to start defining something by cases. In math, the order is reversed from Coq: you say what you return, then you say when you return it. Rather than using patterns like in Coq's match, we use boolean expressions. You use an ampersand & to separate the value returned from the expression. At the end of the case, you write \\ to start the next one, writing \end{cases} when you're done. You can write as many cases as you want (but less than two doesn't make much sense!).

On the typesetting side, notice that we've aligned the & column dividers. This makes it much easier to read things.

On the math side, notice the convention that variables can be 'bound' in the expression: when we say $n = S(n')$, we then use $n'$ in the expression $S(S(\mathit{double}(n')))$... just like how we mention n' in the pattern of the second case of the match and then use it in the recursive call.

Arrays

Arrays let you arrange things in a grid. They're crucial for writing matrices, but they're also very useful for other layout tasks. For example, here's another way to define $\mathit{double}$:

$$ \begin{array}{rcl} \mathit{double}(0) &=& 0 \\ \mathit{double}(S(n')) &=& S(S(\mathit{double}(n'))) \\ \end{array} $$

There are some familiar elements here: & serves as a separator between columns; \\ serves as a line terminator. The new thing here is the column declaration {rcl}, which says that we'd like three columns: the first should be right justified (r), the second should be centered (c), and the third should be left justified (l).

Column declarations let you draw some very nice tables with arrays: a useful thing for truth tables! Here's a truth table for negation:

$$ \begin{array}{|c|c|} \hline a & \neg a \\ \hline \top & \bot \\ \hline \bot & \top \\ \hline \end{array} $$

There are few more elements here. The | character in column declarations inserts a vertical line. The \hline command inserts a horizontal line. By using the two together, we were able to draw the border of the table.

Arrays are very similar to the tabular environment for drawing text-mode tables; once you're used to the array environment, you'll have an easy time with tabular.

Chaining equations

One particularly nice use of arrays is to neatly align "chained" equations. Here's a short example:

$$ \begin{array}{rl} & (x + 47)\cdot(x - 47) \\ = & x\cdot(x-47) + 47\cdot(x - 47) \\ = & x^2 - 47x + 47\cdot(x - 47) \\ = & x^2 - 47x + 47x - 2209 \\ = & x^2 - 2209 \\ \end{array} $$

Notice how we use only two columns: one for the equal signs (omitted on the first line) and one for the thing we're "chaining" equalities on. Each line represents a step of computation.

For longer chains of reasoning, you can add a third column and use \text to explain things, as in:

$$ \begin{array}{rlr} & (x + 47)\cdot(x - 47) & \\ = & x\cdot(x-47) + 47\cdot(x - 47) & \\ = & x^2 - 47x + 47\cdot(x - 47) & \\ = & x^2 - 47x + 47x - 2209 & \text{remember that $47 \cdot 47 = 2209!$} \\ = & x^2 - 2209 & \\ \end{array} $$

The command \text{...} switches you out of math mode back into text mode. You can freely nest \text and math as much as you like.

Packages and other bits

At the top of the homework files we give you, there are a few \usepackage commands, typically something like:

\usepackage{fullpage,parskip}
\usepackage{amsmath,amssymb}
\usepackage{xcolor,graphicx}

The \usepackage command takes a comma-separated list of packages. The first line sets up page margins (fullpage) and using vertical space to separate paragraphs (parskip); we could have written it separately as:

\usepackage{fullpage}
\usepackage{parskip}

The next line loads up some nice math definitions (amsmath) and symbols (amssymb) that have been defined by the American Mathematical Society (AMS).

The last line sets up color (xcolor) and the ability to include graphics in our code (graphicx).

Our homeworks will also include a command like:

\newcommand{\XXX}{{\color{red} \textsf{\textbf{FILL IN AND DELETE THE \textbackslash{}XXX}}}}

The \newcommand form takes two arguments: a command to define, and what it should expand to. Here the new command is \XXX, and it expands to some colored text telling you to fill things in.

Note that \color{red} is used in a funny way: we wrap it inside more curly braces, writing {\color{red} this is red} this isn't. What's going on here? The command \color{red} makes all text after it red. Using { ... } sets up a "scope". Such scoped commands are a bit error prone; you could instead write \textcolor{red}{this is red} this isn't.

Doing the homework

Throughout the homework we mark places for you to fill things in with \XXX which will render as some red text telling you to write your solution there and remove the \XXX. We ask you to put your name at the top, too.

In general, each homework will be a large enumerate environment; each \item between \begin{enumerate} and \end{enumerate}

We're careful to put each problem on its own page---it makes our grading easier. Please don't delete the \clearpage commands at the end of each problem.

We ask you to submit your compiled PDFs, not your TeX source. Please check over the PDF output to make sure it looks, you know, "nice".

Debugging errors

One of the least nice things about $\LaTeX$ is the way it produces output and notifies you of errors. When some erroneous condition is encountered, it will typically just spits out an error message and carry on its merry way. If an error did occur, you'll get a notice at the end of $\LaTeX$'s copious output; you'll have to scroll up to find it.

If things go badly enough---you try to \includegraphics{cool_graph.png} and $\LaTeX$ can't find the file, you'll get dumped in an interactive prompt. Type X and hit enter to leave the prompt, so you can carry on.

If you're using Overleaf, things will be a bit better: they do an okay job finding errors and pointing them out to you, but it's very easy for things to go off the rails. If you're stuck... ask for help!