Lesson #3: Environments

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.

Environments vs. Commands

An environment, structurally, works a lot like a comand with a parameter, in that it does something to some block of text. In fact, there's no really good reason why we need environments to be a separate thing from commands, but as we will see, environments are to some extent self-documenting, andif you have to nest them then they're a lot less fussy than nesting commands.

An environment is enclosed between a \begin{environment-type} and an \end{environment-type} command. You might note that we've already seen one environment: all the printed text in every LaTeX source file we've made so far has been put into the required document environment! But there are other environments too, used for formatting all the text within, much like the \textbf command formats all the text which is passed to it as a parameter.

One simple environment which does exactly this is the em environment, used from lines 30 to 38 of the lesson example source file. The effect of this environment is to emphasize text, just like the \emph command does.

Since \emph{} and \begin{em}\end{em} seem to do the same thing, you might wonder: why have both? As a practical matter, we don't really need both; you could use exclusively one or the other. But if you wanted to apply an effect to a large block of text, making it a parameter to a command is a bit unreadable; notice how at the end there's just a close-bracket, and reading your text later, you might not know exactly why that close-bracket is there, whereas in the case of an environment, the ending command is telling you explicitly what it does. Broadly speaking, commands are used where you want to affect a small amount of text or information, and environments when you're enclosing something large (basically a full line of text or more), because having a reminder of what you're ending is particularly valuable if it's far in the file from the beginning.

Common and useful text-formatting environments

As we saw above, the em environment emphasizes text; many other environments can be used to format text paragraphs with regard to their layout on the page as well as their typeface.

The quote environment is used to layout text which has a wider margin than usual; this is the MLA standard (and standard in many publication forms) for laying out long quotations. A quotation is exhibited in lines 47 to 52 of the lesson example file. The flushleft, flushright, and center environments produce paragraphs with those three alignments (note that text in LaTeX is ordinarily full-justified, i.e., spaced so that both the left and right margins are straight). Centering is probably the most useful or those three effects, and you can see it used (nested within a quote environment!) in lines 48 to 51 on the lesson example.

The verbatim environment is a heavy-duty version of the \verb command seen in lesson 2. \verb was good for rendering single short phrases (e.g. single LaTeX commands) without trying to interpret them as LaTeX code. The verbatim environment does the same for a long block of text, and is ideal for typesetting an extended block of code (LaTeX or otherwise). In lines 64–78 of the example document included, the document's own header is recapitulated in a verbatim environment, and appears as untranslated code in the putput document. Likewise, a program written in C is put in a verbatim environment in lines 152–163; although LaTeX won't ordinarily interpret C code, it could include special characters and whitespace indentation which means that putting it in a verbatim environment produces much more satisfractory effects than allowing LaTeX to format it ordinarily.

List-formatting environments

One special class of environment is the list. Environments are used in LaTeX to produce lists of items, marked either with bullets or with numbers. In a list, each individual item is preceded with an \item command; there must be at least one item per list, although a single item can include paragraph breaks or formatting. A bulleted list (i.e. an "unordered" list) is made with the itemize environment; a simple example appears in lines 87–98 of the lesson source file. A numbered list uses the enumerate environment, as seen in lines 103–110.

List environments can even be nested and will behave in a way consistent with how nested lists appear in most text formats. Nested itemized lists will use a different shape of bullet at different depths, and nested enumerated lists will follow a style not entirely like MLA style (there are packages to change this): on the first level lists are numbered, on the second labeled with lowercase letters, then with lowercase roman numerals, and then with uppercae alphabetic letters. Nested lists are exhibited in the sample source code as well.

If a label is used in an enumerate environment, it will acquire the number associated with its item. This is exhibited on lines 105 and 109 of the sample source code, where we refer back to a previous step in a list of instructions by number. If items were added or removed from the list, the reference would change accordingly.

Floating environments

Some environments are used for providing "floats" in a document. This is a standard way large tables and graphics are presented in research: they are not simply included where referenced, but inserted in a convenient location (usually at the top of a page). To pick a arbitrary example, you can take a gander at an astrophysics paper chock full of figures and with one table. These appear near their references in the text, but they don't have a fixed location in the text; they just get numbers and are referred to in the text. LaTeX uses environments to include figures and tables. Anything put inside of a figure environment is relocated to a typographically convenient location close to the text around the figure environment.

Figure environments will usually want two commands to appear at the end of the environment as well. The \caption{} command gives the figure a numbered label and brief description, and the \label{} command attaches a label to the figure's number, just like labels do for sections and list items. Both of these are theoretically optional, but leaving off the \caption will produce an unlabeled float, which will confuse your reader, and the \label is vital if you want to refer to your figure by name in the text. You can see how a figure environment is set up in lines 151–166 in the soruce file; note also how on line 149 the figure is referred to in the main text of the document.

Troubleshooting

Difficulties with environments mostly have self-explanatory errors, but some peculiarities can happen with listing envronments.

Exercise

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


Return to the tutorial table of contents.

Valid HTML 4.01 Transitional