













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive guide on using LATEX, a high-level language for rendering mathematical documents using TEX. It covers the basics of LATEX, including inline and display math mode, and provides examples for simplifying complex equations using various techniques. It also introduces the use of labels and equation numbers, as well as logical notation and spacing in LATEX.
Typology: Study Guides, Projects, Research
1 / 21
This page cannot be seen from the preview
Don't miss anything!














The CIS 160 Staff
1 Introduction
TEX is a typesetting system designed by Donald Knuth in 1978 that allows for platform-independent ren- dering of documents including mathematical formatting. In the early 1980’s, Turing Award Winner Leslie Lamport wrote LATEX (pronounced “lay-tek” or “luh-tek,” and not with an “eks” sound on the X) as a collection of TEX macros and a program that allows users to use a high-level language to render documents using TEX. If all this sounds complicated, don’t worry; the gist of it is that LATEX is a powerful tool that allows us to easily write nice-looking documents. So why learn LATEX? For one, it’s a relatively easy way to make anything you write (especially math) look beautiful! It’s also industry standard if you are considering publishing any papers in really any mathematical or science-related field (and even some others). Plus, it’s just a nifty tool that makes it easy to write documents that include mathematical formatting – once you get comfortable with it, you may find that you start using it for other classes, or even other activities! Also it’s required for CIS 160. The remainder of the guide will focus on LATEX for writing TEX documents (i.e. .tex files). But this isn’t the only way we use LATEX – some webpages will also allow you to write LATEX code and render it on the page! In particular, one of the most useful CIS 160 resources, the class Piazza site, accommodates LATEX code. We highly recommend you familiarize yourself with LATEX enough to incorporate it in your answers. Note that there are some subtle differences on Piazza – we enter mathmode by enclosing with double dollar signs ($$...$$) rather than single, and some packages are pre-loaded, while others can’t be used. There’s also a couple other differences, but for the most part, you won’t encounter any besides the dollar signs. Sometimes we will reference concepts to be covered in the course for exposition, so don’t be worried if you don’t understand something here (maybe if you still don’t understand it after the class is over). On a similar note, just because a concept appears here doesn’t mean that you can use it as justification for homework or exams (because we skip the proofs here!). There’s a lot of detail here, and we’ll aim to cover everything you need for CIS 160, plus a little more just in case. But we have faith that you’ll pick it up quickly once you actually start writing. With a bit of practice, you’ll be creating beautiful documents with well-formatted mathematical expressions in no time!
2 LATEX Basics
First off, you’re going to need to set up a LATEX editor. We recommend following the setup guide found on our site. You’ll also find there an interactive guide to LATEX, which we also recommend browsing through. Once you’ve gotten all set up, the rest of this guide will be much more helpful! Your LATEX editor will probably have a text editor (for editing .tex files), and an output display (which generates the output from the .tex input, usually in the form of a PDF). The structure of a .tex document will generally be a preamble followed by the document body. The preamble will contain any packages used, macros defined, as well as some general information about the document (title, author, etc.). To import pack- ages, you’ll use the \usepackage{packagename} command; we’ll cover defining macros later, but you proba- bly won’t ever need them. The document itself will be enclosed in \begin{document} ... \end{document} tags. This is the portion that will be rendered in the final result. In general, you won’t have to mess with preambles too much (just the document body). One thing to watch out for – you should always make sure to write your name and recitation number where specified in the homework templates! In normal text mode, most text will appear essentially how you want it to; some special symbols will give you problems. Where LATEX really makes its mark is in mathmode (Section 3.1). There’s a few special characters you should be aware of: the backslash () is used to denote commands or escape special characters; the dollar sign ($) is used to enter and leave inline mathmode; and the percent sign (%) is used to comment the remainder of the line. Additionally, curly braces ({}) are used to tell LATEX that “these things belong together” and to prevent splitting them up.
3 Environments
In general, environments are used to provide a specific form of formatting. Most environments use in the following format (notable exceptions will be duly noted):
In some cases, inline math mode will look unappealing – consider
∑n i=1 i. How can we solve this? We use display math mode! For single-line math mode statements, you can enclose the desired text in [...] opening and closing tags (note that this is another exception to the aforementioned format). This will put the contents in their own line in display math mode (which is generally nicer than inline). It’s typically a good idea to use this when you have a longer expression you want to put in math mode, to avoid cluttering up paragraphs with excessive scary-looking notation. For example, the text:
Input:
We recall the equation $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$. In this case, we have $a = 1, b = -5, c = -24$. Plugging these values in, we find $x = -3$ and $x = 8$ as our solutions.
Output: We recall the equation^ x^ =^
−b±√b^2 − 4 ac 2 a.^ In this case, we have^ a^ = 1, b^ =^ −^5 , c^ =^ −24. Plugging these values in, we find x = −3 and x = 8 as our solutions.
Using the brackets for math mode for the first bit of math will result in the following:
Input:
We recall the equation [ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} ]. In this case, we have $a = 1, b = -5, c = -24$. Plugging these values in, we find $x = -3$ and $x = 8$ as our solutions.
Output:
We recall the equation
x =
−b ±
b^2 − 4 ac 2 a In this case, we have a = 1, b = − 5 , c = −24. Plugging these values in, we find x = −3 and x = 8 as our solutions. The fully expanded expression draws emphasis and is much easier to read (the fraction, in particular, is much larger; it’s also easier to distinguish the ±). We often use display-style for this very purpose, as well as to break up large chunks of text. If you want to use inline math mode, but with display-style formatting, you can use the command \displaystyle immediately before the argument. For example:
$\sum_{i=1}^n i$ will render as
∑n i=1 i.
$\displaystyle\sum_{i=1}^n i$ will render as
∑^ n
i=
i.
Another common application of \displaystyle is for fractions (although there is a separate shortcut for this). Note that using \displaystyle might mess up your line spacing – this is okay!
The align environment is a powerful tool for grouping together series of equations, and perhaps the most common environment you will encounter in CIS 160. The align environment essentially takes all the lines given as inputs as individual mathmode lines, and aligns them based on the positions of the special align character – your friendly neighborhood ampersand (&). To see how we can use this environment, consider the following problem. Suppose we want to show that (x + 1)(x + 2)(x + 6) = x(x + 4)(x + 5) + 12. So our first attempt might look something like this:
Input:
$(x + 1)(x + 2)(x + 6) = (x^2 + 3x + 2)(x + 6) = x^3 + 9x^2 + 20x + 12 = x(x^2 + 9x + 20) + 12 = x(x^2 + 4x + 5x + 20) + 12 = x(x(x + 4) + 5(x + 4)) + 12 = x(x + 4)(x + 5) + 12$
Output: (x + 1)(x + 2)(x + 6) = (x^2 + 3x + 2)(x + 6) = x^3 + 9x^2 + 20x + 12 = x(x^2 + 9x + 20) + 12 = x(x^2 + 4x + 5x + 20) + 12 = x(x(x + 4) + 5(x + 4)) + 12 = x(x + 4)(x + 5) + 12
Wow, that is... kind of a mess. There’s a lot of math happening in not much space. To make this readable, we now try the following snippet, where each line is in display math mode:
Input:
[(x + 1)(x + 2)(x + 6) = (x^2 + 3x + 2)(x + 6)] [= x^3 + 9x^2 + 20x + 12] [= x(x^2 + 9x + 20) + 12] [= x(x^2 + 4x + 5x + 20) + 12] [= x(x(x + 4) + 5(x + 4)) + 12] [= x(x + 4)(x + 5) + 12]
Output:
(x + 1)(x + 2)(x + 6) = (x^2 + 3x + 2)(x + 6) = x^3 + 9x^2 + 20x + 12 = x(x^2 + 9x + 20) + 12 = x(x^2 + 4x + 5x + 20) + 12 = x(x(x + 4) + 5(x + 4)) + 12 = x(x + 4)(x + 5) + 12
This is a bit better, as there’s more room – each line indicates a single logical step. But note that each of the lines is centered. Since they have different lengths, this results in a wobbly and somewhat disorienting display (especially if the difference is greater than in our example). To remedy this, we use the align environment!
Input:
\begin{align} (x + 1)(x + 2)(x + 6) & = (x^2 + 3x + 2)(x + 6) \ & = x^3 + 9x^2 + 20x + 12 \ & = x(x^2 + 9x + 20) + 12 \ & = x(x^2 + 4x + 5x + 20) + 12 \ & = x(x(x + 4) + 5(x + 4)) + 12 \ & = x(x + 4)(x + 5) + 12 \end{align}
Output:
(x + 1)(x + 2)(x + 6) = (x^2 + 3x + 2)(x + 6) = x^3 + 9x^2 + 20x + 12 = x(x^2 + 9x + 20) + 12 = x(x^2 + 4x + 5x + 20) + 12 = x(x(x + 4) + 5(x + 4)) + 12 = x(x + 4)(x + 5) + 12
Much neater! We use \begin{align} ... \end{align} here to avoid equation numbering; if we leave off the asterisks, then each equation will be numbered. Note that the equations are all lined up so that the &’s are all in the same vertical location (though they don’t actually appear in the text). Also note that we have to manually include a line break (\) after each line – otherwise, the the environment won’t split
Input:
\begin{align} (x + 1)(x + 2)(x + 6) &= (x^2 + 3x + 2)(x + 6) \ &= x^3 + 9x^2 + 20x + 12 \ \intertext{ Note that we can pull out an $x$ from the first three terms: } &= x(x^2 + 9x + 20) + 12 \ &= x(x^2 + 4x + 5x + 20) + 12 \ &= x(x(x + 4) + 5(x + 4)) + 12 \ &= x(x + 4)(x + 5) + 12 \end{align}
Output:
(x + 1)(x + 2)(x + 6) = (x^2 + 3x + 2)(x + 6) = x^3 + 9x^2 + 20x + 12
Note that we can pull out an x from the first three terms:
= x(x^2 + 9x + 20) + 12 = x(x^2 + 4x + 5x + 20) + 12 = x(x(x + 4) + 5(x + 4)) + 12 = x(x + 4)(x + 5) + 12
So our alignment is preserved through the comment! Also note that within the intertext environment, we’re automatically in text mode (but can enter math mode normally). For shorter notes, another options is viable – the \tag{...} command, which will appear in parentheses inline, aligned right, and in text mode:
Input:
\begin{align} (x + 1)(x + 2)(x + 6) &= (x^2 + 3x + 2)(x + 6) \ &= x^3 + 9x^2 + 20x + 12 \ &= x(x^2 + 9x + 20) + 12 \tag{ Pulling out an $x$ } \ &= x(x^2 + 4x + 5x + 20) + 12 \ &= x(x(x + 4) + 5(x + 4)) + 12 \ &= x(x + 4)(x + 5) + 12 \end{align}
Output:
(x + 1)(x + 2)(x + 6) = (x^2 + 3x + 2)(x + 6) = x^3 + 9x^2 + 20x + 12 = x(x^2 + 9x + 20) + 12 (Pulling out an x) = x(x^2 + 4x + 5x + 20) + 12 = x(x(x + 4) + 5(x + 4)) + 12 = x(x + 4)(x + 5) + 12
Note that using the \tag{} will remove numbering from that line. To suppress numbering on an individual line if numbering is active (in the non-starred environment), add \nonumber to the end of the line.
Input:
\begin{align} (x + 1)(x + 2)(x + 6) &= (x^2 + 3x + 2)(x + 6) \nonumber \ &= x^3 + 9x^2 + 20x + 12 \tag{expanding}\ &= x(x + 4)(x + 5) + 12 \end{align}
Output:
(x + 1)(x + 2)(x + 6) = (x^2 + 3x + 2)(x + 6) = x^3 + 9x^2 + 20x + 12 (expanding) = x(x + 4)(x + 5) + 12 (1)
If you want to refer to specific equations, equation numbers can be helpful; otherwise, we usually leave them off to avoid clutter. To mark something for later reference, use \label{XYZ}; then use \ref{XYZ} to refer to it later (this also adds a link in digital versions). Adding labels will be helpful in case you end up changing the document later; you won’t have to correct any hard-coded labels.
Input:
\begin{align} (x + 1)(x + 2)(x + 6) &= (x^2 + 3x + 2)(x + 6) \nonumber\ &= x^3 + 9x^2 + 20x + 12 \tag{expanding} \ &= x(x + 4)(x + 5) + 12 \label{eqn:result} \end{align} So from equation \ref{eqn:result} , we see that...
Output:
(x + 1)(x + 2)(x + 6) = (x^2 + 3x + 2)(x + 6) = x^3 + 9x^2 + 20x + 12 (expanding) = x(x + 4)(x + 5) + 12 (2)
So from equation 2, we see that...
Labels can be defined in math mode or text mode and can also be useful for generally linking throughout the document (it’s how we make the links between sections in this guide, for example).
Sometimes, you just want a little bit of space. Maybe you’re working on a lemma and want to empha- size that the proof is separate from the proof of the main result. In this case, you’ll likely use the the \begin{quote} ... \end{quote} or \begin{quotation} ... \end{quotation} environments. The two are almost identical, except for some minor differences in indentation and spacing; it’s basically a matter of preference. Both environments will increase the margins a bit on both sides, providing a slightly narrower output (nice for smaller results).
Numbered lists are a great way to organize your ideas and keep track of thoughts. They’re also useful for, say, labelling homework problems. Thankfully, LATEX comes with this capability built-in, through the \begin{enumerate} ... \end{enumerate} environment. Within this environment, use \item to advance the numbering; otherwise, this is just like writing normally. You can also nest these environments; LATEX will automatically choose appropriate labels, and keep track of the nesting on its own
Input:
\begin{itemize} \item Category 1 \begin{itemize} \item Example 1 \item Example 2 \end{itemize} \item Category 2 \begin{itemize} \item Example 3 \item[;] Example 4 \begin{itemize} \item Explanation \end{itemize} \end{itemize} \end{itemize}
Output:
Again, note that we can assign individual items specific bullets using the square brackets.
The \begin{cases}...\end{cases} environment essentially creates a self-contained miniature align* en- vironment, with a large left brace which scales to encompass the entire selection. This environment can only be used if you’re already in math mode. You’ll probably use this most commonly for piecewise functions. For example, consider the following function:
Input:
f(x) = \begin{cases} x^3+1 &\text{if } x \leq 0 \ x^2 &\text{if } x > 0 \end{cases} ]
Output: (^) f (x) =
x^3 + 1 if x ≤ 0 x^2 if x > 0
Note that you can extend this to as many lines as you want, and, just like with the align* environment, include multiple columns of alignment (the same spacing rules apply).
4 Images and Figures and Diagrams, Oh My!
You may, in the course of your writing, find it necessary to include supplementary figures or diagrams. Perhaps a particular graph example can be drawn out to emphasize a point or provide an example. Or maybe you just happened to take an amazing selfie and needed somewhere to share it. Luckily, LATEX makes it fairly straightforward to do so. You’ll need to import the graphicx package, by including \usepackage{graphicx} somewhere in your preamble. Once you do, you can use \includegraphics{filename} to insert images into your document. You don’t need to specify the extension. If you want to use an image in a different directory, you can also specify the full path. You can also provide additional scaling and rotation arguments in brackets; for example \includegraphics[scale=1.5, angle=45]{filename} to scale and rotate the image or \includegraphics[height=2cm, width=\textwidth]{filename} to specify both dimensions (if you only give one, the other will scale to maintain aspect ratio). So this gets your image on the page. But that doesn’t mean it’ll look nice. While LATEX generally gives you a lot of flexibility on how you lay out your document, it really likes to try to place images on its own (as an aside, LATEX is really particular about not breaking things up, which is why you will often see images ending up flying around the document). While it generally renders objects in the order it encounters them, its image placement mechanism often does not, which can lead to a great deal of puzzlement. To help you deal with this, we’ll teach you a magic word: !htbp. But how do we use this magical power?, you might ask. We’ll enclose relevant images (or text) in a \begin{figure} ... \end{figure} environment. Here’s the basic anatomy of a figure environment:
Input:
\begin{figure}[!htbp] \centering % centers the figure; default alignment is left \includegraphics{my-picture} \caption{Caption} % optional, will display below the picture \label{fig:my_label} % optional, allows \ref to link to the figure \end{figure}
LATEX will automatically keep track of the figure numbers and add the appropriate label to the caption. If you want the caption to appear above the image, just flip the order of these commands. So that’s all well and good. But what does the “!htbp” do? Well, the! first tells LATEX to ignore its usual rules for trying to place things. The remaining letters define the priority of different locations. LATEX will first try to place the image “here,” that is, as close to the given location in the text as it can. Failing this, it’ll go to the top of the page, then the bottom, or a new page. This can be one of the most finicky aspects of using LATEX, so don’t be afraid to ask questions. And in the long run, it really doesn’t matter where your figures end up, so long as we can grade them (you may want to indicate where to find them if it’s not obvious).
Tables can be a great way of organizing information... for example, truth tables. LATEX tables use the \begin{tabular} ... \end{tabular} environment, which requires using alignment characters (&) and line breaks (\) appropriately. Each cell will be in text mode by default, although you can enter mathmode as well. As an alternative, you can use the \begin{array} ... \end{array} environment, which is in mathmode by default. You will have to specify also the number of columns, alignment of each column (l eft, center, r ight), and placement of vertical bars immediately after the \begin{tabular} tag. Also note that any horizontal lines must be manually entered with \hline, which appears at the beginning of its line.
x – nth^ root.
∑n k=1 – summation (see 7.5 for more details).
∏n k=1 – product.
⋃n k=1 – union over indices.
⋂n k=1 – intersection over indices.
Set Notation:
Note that some concepts can be written multiple ways. If unsure, stick to the one presented in class; because of different conventions, you might encounter alternative notations, so we list some of those here.
Logical Notation:
Miscellaneous Notation:
These commands will require you to use the amsmath package (include the line \usepackage{amsmath} in the file preamble); for the most part, this will be in our default preamble, so you won’t need to worry about it.
(n k
x y
6 General Proof-Writing Tips
This section contains a brief list of tips and advice on how to write effective and clean proofs. A major focus of the course will be teaching you how to write proofs, so don’t worry about this too much initially. We’ve included here a couple good general rules to follow to steer your writing efforts and save yourself (and possibly your grader!) some headaches. Note that we do grade for justification as well; a correct answer alone is usually insufficient for full credit. Some of these tips are for specific techniques or methods; they’ll make more sense as we cover the relevant material in class.
Xi before you apply LOE.
7 Advanced LATEX
This is more of an “extra credit” section for those interested in exploring a bit more of what LATEX really has to offer. You won’t necessarily need to use these for CIS 160, but they can be effective formatting tools.
LATEX also gives you the ability to define your own commands. If you find yourself repeatedly using the same notation, but not wanting to type it out every time, you can define a handy dandy macro to do it for you! Or you can define a custom command to get some formatting juuust right, in a way that the standard packages don’t account for. An example of the first case would be as follows: Say you happen to be dealing with the real numbers R a lot and don’t feel like writing out $\mathbb{R}$ every time you want to use it. Instead, you can define:
Input: \newcommand{\R}{\mathbb{R}}
near the top of your document and just write $\R$ whenever you need it, saving you seconds each time! Or maybe you want to redefine some already existing notation. If you want to use a shorter arrow for implication, you might include in your preamble:
Input: \renewcommand{\implies}{\Rightarrow}
Then, instead of using =⇒, LATEX will render \implies as ⇒. Maybe you want to use some notation that isn’t already implemented (or at least, you can’t find any existing commands online). In this case, we can define our own notation using custom commands! We’ll use \genfrac as an example here (you won’t need to be too familiar with this, since most commands you need will already be defined for you).
Input:
\newcommand{\multichoose} [2] {\left(!\genfrac{(}{)}{0pt}{0}{ #1 }{ #2 } !\right)}
$\multichoose{3}{1}$
Will now give us:
. Observe that we supply the number of arguments within the brackets; then
refer to the arguments within the command using the # symbol and index of the argument.
Toggles can be useful to switch the document between two different “modes”. Toggling can be convenient, but keep in mind that you’re working with two versions of the same document at once, particularly if there’s sensitive information that you don’t want appearing.
Input:
\left| \bigcup_{i=1}^n S_i \right| = \sum_{k=1}^n (-1)^{k-1} \sum_{J \subseteq [1..n], |J| = k } \left| \bigcap_{j \in J} S_j \right| ]
Output:
⋃^ n
i=
Si
∑^ n
k=
(−1)k−^1
J⊆[1..n],|J|=k
j∈J
Sj
One note here: if you have a lot of nested parenthesis, you may want to manually size them, since \left and \right won’t change sizes for nesting. Usually, to use these, you will have to match them up. Sometimes, you will only have one symbol (say, a large brace you want to indicate a series of equations). In this case, you can use. (such as \left.) to fill in for the “missing” symbol and let LATEX deal with the sizing (the aligned environment just makes a mini-align within mathmode).
Input:
\left. \begin{aligned} x &= 1, y = 3 \ y &= 2, y = 1 \end{aligned} \right} \text{Candidate solutions} ]
Output:
x = 1, y = 3 y = 2, y = 1
Candidate solutions
As we may have mentioned before, LATEX gives you a lot of flexibility in laying out your document. One key to this end is a series of commands that allow you to adjust spacing however you like. The most specific of these are the \hspace{dist} and \vspace{dist} which add in horizontal and vertical spacing, respectively, of the provided distance. Distance can be provided in various units (most commonly: in, cm, em (roughly the size of an “M”, pt). We will commonly use the line break (\), especially in align or similar multiple-line environments. You can use \newline or \newpage, which do about what you’d expect them to. It’s also possible to customize the distance for a line break, by using \[dist] – this is especially useful for helping space out tables with complicated expressions. There’s also a couple spacing commands (both in mathmode and text) for tweaking to get nice cushions, in increasing order: ! , : ; \ \quad \qquad. Note that ! is actually negative spacing – less than not writing anything! Using these in, for example
{x^2 \ + \ 1 \ | \ x \ \in \ \mathbb{N} \ }
will give the following results, in increasing order:
Finally, the ∼ symbol adds a non-breaking space. LATEX usually justifies the text; if you throw in one of these bad boys, then the resulting space will never be broken by a newline (this can also be useful for forcing a little distance between things which are too close together).
Summations pop up a lot in 160 material – it’d be hard to count without them! The general format for writing summations is (note the mathmode) is:
Input: [ \sum_{k=1}^n k^2 ]
Output:
∑^ n
k=
k^2
Depending upon the intent, you may prefer to use inline summations
∑n k=1 k
(^2) or display style summations ∑^ n
k=
k^2 (use \sum\limits_{k=1}^n or \displaystyle\sum_{k=1}^n instead). Inline style can be useful to
keep spacing consistent, but can sometimes be more difficult to read. If you are in display mode already (that is, using [ ... ] or similar), then you will automatically be in display style. Everything above also applies for the
product notation, except with \prod in place of \sum and similarly for \bigcup (
) and \bigcap (
It might be the case that the conditions on the sum are kind of bulky and stretch things out a bit. For example, recall the formula for PIE:
Input:
\left|\bigcup_{i=1}^n S_i\right| = \sum_{k=1}^n (-1)^{k-1}\sum_{ J \subseteq [1..n], |J| = k } \left| \bigcap_{j \in J} S_j \right| ]
Output:
⋃^ n
i=
Si
∑^ n
k=
(−1)k−^1
J⊆[1..n],|J|=k
j∈J
Sj
Observe that we’re using the sizing commands from above. Note the decently large gap made by the big summation condition. We can tell LATEX to stack the relevant conditions as follows:
Input:
\left|\bigcup_{i=1}^n S_i\right| = \sum_{k=1}^n (-1)^{k-1} \sum_{ \substack{ J \subseteq [1..n] \ |J| = k } } \left| \bigcap_{j \in J} S_j \right| ]
Output:
⋃^ n
i=
Si
∑^ n
k=
(−1)k−^1
J⊆[1..n] |J|=k
j∈J
Sj