Lesson #2: Commands

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.

What are Commands?

A command is basically anything in LaTeX that isn't purely text. This doesn't include the "escaped" single characters like \%, but it includes anything else that is preceded by a backslash. We already saw a few commands in the first lesson; you don't have a LaTeX document at all without the \documentclass command, and almost all LaTeX files also include the commands \begin{document} and \end{document} as well.

If you want to puzzle out the source file on your own, there's a convenient list of every command used in this lesson for reference.

Package-management commands

We see some examples of commands used even in the preamble of the lesson. The \usepackage command is used to add in packages to extend LaTeX's functionality. We'll look at packages more deeply in some later sections.

Simple commands for special symbols

The easiest commands to understand are those which render a symbol which isn't conveniently typeable on a keyboard. Historically, LaTeX has only used those symbols, and although most computers can handle a wide variety of characters, those characters are, first of all, difficult to type, and second, may not transfer between file systems very well, so they are not conventional in LaTeX and not guaranteed to be supported. So, to include a copyright symbol in a LaTeX document, you don't want to go through your computer's character map to hunt down character U+00A9, but instead want to include a command which tells LaTeX to insert that character, which is \textcopyright.

There are an enormous number of other commands which just insert some symbol or logo into the text. They range from unusual punctuation (\ldots for an ellipsis) to common non-alphabetical symbols (\textdagger for a dagger, \pounds for a British currency symbol) to nonstandard alphabetical letters (\ae for the ligature æ, \ss for the German ß, \TH for the uppercase Icelandic Þ).

As a notable detail used extensively in this lesson's source file, there are also commands \LaTeX and \TeX to render the logo of LaTeX and its underlying engine.

How many different commands, you might ask, are there for symbols you can put into text? A lot. An extraordinarily comprehensive list of the symbols which LaTeX can render together with all of its add-on packages can be downloaded here. Tables 2, 3, and 5 contain all the symbols relevant to you at this stage; as you learn to use packages and math mode, eventually all of these symbols will become available.

How LaTeX reads and processes commands

A command is either a backslash followed by a single nonalphabetic symbol, or a backslash followed by any number of letters. Case matters, so \myCommand would be a different command than \MYCOMMAND or \mycommand.

The way LaTeX processes commands makes certain behaviors on its part a bit nonintuitive. For instance, how might you write the German word "heißen"? Since the ß character is represented by the command \ss, then you might expect that it could be written as hei\ssen. But then, LaTeX will assume the entire end of that phrase, \ssen, is a single command, which it isn't! There are several ways to make sure the command ends in the right place, and the easiest-to-use of them can unfortunately lead to other surprising behaviors.

Curly braces are what LaTeX uses for grouping. There are two ways to use them to delimit commands. One would be to enclose the command itself in braces, so we could write the above as hei{\ss}en. Alternatively, we could put an empty group right after the command, as hei\ss{}en, and then since the braces aren't an alphabetic character, LaTeX knows that the command \ss ended there. Finally, since wanting to indicate the end of a command is so common, we can just do it with a space, so hei\ss en would render as "heißen", with no space in the rendered output.

Unfortunately, the convenient form above makes LaTeX sometimes behave in a different nonintuitive way! Suppose we want to ask "Hast du Spaß gemacht?" and we write out Hast du Spa\ss gemacht?. Here the command will be read just fine, but it'll swallow up the space right after it, because LaTeX doesn't know that hei\ss en is supposed to be a single word while Spa\ss gemacht isn't. To say "this command is followed by a literal space, not a command-parsing space", there are three approaches, two of which involve explicit grouping like above: Spa{\ss} gemacht and Spa\ss{} gemacht will also include a space in the output, and so does Spa\ss\ gemacht; the backslash before the space tells LaTeX "this is a literal space".

Some commands also have parameters; we'll get to those as we see commands of that sort.

Modifying text: accents and styling

We've seen a few letters from foreign Roman alphabets above, but most letters you see in German, French, Hungarian, Spanish, Czech, and so forth are accented variants of regular letters; to write these, we actually use a command which takes the letter to be accented as a parameter. The command \^, for instance, puts a circumflex over whatever letter is passed as a \emph{parameter} to the command. A parameter is the next "thing" after the command, where a "thing" is either a single (non-space) character or something within a pair of braces. So we could write "L'Hôpital" as L'H\^{o}pital or even as L'H\^opital (since the command grabs the next character as its parameter).

Many other accents have fairly intuitive symbols. The acute accent \', grave accent \`, and the umlaut/diaresis mark \" are among the most common. The complete list of all accents built into LaTeX (and a fw only provided by special packages) appears in Table 18 of the aforementioned huge symbol list.

Accents with alphabetic command names (which are less common) can be slightly trickier, because remember that, unlike a symbol following a backslash, a letter following a backslash may not be immediately parsed as a single-letter command name! So if you wanted to write the Hungarian name "Erdős", whose long-umlaut uses the accent code \H, it would be a mistake to write Erd\Hos, which refers to the nonexistent command \Hos. Either Erd\H{o}s or Erd\H os would work, however.

Text styling is also done with commands which take as a parameter the text to be styled. You're familiar, no doubt, with boldface, italics, and small-caps, as well as the typewriter text we've been using to render command examples. LaTeX renders these with the commands \textbf, \textit, \textsc, and \texttt, respectively, so you might say something like It is \textbf{important} to make sure to read carefully!.

A slightly different stylistic discipline is described by the \emph command (meaning "emphasize"). In practice, text styled with \emph usually looks like text styled with \textit; that is to say, it's slanted. But \textit is saying specifically how the text should be rendered (i.e. slanted), while \emph merely says the text should look substantially different from the surrounding text; certain packages or editors might choose to render "emphasized" text in an atypical way. Another notable distinction between the two commands is in how they behave when nested within each other; a \textit applied when text is already being rendered in italics does nothing, because the text is already styled that way, while a \emph applied when text is already being rendered in italics produces upright text to "stand out" from its surroundings.

Structuring text

LaTeX provides some standard commands for laying out your document into sections, subsections, etc. If you put \section{Higher dimensions}, for instance, into your document, then a numbered section (with numbering starting at 1) label will be inserted, with the section titled "Higher dimensions". You can also include a structure like \subsection{Space-time as a four-dimensional structure} which will use a smaller font for the label and a dot-separated subsection number (if you had just started section 2, then that subsection would be 2.1, the next would be 2.2, etc.). There is also a command \subsubsection{} for an even lower level of structure.

If you want a section-styled header without a number, you can use \section*{}, \subsection*{}, or \subsubsection*{}, noting those asterisks.

Labels and references

One useful effect you can produce with a pair of commands is a reference to a previous number. Sections and subsections are numbered; so are a lot of other things we'll see later, like equations, figures, tables, and list items. As you add new content before a section, equation, etc., their numbers might change, so putting a literal reference to "Section 3.5" into your work is asking for trouble if you might later change the work by adding sections and causing this section to be renumbered.

To avoid this problem we use pairs of labels and references. A label captures the number of the most recent "numbered thing" (sections and subsections at present, and other structures as we discover them), while a reference puts that number into the document itself. You create a label with the command \label{key}, and then refer to it later with \ref{key}. Some good examples of labels and references appear in Section 3.2 of the source file included with this lesson, and inside the source file you can see both the command \label{references-and-labels} to label the section, and the command \ref{references-and-labels} which outputs the section's number (i.e. 3.2).

If LaTeX sees a reference it doesn't recognize, it puts a warning into the output of running LaTeX, and just puts "??" where the reference is. The source file included with this lesson has such an example, with references to the nonexistent label holy-grail; note that when you compile the source file, you should also get the warning LaTeX Warning: Reference `holy-grail' on page 3 undefined on input line 244. in your compilation log.

The first time you compile this source file, something you might not expect also happens: it doesn't recognize the label references-and-labels either! If you want to observe this happening and have already compiled your source file, delete the file lesson-2.aux and recompile. The reason for this behavior is that LaTeX is what is technically known as a one-pass system: it reads your file from beginning to end, using only knowledge it already has and squirreling away new information in an auxiliary file. So new labels aren't acknowledged when compiling; rather, they're put in the file lesson-2.aux so as to be usable on future compilations. LaTeX will warn you when it thinks this is happening, and the first compilation not only includes the seemingly unjustified warning LaTeX Warning: Reference `references-and-labels' on page 3 undefined on input line 230., but also the explicit instruction Label(s) may have changed. Rerun to get cross-references right.. If you get into the habit of compiling your LaTeX file twice every time you think a document's done, you shouldn't run into this problem. Some development environments might even be able to automatically do this for you.

In addition to referring to sections by number, you can refer to page numbers. The command \pageref{key} will insert the number of the page where the label key appears; the command \thepage will insert the number of the current page.


You're only really likely to encounter one new type of error with what we have here, and usually it'll be easily fixed:

What is far more likely is that you'll see warnings. A warning, unlike an error, doesn't halt the processing of your LaTeX file, but it usually lets you know something is undesirable in your output and is worth looking at. The warnings you're likely to see have to do with references:


Write a document which compiles to make a PDF looking like this. Be careful about spaces near commands!

Return to the tutorial table of contents.

Valid HTML 4.01 Transitional