# Introduction to LaTeX: 2. Typing Math

## Text and math modes (review from Part 1)

TeX has three basic modes: a text mode, used for typesetting ordinary text, and two types of math modes, an ordinary math mode for math formulas set "inline", and a display math mode, used for displayed math formulas. At any given point during the processing of a document, TeX is in one of those three modes. The behavior of TeX depends on the mode it's in. For example, certain characters (like the underline or caret symbols) are only allowed in a math mode, while others (like the "greater than" symbol) take on completely different meanings, depending on whether TeX is in text or in math mode. (Try this: write some ordinary text that includes the string ">From" (which is often generated by email software), and see what the ">" symbol becomes after compiling the document. In math mode, by contrast, ">" does what you'd expect: it serves as the "greater than" symbol.)

Text mode. This is the normal, or default, mode of TeX. TeX stays in that mode unless it encounters a special instruction that causes it to switch to one of the math modes, and it returns to text mode following a corresponding instruction that indicates the end of math mode.

Ordinary (inline) math mode. Mathematical material to be typeset inline must be surrounded by single dollar signs. For example: "$a^2 + b^2 = c^2$". The single dollar signs surrounding this expression cause TeX to enter and exit (ordinary) math mode.

Display math mode. Material that is surrounded by a pair of escaped brackets ("$" and "$"), or by "equation environments" such as \begin{align}$$...$$\end{align}, or $$...$$ is being processed by TeX in "display math mode." This means that the expression enclosed gets displayed on a separate line (or several lines, in case of multiline equations). Longer mathematical formulas and numbered formulas are usually "displayed" in this manner. Note that the commands for entering and leaving display math mode are distinct (\begin{...} or $for entering and \end{...} or$), in contrast to the ordinary math mode, where a single dollar sign serves both as entry and exit command. This allows for better error checking. (This is a major difference between LaTeX and AmSTeX or Plain TeX. In the latter two TeX versions, a double dollar sign () is used to indicate the beginning and end of display math mode. While the double dollar sign (still) works in LaTeX, it is not part of the "official" LaTeX command set (in fact, most books on LaTeX don't even mention it) and its use is discouraged. Use the bracket pair "$", "$" instead.)

## Basic math

Elementary arithmetic operations: The plus (+), minus (-), division (/) symbols have the usual meaning. To denote multiplication explicitly (this is rarely necessary), use \cdot (producing a centered dot) or \times (producing an "x"). The "equal", "less than", and "greater than" symbols on the keyboard work as expected; to get "less than or equal", use "\le"; similarly, "\ge" gives "greater than or equal".

Square roots: Square roots are generated with the command \sqrt{...}. For example, $z=\sqrt{x^2+y^2}$.

Subscripts and superscripts: These are indicated by carets (^) and underscores (_), as in $2^n$ or $a_1$. If the sub/superscript contains more than one character, it must be enclosed in curly braces, as in $2^{x+y}$.

Fractions and binomial coefficients: Fractions are typeset with $\frac{x}{y}$, where x stands for the numerator and y for the denominator. There is a similar construct $\binom{x}{y}$ for binomial coefficients. (The latter is part of the amsmath enhancements which you get when using "amsart" as documentclass.)

Sums and integrals: The symbols for sums and integrals are \sum and \int, respectively. These are examples of "large" operators, and their sizes are adjusted by TeX automatically, depending on the context (e.g., inline vs. display math). Note that the symbol generated by \sum is very different from the "cap-Sigma" symbol, \Sigma; the latter should never be used to denote sums. TeX uses a simple, but effective scheme to typeset summation and integration limits: Namely, lower and upper limits are specified as sub- and superscripts to \sum and \int. For example, $\sum_{k=1}^n k = \frac{n(n+1)}{2}$. (Note that the "lower limit" "k=1" here must be enclosed in braces.)

Limits: The "subscript" trick works also for limits; "\lim" produces the "lim" symbol, and the expression underneath this symbol (for example, "x tends to infinity") is typeset as a subscript to \lim: $\lim_{x\to\infty}f(x)=0$. Here "\to" produces the arrow, and "\infty" (note the abbreviation - \infinity does not work!) produces the "infinity" symbol. "\limsup" and "\liminf" work similarly, as do "\sup" and "\inf" (for supremum and infinimum), and "\max" and "\min" (for maximum and minimum). For example, $\max_{0\le x\le 1}x(1-x)=1/4$.

Exercise 2.1: Continuity of a function f(x) at a point x=c can be defined in terms of a limit: "f(x) is continuous at x=c if lim .....". Fill in the blanks and typeset the statement first inline, and then with the "lim ..." formula displayed on a single line. Observe how TeX typesets the limit differently, depending on the context.

Exercise 2.2: Typeset the binomial theorem (giving an expansion for (x+y)^n) in TeX, first as an "inline" formula (enclosed in a pair of single dollar signs), then as a displayed formula (enclosed in a pair $,$). Compile the TeX file, and observe the differences in the appearance of the output of the inline and the displayed formulas.

Operators: TeX has commands for common mathematical "operators" or "functions", such as \sin, \cos, \log, \ln, \exp, \arctan, etc. You should always use these commands instead of simply typing "sin", "cos", etc., without the backslash. Using the TeX commands ensures that the operators get typeset in the proper font and takes care of the spacing surrounding these operators.

Exercise 2.3: Typeset the addition formula for the sine: sin(x+y) = sin x cos y + cos x sin y, first using the proper TeX commands \sin and \cos and then by just typing sin and cos without the backslash. Observe the difference.

## More math

Greek letters and other special characters: The commands for Greek letters are easy and intuitive: Just type $\epsilon$, $\delta$, $\nu$, $\phi$, etc. To get upper case versions of these letters, capitalize the appropriate command; e.g., $\Delta$ gives a "cap-Delta" (which looks like a triangle). The most common notation for the reals, rationals, and integers involves the so-called "blackboard bold" font; to get these symbols use \mathbb{...} (in math mode): $\mathbb{R}$, $\mathbb{Q}$, $\mathbb{Z}$. Similarly \mathcal{...} produces a symbol in "script" or "caligraph" font, often used to denote sets: For example, $\mathcal{A}$ generates "script A".

Parentheses: The symbol pairs (), [], and \{ \} (note the backslash!) generate round, square, and curly parentheses in normal size. They work fine in math mode, but mathematical expressions often look better if the parentheses are enlarged to match the size of the expression. There are ways to manually enlarge these parentheses (by preceding the symbol with a command like \big, \bigg, \Big, etc.), but one rarely has to use these, since TeX can (in most cases) automatically size parentheses. To let TeX do the sizing, precede the left brace by \left, and the right brace by \right. This also works for other parentheses-like constructs, such as the absolute value symbol "|". Here is an example:


$\left|\sum_{i=1}^n a_ib_i\right| \le \left(\sum_{i=1}^n a_i^2\right)^{1/2} \left(\sum_{i=1}^n b_i^2\right)^{1/2}$


Exercise 2.4: Typeset the above expression and look at the output. (It's a famous mathematical theorem!). Then remove, or comment out, one of the bracket expressions (say, one instance of "\left("), and see what error messages you get. All bracket expressions generated by \left.. or \right.. must occur in pairs, and TeX gives an error message if this is not the case. (The left and right brackets don't have to be of the same type; for example, \left$\frac{3}{4}, \frac{4}{5}\right[ to denote the half-open interval [3/4, 4/5[ is perfectly legal.) ## Displayed equations Single line displays: To get a single line, displayed equation (without equation number), just use the pair "\[", "$". If you want TeX to automatically number the equation, use instead the $$...$$ environment. (The asterisk variant, \begin{equation*}$$...$$\end{equation*}, turns off the equation numbering, and is equivalent to typing $$$...$$$.) Multi-line equation environments: Things get more complicated if you have multiline equations that need to be lined up at suitable places. For most situations, the \begin{align} ... \end{align} environment, and its variant \begin{align*} ... \end{align*}, are sufficient. As with the equation environment, the asterisk version does not automatically number equations. The use of align is best illustrated with an example:  \begin{align} (a+b)^3 &= (a+b)^2(a+b)\\ &=(a^2+2ab+b^2)(a+b)\\ &=(a^3+2a^2b+ab^2) + (a^2b+2ab^2+b^3)\\ &=a^3+3a^2b+3ab^2+b^3 \end{align}  Here a double backslash (\\) is used to separate the lines, and an ampersand symbol (&) is used to indicate the place at which the formulas should be aligned. You can include more than one ampersand symbol per line to specify alignment at multiple columns, but the number of alignment symbols must be the same for each line of the display. Multiple alignments are rarely needed; in almost all cases a single alignment symbol, usually placed right before an equality (or inequality) sign, is enough. Exercise 2.5: Typeset the above multiline equation, compile it, and look at the output on the screen. Also, make some intentional mistakes (like leaving out the ampersand symbol, or leaving out one of the \\'s), and see what kind of error messages you get. Errors in multiline displays are among the most difficult to track down and diagnose. ## Spacing in math mode In math mode (both ordinary and display math), TeX decides on spacings between symbols in math mode, using rather sophisticated algorithms; in particular, any blank spaces inside math mode are ignored, For example, the formula "a^2 + b^2 = c^2$could have been typed as "$a^2+b^2=c^2\$", or even placed on two different lines, without any difference in the output. Letting TeX figure out the spacings almost always results in very good looking output, and you should avoid putting explicit spaces into mathematical formulas. However, there are a few situations where one does need appropriate spacing instructions. For those cases, there is a standard spacing command, "\quad" which generates the right amount of horizontal spacing to separate two equations on the same line, or a formula from an associated range or condition (such as "n=2,3,...") that is given on the same line, usually in parentheses.

Exercise 2.6: Typeset the recurrence defining the Fibbonacci numbers, along with appropriate initial conditions. Use a single line display, with appropriate spacing commands (\quad's) separating the parts.

## What to do next

There is a lot more to learn, but the above should get you started with typing mathematical material. You'll learn the rest by practicing and referring to books as needed. The authoritative guide for LaTeX with the amsmath extensions is Gratzer's book "Math into LaTeX" (especially Chapter 4 which covers typing mathematics). You should have this book within reach whenever you are working on a TeX document so that you can consult it if necessary. The computer labs have several copies. (If you are at home and don't have a copy handy, use an online reference such as the Harvard's LaTeX Command Reference instead.)

Don't try to learn LaTeX by imitating what you see in other papers (unless you know for sure that the author is a competent LaTeX typist). Many papers written in LaTeX are done poorly (as far as typesetting is concerned), and would make very bad example to follow. You are likely to delevop bad habits (which are hard to shed once you get used to them) if you learn TeX in this way.

A note about display math environments: You will probably be overwhelmed by the variety of display math environments that are available in LaTeX: besides the "align" and "align*" environments discussed above, there is aligned, alignat, gather, gathered, multline, and a few more. However, in practice all you need is align, align*, equation, equation*, and (occasionally) the "cases" environment. I have never felt the need to use any of the other environments.

Note on the "eqnarray" environment: This is the standard LaTeX equation environment, and the one you'll find in books on standard LaTeX (i.e., LaTeX without the ams extensions). However, I would not recommend using this environment as the "align" type environments available with the ams-enhanced version of LaTeX provide better looking output, more functionality, and are easier to work with.

Exercise 2.7: As a final exercise in this section, try to typeset the problems of the 2000 UIUC Undergraduate Math Contest. Some hints: Use the following structure:
\documentclass{amsart}
\begin{document}

\begin{enumerate}
\item ...
\item ...
...

\end{enumerate}
\end{document}


Here each item introduces one problem. The items get automatically numbered, so you shouldn't say "Problem 1"; just add the text of the problem.

Extra credit I: Solve these problems.

Extra credit II: Typeset the solutions to these problems. This is a lot more challenging than typesetting the problems since most solutions involve complex math constructs and/or multiline equtions. (The solutions are on the web, in pdf and ps form. I won't give the URL (that would be too tempting!), but with a little digging, you'll find them.)