Charles H. Pence

An Introduction to LaTeX

Posted on 05 Nov 2009


So you want to typeset a document with lots of math, and you don’t want it to look like x + y^2 = 4z. No, you actually want it to look like your textbook. Well, you’re in luck. In all likelihood, the program the authors of your textbook used to write it is a handy little tool called LaTeX (‘LAY-tech’). There’s all sorts of history here involving LaTeX’s author (Knuth), the programming of TeX, the book that is the TeX source code (yes, the entire source code to TeX is actually a book that you can buy), but that’s for another time and place. Or some kind of computer science lecture or something. You just want to draw pretty integrals, right? Right. So let’s get to it.

Before we start, a brief note. This isn’t necessarily the easiest thing to do in the world, nor is TeX the easiest thing to learn. However, if you don’t have too solid of computer skills, this is a great time to start learning — especially if you’re going to be in the sciences or in math, you’ll have to use computers in a relatively advanced manner, all the time. So, even if you have to struggle through this, it’ll be good for you. No, really. Like eating your vegetables.

To get LaTeX, go to the TeX Live website if you have Windows, the MacTeX website for a Macintosh (running OS X), and either the TeX Live website or your package manager if you’re running Linux. Get the full TeX Live package, and install it (warning: this is a large download and a long install).

Now you need a good editor in which to write TeX documents. If you’re on Windows, you have to go fetch one — I like WinEdt, and people have recommended TeXnicCenter. On Mac, you got TeXShop for free with your TeX installation, which works pretty nicely. On Linux, you probably already have a favorite text editor (Emacs has really nice TeX support, as does vim), and you probably know how to use the terminal, so you’re set. (On a related note, this is all about to go away, as TeXworks will be include with TeX Live 2009 on Windows, MacTeX 2009 on Mac, and downloadable for Linux.)

So, with that, let’s write our very first TeX document. Fire up your editor and fill a new document with the following:

Save this file as paper.tex (I‘d recommend having a different folder somewhere on your computer for each TeX document you make, because TeX will produce a lot of output files). Now, compile the document. [WinEdt: click the brown-dog-thing with a red curly-cue behind it; Mac: click the ’Typeset’ button; Linux: go to the command-line and type pdflatex paper.tex.] This will magically create a PDF file out of the above document. [WinEdt and TeXShop will open it for you automatically, Linux users run xpdf paper.tex.] Astute readers will immediately recognize the font here as that in which every single PDF set of lecture notes for every math class is written. Several things about the template we’ve already got. The first line tells LaTeX that you’re writing an article, which is the right kind of document for most academic papers, and sets the font size and paper size. The next block of five lines loads up packages, which, in TeX parlance, are add-ons that do different things. The first sets page margins, the second is a technical upgrade for nicer fonts, and the last three load useful math packages. The \newtheorem lines create things we’ll use later to write Theorems, Propositions, and Lemmas (shock). The title and author lines are processed by the \maketitle command below to produce the title block on the first page, so set them accordingly. The \begin{document} and \end{document} are the first and last lines of the actual paper. Right after \begin{document} should always be \maketitle, so TeX can write your title block/title page in your document. Now, we get to actually writing LaTeX.

First off, your document is divided into sections by the \section command. We’ve defined one section right off. You can define as many as you want—try copying and pasting the section command/paragraph and changing the section header. You’ll note that TeX numbers them automatically and formats them specially, all without you actually having to tell it how to do that formatting. Starting to see the beauty of the system? Good. Now, straight text in LaTeX is pretty easy. First of all, you can’t add white space between lines or paragraphs just by pressing return. Try typing the following lines to get a feel for how the paragraphs work in TeX:

This is one of the guiding principles of TeX—how you format things in your TeX file has little or nothing to do with the exact formatting of the final document. Get used to it, it’s actually kinda nice. Okay, I hear you say, now I can write great English essays in this thing, but where’s the math? Right, right, here we go then. First off, you should know that there are basically three ways to type math in a TeX file. Here’s a little demonstration:

Note that the math code between $$ is inline, and between \begin/end{equation} is typeset in the middle of the page, but there’s absolutely no difference between the math code between those delimiters — i.e. if you see me write somehwere else in the document that you do something between dollar signs or between equation tags, you can still use it between equation tags or dollar signs, respectively — these just change how the math is placed on the page. There’s also a more complicated environment you can use to write multiple equations in a row and line them up vertically on their equals sign, but it’s beyond the scope of this guide. Google “latex align” and you’ll find it.

So there’s a lot of math syntax to digest there. First, you can see that the simple syntax for raising things to a power is x^2 (that’s caret, or shift–6 on your keyboard). Also, if you want to raise to a power that’s not just a number, you have to put the argument in curly braces, like 3^{x^2} or a^{(p - 1)/2}. You should also be shocked at how automatic all of the formatting of this math is—you don’t have to do anything but type the formula. Now, I think more math formatting is best taught by example, so here we go. I’d recommend you copy these examples into your TeX file, compile it, and read the PDF along with the TeX source:

If you need some kind of random symbol that’s not listed above, it’s almost certain that it’s available in LaTeX. Download this massive PDF file, and you’ll have a list of basically everything you could ever want.

This is about all the examples that I can think of right now. Google is your friend with LaTeX—much has been written about how this system works, and it’s pretty easy to find more information. Of course, if you have questions, you can always e-mail me. If you can think of some kind of problem you want to typeset that you can’t figure out (or that you can figure out, that you think should be on this page), e-mail it to me and I’ll add to the example collection! I hope this managed to make some sense, and you’ll find it useful. LaTeX is the best computer program since the C compiler, and I hope you find using it as easy and as holistically life-fulfilling as I do. Or something. Go write brilliant math!