Introduction to LaTeX: 1. Getting Started

The three steps involved in creating TeX documents

Writing a document in TeX or its variants (LaTeX, AmSTeX) is much like writing a computer program, in that involves three distinct steps: As with other programming languages, one usually has to go through this "edit-compile-run" cycle multiple times (a few dozen times or more in case of some longer documents) to fix any bugs and obtain the desired output. In detail the steps are:

Creating simple LaTeX documents

The simplest working LaTeX document is something like the following.

Hello world.
The essential parts of this document are:

A "\documentclass{...}" statement. This determines the general format of the document (such as font sizes of headings, whether or not to indent paragraphs, etc.). There exist hundreds of possible so-called "document classes", but the two standard ones (and the only ones you'll probably need) are "article" and "amsart". The latter is the AMS version of "article" and automatically loads the AMS enhancements to LaTeX. With "article", you can still get these enhancements, but you have to explicitly load them by adding a line "\usepackage{amsmath}" after the "\documentclass" line. Both "article" and "amsart" can be used as general purpose document classes - your document does not have to be an "article".

A "\begin{document} ... \end{document} pair. This is where the body of the document goes (body meaning everything except "title matter" (title, author, date, etc.).

Exercise 1.1: Create a short tex document similar to the above example, call it "paper.tex" (say), and compile and preview the file as explained above.

Comments in TeX

As with most programming languages, one can insert comments into a TeX document. The comment symbol for TeX is a percentage sign (%). TeX ignores anything from the percentage sign through the end of the current line. Note that there is no "end comment" symbol in TeX; to write extended, multiline comments, you'll need to put a percentage sign in front of every line.

Exercise 1.2: See what happens when you delete (or accidentally forget) the "\begin{document}" part. However, instead of actually deleting this line, put a percentage sign in front of it, which has the same effect.

Special characters

Escaping special characters: The usual alpha-numerical characters and punctuation marks can be used as is and behave as expected. However, some symbols have a special meaning to TeX, and to use these characters, you have to (usually) "escape" them by putting a backslash in front of the character. For example, "$10" should be typed as "\$10", and 10% as 10\%. (Note: TeX ignores blanks following an escaped character; if you want a blank (as in "10% of ..."), you have to "escape" the blank as well ("10\%\ of ...").

Accents: To get accents, precede the character with the accent by an escape quote of the appropriate kind (left, right, or double): For example, "Paul Erd\"os", "Andr\'e", "l'H\^opital's rule".

Braces and parentheses: Brackets and round parentheses can be used as is and shouldn't be escaped, but curly braces ("{","}") are used for grouping in TeX, and don't get printed. To get curly braces in the output, you must use the escaped versions, "\{", and "\}".

Quotation marks: The usual double quotation marks don't come out correctly in TeX. To get correct double quotes, surround the quoted phrase by a left and a right pair of single quotes (``TeX'').

Commands in TeX

Most of what you type is interpreted literally, as ordinary text. The exceptions are special characters and so-called macros - instructions for the TeX program. Macros always begin with a backslash followed by either a single symbol (e.g., \$), or by a one or more letters (such as \par, \newline). Macros can have one or more arguments, usually enclosed in braces.

Line and paragraph breaks

TeX decides where to break lines, regardless of where the line breaks occur in the source file. (In this respect, the behavior is the same as with html code.) However, paragraph breaks have to be explicitly specified, either by leaving a blank line between paragraphs (the recommended method), or by adding a "\par " instruction at the place you want to break paragraphs). Be sure not to leave blank lines where you don't wont a paragraph break, e.g., before and after formulas.)

Exercise 1.3: Replace "Hello World" by a couple of short paragraphs and rerun the file through latex, to see how TeX treats lines and paragraphs. Add some words with special symbols and accents for variety.

Title matter

Author, title, date, etc., are specified as in the following example:

\author{Don Knuth}
\title{The \TeX\ Book}





Note that the "title matter" material (author, title, etc.) goes before the "\begin{document}" instruction, i.e., in the "preamble" of the document, but the "\maketitle" instruction which causes the title matter to be printed, goes after "\begin{document}". The command "\today" is a macro instructing TeX to use today's date. You can, of course, specify a date explicitly by saying something like "\date{August 1, 2001}". The special macro "\TeX" generates the "TeX" logo; the blank following this macro has to be escaped since otherwise it will disappear.

Exercise 1.4: Add a "title matter", similar to the one above, to your document, and rerun the file through latex. Also, see what happens if you "delete" (i.e., comment out) the "\maketitle" command.


Titles of sections, subsections, etc., are specified by using commands like the following the body of the document (i.e., between \begin{document} and \end{document}).
\section{Special characters}



\subsection{Dollar signs}




Exercise 1.5: Add sectioning commands like those above to your document and rerun the file through latex. Notice that LaTeX automatically numbers the sections. The numbering can be prevented by using "asterisk" versions of the sectioning commands: \section*{...}, \subsection*{...}, etc. Do that and rerun your program.

Exercise 1.6: Now replace "article" in "\documentclass{...}" first by "amsart" , and then by "book", and observe how the appearance of the document changes, depending on the documentclass chosen.

Note on fonts in titles and section headings. In almost all cases, you should leave the choice of fonts to TeX and not try to explicitly force a specific font, type face, of type size. For example, you could make the section headings larger than they normally would be by placing a command "\Huge" or "\Large" in front of the title (as in "\section{\Huge Special Characters}"). It does work (try it!), but this is very bad style and should be avoided, since it defeats the point of having structuring commands like "\section" (and has other undesirable "side effects"). Nonetheless, there are situations where you might want to put something in boldface or italic, e.g., to highlight a special term. You can do that with the command "\textbf{...}" (for boldface), or "\textit{...}" (for italic). For example, you could start an exercise as follows:

\textbf{Exercise 6.} \textit{Prove Fermat's Theorem.}


A fundamental concept in LaTeX is that of an environment. An environment is a pair of matching commands of the form \begin{command} ... \end{command} that cause TeX to behave in a particular way when processing material enclosed by this pair. The "document" environment surrounding the body of a document is one such example. Other examples are the following:

\begin{center} ... \end{center}

\begin{quote} ... \end{quote}

\item This is the first item
\item This is the second item
\item This is the last itme

\begin{equation} ... \end{equation}

\begin{align} ... \end{align}

The third example creates a bullet list. The last two examples are math environments for typesetting numbered equations and multiline numbered equations, respectively (see below).

Errors during compilation

If TeX encounters an error while processing a tex file, it displays a message describing the error, and (usually) giving the line number in the tex file at which the error occurs, and displays a question mark prompt. The best action in most cases is to type "x" to exit the compile phase, after noting the line number of the error. Then make an appropriate correction in the source file, and try again. At times the error messages are misleading or seem uninformative, and you may not be able to figure out what is wrong. In that case, you could try to press the "Enter" a few times to force TeX to proceed with processing the file, or type "s" which is essentially equivalent to pressing "Enter" at every prompt.

Typesetting mathematical material

Text and math modes. 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.

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: "$a^2 + b^2 = c^2$". The dollar signs cause TeX to enter and exit (ordinary) math mode.

Display math mode. Formulas that are to be displayed on a separate line should be surrounded by a pair of escaped brackets ("\[" and "\]"). For example, to typeset the above equation as a displayed formula, use the following:

a^2 + b^2 = c^2
Note that the the two "display math symbols" have been put on lines by themselves. This is not necessary, as far as TeX is concerned (the above input is completely equivalent to the terse "\[a^2+b^2=c^2\]"), but it greatly improves the readability of the tex file, and it's a good habit to keep. (In contrast to conventions for c programs, there is no need for, or particular advantage to, indenting lines.)

Note about the "double dollar sign" ($$) symbold: In AmSTeX and Plain TeX, material to be typeset in display math mode is enclosed in a pair of double dollar signs. While the double dollar sign does work 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. The main advantage of the bracket pair "\[", "\]" over the old double dollar sign pair is that the bracket pair differentiates between beginning and end of math mode, which in turn facilitates error checking.

Symbols in math mode. Some symbols take on special meanings inside math mode, or are valid only in math mode. For example, the caret (^) (denoting exponentiation) is not valid outside math mode, and TeX will give an error message if it finds this symbol in text mode.

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 not put explicit spaces into mathematical formulas. (There are a few situtations where one might want to adjust the spacing, but those are extremely rare, and for a beginner it is best to just let TeX do the work.)

Equation numbering. There are a number of other "environments" that, just like the escaped bracket pair, delimit display math mode. One of these is the "\begin{equation} ...\end{equation}" pair which acts just like the escaped bracket pair except that the formula gets automatically numbered.

Exercise 1.7. Add a few equations to your documents, including some displayed equations. (Some examples of math formulas you can use as models or building blooks are: \sum_{k=1}^n k^2, \frac{a}{q}, \int_1^x\frac{1}{x}dx, \sin(x), \arcsin(x), e^{2 \pi i}. Typing math will be covered in detailed next time.) Typeset these with \begin{equation} ... \end{equation} and see how formulas get numbered. Now switch from the "article" document class to the "book" document class; what effect does this have on the numbering of equations?

Back to Table of Contents

Last modified: Wed 18 Aug 2004 02:32:05 PM CDT