02 Using LaTeX for Homework


I hope that you will use the LaTeX typesetting system to write your homework solutions for this course. (See the guide to submitting homework.) It might look daunting at first, if you haven't used LaTeX before, but you won't have to learn LaTeX in depth. You just have to fill in answers to exercises, so you won't need to deal with the complexities of writing complete documents. You will mainly need to know how to express mathematics in LaTeX. Unfortunately, matrices and systems of linear equations are among the more difficult things to write. Fortunately, you will have examples that you can imitate. What follows here is some basic information to get you oriented.

Using LaTeX is something like programming: You write "source code" in plain text, which is "compiled" to produce the actual document. Perhaps the first thing to know is that to start a new paragraph, you need to leave a blank line. Other than that, carriage returns are ignored. Also, multiple consecutive spaces are treated as a single space.

To Include mathematics in a document, you type the LaTeX source code for the math between dollar signs. For example, $ax^2+bx+c=0$ will be typeset as \(ax^2+bx+c=0\). If you enclose the code between double dollar signs, the math will be displayed on on line by itself. (This is called "displayed math.") For example, $$ax^2+bx+c=0$$ will be typeset as \[ax^2+bx+c=0\] Some things look different as displayed math. For example, compare \(\lim_{n\to\infty}\sum_{k=1}^n\frac{1}{2^n}=1\) to \[\lim_{n\to\infty}\sum_{k=1}^n\frac{1}{2^n}=1\] If case you are wondering, the LaTeX code for that is \lim_{n\to\infty}\sum_{k=1}^n\frac{1}{2^n}=1. Remember that it has to be enclosed between either single or double dollar signs. Stuff that is between dollar signs is said to be in math mode, as opposed to the regular "text mode."

In the source code, certain characters have special meaning: $ \ { } ^ _ & ~ # %. We have seen that $ is used to surround math code.

The backslash character, \, is used for LaTeX commands, and braces, { and }, are used to enclose the parameters to which a command applies. For example, in \sqrt{b^2-4ac}, the command is \sqrt, the parameter is b^2-4ac, and the typeset result is \(\sqrt{b^2-4ac}\). The \frac command is used to make a fraction, and it has two parameters, so \frac{n!}{k!(n-k)!} is typeset as \(\frac{n!}{k!(n-k)!}\). Some commands don't have parameters. For example, \infty represents the infinity symbol, \(\infty\), and \to produces a right arrow, \(\to\). Note that you might need to leave a space after a command like \to to mark the end of the command word. For example, use f\colon X\to Y to get \(f\colon X\to Y\). Typing \colonX instead of \colon X will just get you an error.

The special characters caret, ^, and underscore, _, are used for exponents and subscripts in math mode. Note that if an exponent or subscript is more than one character or command, it must be enclosed in braces, so you need f^{-1}(x_i) to get \(f^{-1}(x_i)\). Some similar things, such as the upper and lower limits on a sum (\sum_{n=1}^\infty for \(\sum_{n-1}^\infty\)) and the thing that goes below "lim" (\lim_{x\to a} for \(\lim_{x\to a}\)). By the way, in display math, that would look like \(\displaystyle\lim_{x\to a}\). If you want to force some math into display style, use the command \displaystyle in regular math mode: $\displaystyle\lim_{x\to a}$.

We will see that the amphersand, &, is used similarly to a tab character in tables and matrices. The other three special characters probably won't come up in your writing for this course. (A ~ represents a non-breaking space, # is used when defining new commands, and % marks the start of a comment that ends at the end of the same line. Comments are ignored by LaTeX.) Most of the special characters can be output to the typeset document by preceding the character by a backslash in the source code. For example, type \{ for \(\{\). However, this does not work for the backslash character; \\ is used to force a line break in the output. It also does not work for \^ or \~ (which are used for making accented characters). I also note that |, <, and > work in math mode but give funny results if used outside of math mode.


Some LeTeX code uses environments. Environments are coded using \begin and \end, which take the name of the environment as parameter. For example, you will write your solution to exercises in this course using my "answer" environment. That means that you will write your solution between \begin{answer} and \end{answer}. Similarly, I will write the problem text between \begin{problem} and \end{\problem}. (These two environments are not built into LaTeX. It is possible to define new environments and new commands in a document. The LaTeX source files that I give you will already define any extra commands and environments that you need.)

If you are going to use LaTeX in this course, you need to learn about matrix environments. To produce a matrix enclosed in parentheses, use the pmatrix environment. The rows of the matrix are separated by \\. In each row, the elements of that row are separated by &. All of that goes between \begin{pmatrix} and \end{pmatrix}, and it must all be in math mode or displayed math mode. LaTeX ignores end-of-lines in math mode, but it's a good idea to lay out the matrix in the source code similarly to how it looks when typeset (except I don't always do that for simple column vectors, which have just one element per row). For example,

        $$\begin{pmatrix}
            3  & -10 & 2 \\
            -1 &   7 & 4 \\
            5  &   0 & 1
        \end{pmatrix}
        \begin{pmatrix}
            x\\ y\\ z
        \end{pmatrix}
        =
        \begin{pmatrix}
           3x-10y+2z \\
           -x+7y+4z \\
           5x+z
        \end{pmatrix}$$

will produce this in the typeset document: \[\begin{pmatrix} 3 & -10 & 2 \\ -1 & 7 & 4 \\ 5 & 0 & 1 \end{pmatrix} \begin{pmatrix} x\\ y\\ z \end{pmatrix} = \begin{pmatrix} 3x-10y+2z \\ -x+7y+4z \\ 5x+z \end{pmatrix}\]

Later, we will use the vmatrix environment to make matrices that are enclosed between vertical bars instead of between parentheses. There is a plain matrix environment for typesetting the elements of the matrix with nothing enclosing them. I use the matrix environment to typeset systems of linear equations, where each equation is one row in the matrix. Each term in an equation is one element of the row. The equal sign and the right-hand side of the equation are also elements. A column can be left empty—two &'s in a row—if a term is missing from the equation. For example:

        $$\begin{matrix}
           3x_1 & -2x_2 &  +x_3 &  -x_4 & = & 7\\
           -x_1 &       & -5x_3 & +2x_4 & = & 2\\
                &   x_2 & +2x_3 &       & = & 0\\
           2x_1 & +3x_2 &       & -5x_4 & = & -1
        \end{matrix}$$

gives \[ \begin{matrix} 3x_1 & -2x_2 & +x_3 & -x_4 & = & 7\\ -x_1 & & -5x_3 & +2x_4 & = & 2\\ & x_2 & +2x_3 & & = & 0\\ 2x_1 & +3x_2 & & -5x_4 & = & -1 \end{matrix} \]

Unfortunately, there is no simple environment for making augmented matrices. I use a pattern based on the array environment, which makes it possible to put lines between columns of the array and to say whether you want the columns to be centered, left justified, or right justified. This is specified in an extra parameter that comes after \begin{array}. For example, \begin{array}{cccc|c} is used for an array with five centered columns and a line between the fourth and fifth columns; you can use r and l instead of c to get right-justified or left-justified columns. Since the array environment does not include parentheses, I have to add them myself: \left( and \right) add appropriately sized parentheses around whatever is put between them. Here's the augmented matrix that represents the above linear system:

          $$\left( \begin{array}{cccc|c}
             3 & -2 &  1 & -1 &  7\\
            -1 &  0 & -5 &  2 &  2\\
             0 &  1 &  2 &  0 &  0\\ 
            -2 &  3 &  0 & -5 & -1
          \end{array} \right)$$

giving \[\left( \begin{array}{cccc|c} 3 & -2 & 1 & -1 & 7\\ -1 & 0 & -5 & 2 & 2\\ 0 & 1 & 2 & 0 & 0\\ -2 & 3 & 0 & -5 & -1 \end{array} \right) \]

The only other environment that is really essential is align*. This environment is unusual in that it is used for mathematics, but it has to be used in regular text mode, not inside math mode. Its purpose is to vertically align a sequence of rows. Rows are separated by \\, and the position in each row that is to be aligned is marked with a &. Often, a row is an equation or inequality (or just the operator and the right half of an equation/inequality), with the operators vertically aligned. Here is an example:

       \begin{align*}
          \frac{d}{dx}(x\sin(x^2))  &= x\frac{d}{dx}(\sin(x^2)) + \sin(x^2)\frac{d}{dx}(x) \\
                                    &= x\cos(x^2)\frac{d}{dx}(x^2) + \sin(x^2)\\
                                    &= x\cos(x^2)2x + \sin(x^2)\\
                                    &= 2x^2\cos(x^2) + \sin(x^2)
       \end{align*}

which is typeset as

\(\begin{align*} \frac{d}{dx}(x\sin(x^2)) &= x\frac{d}{dx}(\sin(x^2)) + \sin(x^2)\frac{d}{dx}(x) \\ &= x\cos(x^2)\frac{d}{dx}(x^2) + \sin(x^2)\\ &= x\cos(x^2)2x + \sin(x^2)\\ &= 2x^2\cos(x^2) + \sin(x^2) \end{align*}\)

For showing a series of row reductions applied to a matrix or linear system, you might use array or matrix environments inside an align* environment. This can get complicated, but an example is given in the first homework assignment. I have defined the command \rowop for making the arrows that go between the steps of a row reduction. The parameter to \rowop is either a single row operation or several operations separated by \\. For example, \rowop{\rho_2\leftrightarrow\rho_3} and \rowop{2\rho_1+\rho_2\\-3\rho_1+\rho_3} for \(\rowop{\rho_2\leftrightarrow\rho_3}\) and \(\rowop{2\rho_1+\rho_2\\-3\rho_1+\rho_3}\)


You can find lists of other LaTeX command elsewhere, and you will find examples in the documents that I give you. Here are a few more that you might use in this course. Most of these are used only in math mode, and a few of them require some preliminary configuration in the LaTeX file.

LaTeX codeTypeset Output
\textbf{bold}, \textit{italic}, \texttt{monospace} bold, italic, monospace
$\subset, \subseteq, \supset,\supseteq$ \( \subset, \subseteq, \supset,\supseteq\)
$\{, \}, \in, \notin$, \cup, \cap, \varnothing$ \(\{, \}, \in, \notin, \cup, \cap,\varnothing \)
$\le,\ge,\ne,\approx$ \(\le,\ge,\ne,\approx \)
$\forall,\exists,\therefore$ \(\forall,\exists,\therefore\)
$\cdot, \dots, \cdots, \vdots, \ddots$ \(\cdot, \dots, \cdots, \vdots, \ddots \)
$\alpha, \beta, \gamma, \rho, \dots$ \( \alpha, \beta, \gamma, \rho, \dots\)
$\mathbb{R}, \mathbb{Q}, \mathbb{N}$ \(\mathbb{R}, \mathbb{Q}, \mathbb{N} \)
$\mathscr{A}, \mathscr{B}, \dots$ \(\mathscr{A}, \mathscr{B}, \dots \)
$\mathcal{A}, \mathcal{B}, \dots$ \( \mathcal{A}, \mathcal{B}, \dots \)
$f\colon A\to B$ \(f\colon A\to B \)
$\bigcup_{n=1}^\infty, \bigcap_{\alpha\in A}$ \(\bigcup_{n=1}^\infty, \bigcap_{\alpha\in A} \)
$\rightarrow,\Rightarrow,\longrightarrow$ \(\rightarrow,\Rightarrow,\longrightarrow \)
$\leftrightarrow,\Leftrightarrow$ \( \leftrightarrow,\Leftrightarrow\)

(back to contents)