Guide to $\LaTeX$


Written by Keith, inspired by materials by Cynthia Lee and David Varodayan

In CS103, we require you to type your problem sets. While you can use pretty much any system you'd like for this (Microsoft Word, LibreOffice, Google Docs, etc.), we encourage you to use $\LaTeX$.

$\LaTeX$ is a mathematical typesetting system. It's widely used in academia for typesetting papers, posters, and books. Once you learn what something typeset in $\LaTeX$ looks like, you'll start seeing it everywhere.

Although $\LaTeX$ is powerful enough to create intricate and beautiful layouts for books, it's also simple enough that you can learn the basics in a matter of hours. This guide explores the basics of how to typeset your problem sets in $\LaTeX$.

Getting Started with $\LaTeX$

In order to write your problem sets in $\LaTeX$, you'll need a compiler that converts .tex files into beautiful PDFs. We recommend the lovely online tool Overleaf. Sign up using your @stanford.edu email account - to get you full access to all the site's features.

Once you're there, click the "New Project" button. Pick a title for your project (maybe something like "Test" or "My Very First LaTeX Document"), and you'll be taken to a screen with two panes. On the right you'll see a PDF containing some sample text. On the right you'll see the raw $\LaTeX$ source that generated that document. Here's the source that generated my document:

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

\title{Test Project}
\author{htiek }
\date{April 2024}

\begin{document}

\maketitle

\section{Introduction}

\end{document}

Let's add some text onto the page. Right below the line \section{Introduction}, add this text:

Hello, world! Watch me do math: $(x + 1)^2 = x^2 + 2x + 1$.

After you've done this, click the "recompile" button. You should see see this text appearing below the "1 Introduction" header. (I've put the output in a bordered box to separate it from the rest of this text; you won't see the border in the version on Overleaf.)

Hello, world! Watch me do math: $(x + 1)^2 = x^2 + 2x + 1$.

Isn't that beautiful! Look at those superscripts!

Now, change what you've added beneath the \section{Introduction} line to look like this:

Hello, world! Watch me do math: $(x + 1)^2 = x^2 + 2x + 1$.

\LaTeX can do other things too. Here's a fraction: $\frac{137}{42}$.

You'll now see this:

Hello, world! Watch me do math: $(x + 1)^2 = x^2 + 2x + 1$.

$\quad \LaTeX$ can do other things too. Here's a fraction: $\frac{137}{42}$.

You might be starting to infer some things about how $\LaTeX$ documents are formatted. Plain text just displays as-is. When you surround text with the $ character, it somehow starts thinking that you're doing Mathy Things. Special commands start with the \ character, and curly braces are used for arguments or to group things together. And indeed, all of that is correct! Let's start looking at each of these in detail.

Text Mode and Math Mode

$\LaTeX$ has two modes: text mode, where characters are written out as text, and math mode, which is used for equations. By default, everything is written in text mode. So if you want to write text in a $\LaTeX$ document, just write that text as usual. $\LaTeX$ will then make it absolutely beautiful. (Like, seriously - look closely at the text that's rendered.)

In math mode, $\LaTeX$ will interpret text as if you're writing out formulas. Use the $ character to surround what you want to include in math mode. Many simple expressions work the way you'd expect:

  • Powers and exponents can be done using the ^ operator. For example, $x^2 + 5$ renders as $x^2 + 5$.
  • Subscripts can be done using the _ operator. For example, $F_2 = F_0 + F_1$ renders as $F_2 = F_0 + F_1$.

When using subscripts and superscrips, be aware that, by default, only a single character is picked up by the ^ or _ operators. For example, if we write $x^42 = a_137$, we get $x^42 = a_137$, which doesn't match the intuitive mathematical meaning. To fix this, we can use curly braces to group things together. The expression

x^{42} = a_{137}

will render as $x^{42} = a_{137}$, more closely matching our meaning.

Although you probably won't need to do this all that much, you can actually chain ^ and _ together! For example, the expression

$x^{y^{z_2}}$

renders as $x^{y^{z_2}}$.

There are a few other things to be aware of about math mode. First, you can surround text with two dollar signs ($$) to have that text intepreted as math, but then displayed on its own line and centered. For example, consider this $\LaTeX$ example:

The quadratic formula says that the solutions to $ax^2 + bx + c = 0$ are
given by $$\frac{-b \pm \sqrt{b^2 - 4ac}}{2a},$$ a formula that is commonly
memorized in early algebra courses.

This renders as follows:

The quadratic formula says that the solutions to $ax^2 + bx + c = 0$ are given by

\[\frac{-b \pm \sqrt{b^2 - 4ac}}{2a},\]

a formula that is commonly memorized in early algebra courses.

On that note, let's piece apart how we did the quadratic formula. The command \pm is the "plus or minus" symbol. To display a fraction, we use the syntax \frac{numerator}{denominator}. And to do a square root, we write \sqrt{expression}. Can you see how everything fits together here?

There are a couple of other ways to enter math mode, but we'll save them for later.

Math Symbols

In what we covered above, you saw \pm for $\pm$, the \frac command to $\frac{\text{display}}{\text{fractions}}$, and the \sqrt command to $\sqrt{\text{say something radical}}$. But this is just the tip of the iceberg, and there are a bunch of other mathematical tools available in $\LaTeX$.

Let's begin with some operations on sets:

  • A \cup B displays as $A \cup B$.
  • A \cap B displays as $A \cap B$
  • A \backslash B displays as $A \backslash B$.
  • A \triangle B displays as $A \triangle B$.
  • x \in S displays as $x \in S$.
  • x \not\in S displays as $x \not\in S$.
  • S \subseteq T displays as $S \subseteq T$.
  • S \not\subseteq T displays as $S \not\subseteq T$.
  • |S| displays as $\vert S \vert$.
  • \wp(S) displays as $\wp(S)$.

Here's some ways of describing names of single sets:

  • \emptyset displays as $\emptyset$.
  • \mathbb{N} displays as $\mathbb{N}$.
  • \mathbb{Z} displays as $\mathbb{Z}$.
  • \mathbb{R} displays as $\mathbb{R}$.

In case you're wondering what \mathbb means, it's "math blackboard bold," the fancy name for those sorts of symbols.

And, should you need it:

  • \aleph_0 displays as $\aleph_0$.

Writing out actual sets is a bit trickier. Remember that $\LaTeX$ treats curly braces as something that groups mathematical expressions together. As a result, if you write {1, 2, 3}, it'll display as ${1, 2, 3}$. To explicitly write out those braces, write it out like this:

\{1, 2, 3\}

That will display as ${1, 2, 3}$, which is what's intended. Keep an eye out for this when typesetting your solutions - it's really easy to forget to do this, and leaving off braces can change the mathematical meaning of what you write!

Other Symbols

There are plenty of other symbols that we'll use over the course of the quarter. What should you do if you want to write one of them, but aren't sure how?

One option would be to check our $\LaTeX$ templates - we'll include some examples up on the front page to make things easier for you to copy and paste.

Another option would be to use the remarkable website Detexify. Draw whatever symbol you're looking for, and it will suggest possible options for what you want. It's supremely handy!

And of course, you can post over on EdStem or just do some Google searching to see what you find.

Hope this helps, and happy $\LaTeX$ing!