Lesson #4: Mathematcs

Download the lesson example and open it up in your development environment. The text here is complementary to the source code, and this lesson will mostly be a description of how the source code works. It's probably worth compiling the source code just to get a notion of what the result of each bit of the source code looks like.

# At long last, mathematics!

The main reason professionals in math-heavy fields like LaTeX is that it handles equations and mathematical formatting so very well. And surely you've been impatient to get to the good stuff! The big idea is that LaTeX has some things which it'll only do in text mode, like small caps, and some things which it only does in math mode, like fractions. It also handles ordinary text in math and text modes differently; in math mode, every letter is assumed to be a variable, and so it's typeset in italics and spaced a little bit differently than letters in a word would be.

To switch between modes in a paragraph, you just include a dollar sign character to switch to math mode, and another dollar sign to switch back. So writing something like We know that if $x>3$, then $2x>6$. will format right, with the right symbols, styling on the variable, and spacing. If you wrote this without the dollar signs, it would compile fine but not look right. Lines 17–23 of the lesson example exhibit some simple examples of this "inline math mode".

If you want "start math mode" and "end math mode" to look different, you can use $$to start writing math and$$ to end it. But using $for both the beginning and end is pretty standard. If you want to put an equation (or other bit of mathematics), all on a line by itself, either for emphasis or because it's large and wants to be alone, you can do it in any of three different ways. You can enclose the math to be displayed in two pairs of dollar signs, or can bracket it with $and$, or even put it in an environment called displaymath. All three of these are shown in the lesson example: double dollar signs are used on line 20, backslashed brackets on line 49, and the displaymath environment on line 53. # Symbol-printing commands in math mode But to do anything really interesting in math mode (aside from spacing variables to look like variables) you need commands. Some of those commands are used to produce mathematical symbols, just like we needed commands to produce unusual symbols in text. There are a huge number of those, because there are a huge number of mathematical symbols you might want to use, so there's \times for a cross-multiplication and \cdot for a multiplication dot, and \subseteq for a nonproper-subset symbol, and \ge for a greater-than-or-equal-to sign, and so forth. A good list of the built-in symbols made for math mode appears in Tables 48, 50, 88, and 122 of the Comprehensive LaTeX symbol list. Don't worry, you probably won't need all of them at once, and most of the time the names make some sense. ## Common functions in math mode One problem with the way math mode formats text is that it's not right for a lot of the common functions in math. Logarithms, sines, and the like should use upright letters typeset as words, not italic individual variables. If you write$sin(x)$, it doesn't actually look right; the letters are italicized and spaced wrong. Compare that with$\sin(x), which has the sine function typeset upright. These are broadly known to LaTeX as "log-like" symbols, and a complete list of them is in Table 181 of the above-mentioned symbol list. Contrast how these two are typeset on line 42 of the example source. Defining new log-like symbols is possible but a bit difficult at this stage, but in a later lesson we'll see how to define, say, a command \arccsc. ## Layout commands in math mode Math is often more than just horizontally laid-out text, though. Mathematical expressions often have superscripts, subscripts, fractions, radicals, matrices, and so forth. For now, we'll look just at the first four in that list. To typeset a fraction in math mode, use the command \frac{numerator}{denominator}. The numerator and denominator can be as complicated as you like and can even include fractions in their own right! See examples of this usage on line 47 and 49. To typeset something with a superscript in math mode, use the command base^{exponent}. For subscripts, use the command variable_{index}. Note that these don't include backslashes but are just single-character commands in their own right. Recall that a literal underscore would be coded as \_. A literal caret would be \^{} (because \^ is actually an accent, so you have to tell LaTeX to put it over nothing). Examples of subscripts and superscripts are in lines 27 and 28 of the sample source; these examples also show that subscripts and superscripts can themselves be superscripted or subscripted. A square root is written as \sqrt{parameter}; as with other commands, the parameter itself can be complicated, and the square root will resize to accomodate it. If you want radicals other than square roots, this command has an optional parameter: \sqrt[n]{parameter} will make a radical with a small number to its left; for instance, \sqrt[3]{5} is the standard way to depict the cube root of 5. A simple square root is exhibited in line 53 of the source example. You can, and some users do, omit braces when the terms in question are a single character long. Thus \frac25 is as good a way of saying two fifths as \frac{2}{5} is, and likewise \frac{x}{y} could be written as \frac xy (note that space, because \fracxy would just be a single invalid command). Likewise x^2 works just as well as x^{2}. The danger of this practice is that if you get into the habit of simplifying this way, you're almost certain to at some point do it with a parameter that's more than one character long. Once you think of x^4 and x^7 as natural ways to write x raised to the fourth and seventh power, it's easy to fall into the trap of writing the twelfth power as x^12, which won't look right. That would be parsed as just superscripting the 1; the correct way to write it is x^{12} (see lines 30 and 31 for further examples of this problem). # Layout in display vs. inline math We have two different versions of math mode, and they have somewhat distinct typesetting priorities. Inline math wants to keep your paragraph formatting uninterrupted, so its priority is to use as little vertical space as possible, whereas display math is already breaking out of the paragraph structure, and so it can be as tall as it needs to be without making the line of text it's in look strange. Consequently, some expressions typeset differently in display mode than they do in inline mode. One easy example is that fractions are a lot taller in display mode: numerators and denominators are full-size, unlike the half-size numerals used in inline mode. Contrast the fractions in line 47 of the source example with the derivative (typeset as a fraction) in line 49, or the fraction in line 61. Another family of expressions which are typeset differently in display and inline modes are those symbols which conventionally have additional text above and/or below them. Such symbols include the integral (\int), big-sigma sum (\sum), and limit (\lim), and the additional text is passed to LaTeX as a subscripts and/or superscripts. Inline these are treated like conventional subscripts and superscripts, but in display math, where vertical space is not at a premium, these elements are horizontally aligned with the symbol. Contrast the way the range on the sum is typeset in the examples in line 59 and 61 of the source code example. # Equation numbering The equation environment functions like the displaymath environment (or either of its shorthand forms), except that, in addition, each equation receives a sequential number for future reference. Lines 60–63 of the source example are such an environment, so when this bit of math is typeset it receives a flush-right label "(1)". Like any other label, this one can be referred to by using the \label command and a later \ref, as seen on line 69. ## Troubleshooting Most simple math errors come from failing to turn math mode on or off appropriately. The error messages mostly say exactly what needs to be done, but not always in the right place. • Missing inserted: You have mismatched (or absent) math-mode signs, so you're not in math made somewhere where you want to use a fraction, or a square root, or some other uniquely mathematical command. Note that the actual site of your error is probably some ways before the place where LaTeX ran into trouble; it's an earlier math-mode expression which may have a missing dollar-sign.
• Display math should end with : You have failed to end a display-math equation, probably somewhere before where the error occurred.
• Bad math environment delimiter: This one is much rarer, but it denotes an attempt to begin a display-math environment when already in one, or an attempt to end one when you aren't. This usually means your display-math symbol-pairs are mismatched somehow.
• Missing } inserted: This is a catchall for all sorts of unparseable commands in math mode. For instance, it arises when a math expression ends with a fraction missing its denominator. Best practices are just to look very closely at the equation immidiately preceding it. Often there is more going on that just a missing close-bracket.
• Missing \endgroup inserted: This is a similar catchall to the above, although it usually comes of soem sort of unhappy interaction between math mode and environments. Check that all math expressions are closed properly.

## Exercise

Write a document which compiles to make a PDF looking like this.