Day 4 - $\LaTeX$ for Fun and Profit

Video

Motivation

Back in the 70s, famous mathematician and computer scientist Donald Knuth was writing his Magnum Opus - The Art of Computer Programming - to this day considered one of the fundamental texts in the genre.

Unfortunately, he was frustrated with the options for writing mathematics - which honestly were awful - and so he wrote some systems to help him write his book. These systems became $\TeX$, the precursor to almost all math typesetting in the modern world.

$\TeX$ saw widespread adoption and expansion, and the version we are going to be using - AMS packages in $\LaTeX$ - quickly became standard for mathematicians the world over.

As for Prof. Knuth, he is still a respected mathematician and still writing that book. 40+ years later, TAoCP is now nearly half done, and you will often find algorithms and techniques referred to by their numbering in his book. If you are serious about computer science, or about programming as a profession, I cannot recommend picking up a copy of the first few chapters enough.

Why $\LaTeX$?

Most people in this course are already to some degree familiar with $\LaTeX$, but we will briefly touch on the elephant in the room - what makes $\LaTeX$ special?

  • Separation of Content and Format: writing a document involves specifying all the important things, and none of the markup.
  • Quality Output: The default settings are able to produce professional-quality documents, including padding and kerning.
  • Versatility: With practice, you can create almost arbitrarily complicated expressions with relative ease. If you can write it on paper, you can often write it in $\LaTeX$.
  • Extensability: you can add in extensions for almost anything, and write new macros and commands on the fly to streamline complicated constructions.

Basic Structure

$\LaTeX$ is, at its core, a document markup language - like HTML. Similar to HTML, it typically has three main components:

Declaration

\documentclass{article}

The declaration says what kind of file the document is; some common useful ones include:

class explanation
article The default document. Produces pages of text with standard formatting, no frills. Great for simple content, or preparing a piece for inclusion in a larger work.
standalone The simplest document class, it produces your content in a minimal window. Great for getting a sharable, embeddable image of a small portion of $\LaTeX$.
report An expanded article, intended for portions with multiple chapters - such as your notes for a class, or your thesis.
book An expanded report, includes even more metacontent. Designed for print and e-books.
beamer The most common presentation format. Compiles to slides, allowing a relatively trouble-free presentation experience. Never fail to launch PowerPoint again!
letter Designed for writing correspondence.

The header, in most markup languages, is where you import packages/libraries, define variables, and set up everything you need to set up before you get to the content.

In $\LaTeX$, for typesetting mathematics, there are some packages you are almost always going to want to use:

\usepackage{amsmath, amsthm, amssymb}

These official publications of the AMS provide you with a wide variety of essential math commands.

For calculus classes or certain fields, another very useful package is

\usepackage{physics}

Which does similar things for common operators in physics such as \div and \curl. When you are importing packages without options (which we will explain later), you can combine them in one \usepackage command for brevity.

There are a wide variety of $\LaTeX$ packages you may want to use in various situations - too many to list here - but you can find a good partial list on wikibooks. We will go over some more specific packages and their usage over the $\LaTeX$ portion of the course.

For now, these packages are all we need. Our document looks like:

\documentclass{article}
\usepackage{amsmath,amsthm,amssymb,physics}

Body

The third and final portion of a typical markup is the body. In $\LaTeX$, the body is done using an envirionment called document - which we will cover in more detail in the next session. Similar to enclosing tags in HTML, an environment wraps content:

\begin{document}
An Example \LaTeX document.
\end{document}

Math mode

There are two primary "modes" within the body: Text mode and math mode.

Text mode, the default, formats full lines and paragraphs but cannot handle special math formatting such as x^2 or y_1.

Math mode formats everything within it as if it is a mathematical expression, and characters are being multiplied as variables. To place math mode within a line, enclose it in single $s: for example, $x^2$ is wrtten $ x^2 $.

There are also full line math evaluations, for example:

[ \sqrt{2} ]

is written \[ \sqrt{2} \].

You can even place text-mode content inside math mode - $\sqrt{ \text{Text Here} }$ produces $\sqrt{ \text{Text Here} }$.

Most of the $\LaTeX$ commands only work in one mode or the other. Check your compiler errors if your document doesn't compile - often, this is the problem.

Some environments - which we will touch on next course - will enter or exit math mode.

Compiling

We now have our first $\LaTeX$ document: the declaration, header, and body. But it isn't very pretty to look at.

The last thing to do is compile.

We have constructed our first file:

latex_first_steps.tex

\documentclass{article}
\usepackage{amsmath,amsthm,amssymb,physics}
\begin{document}
An Example \LaTeX document.
\end{document}

Navigating to the location in a terminal, and running:

pdflatex latex_first_steps.tex

compiles the written $\LaTeX$ into a pdf that you can view and share.

Continue to the worksheet to experiment with basic $\LaTeX$.

When you are done, you can try to move on to further content in the $\LaTeX$ module.

Department of Mathematics, Purdue University
150 N. University Street, West Lafayette, IN 47907-2067
Phone: (765) 494-1901 - FAX: (765) 494-0548
Contact the Webmaster for technical and content concerns about this webpage.
Copyright© 2018, Purdue University, all rights reserved.
West Lafayette, IN 47907 USA, 765-494-4600
An equal access/equal opportunity university
Accessibility issues? Contact the Web Editor (webeditor@math.purdue.edu).