Environments

TeX itself has no \begin command. Knuth's plain TeX defines only \beginsection; the \begin{...}\end{...} form was added by LaTeX. So what was it added for? The answer is a single promise — the moment you pass \end, everything you changed inside is put back the way it was. That mechanism, the environment, made lists, displayed equations, figures, quotations, and even the body of the document itself all take the same shape. This page covers what a LaTeX environment guarantees, how environments nest, and how to read the \begin{...} ended by \end{...} error you get when the two names disagree.

What \begin and \end guarantee

What an environment guarantees is a group. \begin{name} opens a TeX group before it even works out what the environment is supposed to do, and \end{name} closes it again. So whatever you change inside — the font, the margins, anything — nothing of it survives the line after \end. A center environment centres its contents, an itemize turns them into a bulleted list, a quote widens the margins on both sides, and every one of those effects finishes inside its own span. Put the other way round: what an environment supplies is not decoration but a guarantee of cleanup, and that is precisely why “reach for an environment first” is the standard advice.

The names in \begin{name} and \end{name} must match exactly: you cannot open \begin{center} and close it with \end{flushleft}. Behind the scenes LaTeX does the unglamorous work of remembering the name of the environment currently open — which is why its error message can quote both names and the line number where the opening happened. The document is no exception either. \begin{document}\end{document} is a genuine environment, and the basic rule that body text goes there is really just the ordinary observation that it goes inside the outermost environment.

latex
\begin{center}
This paragraph is centred; the line after \end is not.
\end{center}

\begin{quote}
  {\itshape Everything inside is a group,}
  so this italic never escapes the environment.
\end{quote}

Nesting: close in the reverse of the order you opened

Environments can be nested, but you close them in exactly the reverse of the order you opened them: open \begin{b} inside \begin{a} and you close \end{b} first, then \end{a}. The spans cannot be made to overlap, as in \begin{a}\begin{b}...\end{a}\end{b}. The reason is the guarantee from the previous section: since an environment is a group, groups can only sit inside one another like boxes, and a half-overlapping shape simply does not exist. A list inside a quotation, a table inside a figure, an equation inside a theorem — every combination that comes up in practice writes out naturally as this kind of nesting.

latex
\begin{quote}
  A bulleted list inside a quotation:
  \begin{itemize}
    \item first
    \item second
  \end{itemize}
\end{quote}

There is only one habit worth building here: when you open an environment, write the closing line before you write the contents. Then indent the inside by two or four spaces, and the pairings — a tabular inside a figure, an itemize inside a quote — become visible in the shape of the source. The deeper the nesting, the easier it is to mismatch one \end while swapping the contents later, and as the next section shows, that particular accident does not surface where you made it.

The environments you meet first, grouped by purpose

The environments that standard LaTeX provides come into focus if you look at them in five families: text alignment, lists, mathematics, floats, and tabular material. The details of each belong to their own pages, but the shape is identical throughout — open with \begin, close with \end, and the inside is a group. When you meet a new environment, asking first “what span is this fencing off?” will usually get you close.

FamilyEnvironmentsWhat they do
textcenter, flushleft, flushright, quote, quotation, verbatimCentred, left and right alignment; quotations; output exactly as typed
listsitemize, enumerate, descriptionBulleted, numbered, and labelled lists
mathequation, align, gatherDisplayed equations, multi-line alignment, numbering
floatsfigure, tableCarry a caption and a number, and let the typesetter choose the position
tabulartabular, arrayThe grid of rows and columns itself — a different job from figure and table

Three pairs in that list are routinely confused. quote versus quotation: both indent quoted material from the left and right, but quotation also indents the first line of each paragraph, which suits a long quotation running to several paragraphs. figure/table versus tabular: these are entirely different things — the first pair is “a container that floats and carries a caption and a number”, the second is “the grid itself”. If a table needs a number and cross-references, put the tabular inside a table. And verbatim prints its contents in a monospaced font exactly as typed, refusing to interpret even \ or { as commands — the standard way to show program source.

Some environments take arguments, given in the {...} or [...] that follow \begin directly. A tabular, for instance, requires a column specification: the {cc} in \begin{tabular}{cc} gives the alignment of each column (c centre, l left, r right), and inside, columns are separated by & and each row ends with \\. Likewise figure takes a placement preference as an option, as in \begin{figure}[htbp]. Arguments always go immediately after the environment name; nothing is ever written on the \end side.

latex
\begin{figure}[htbp]
  \centering
  \begin{tabular}{ll}
    input  & output \\
    source & PDF    \\
  \end{tabular}
  \caption{A tabular grid inside a floating table}
  \label{tab:demo}
\end{figure}

align versus align*: the star is part of the name

A name with a * on the end is the version that does not number anything. align numbers every line; \begin{align*}\end{align*} numbers none. equation* is the same idea — a displayed equation with no number. What is neat here is that the * is neither an option nor an argument but part of the name itself. \begin assembles the string it is handed straight into a command name, so a command genuinely named align* — star included — comes into being. As the previous page showed, a control word's name ends at the first non-letter, so that command can never be invoked by typing \align*; the only way to reach it is through \begin{align*}.

latex
\begin{align}          % every line gets an equation number
  a &= b + c \\
  d &= e + f
\end{align}

\begin{align*}         % same layout, no numbers at all
  a &= b + c \\
  d &= e + f
\end{align*}

When you get \begin{...} ended by \end{...}

This message is not telling you “you closed it wrongly” but “here is what was open.” When the names disagree, LaTeX stops with something like ! LaTeX Error: \begin{center} on input line 5 ended by \end{flushleft}., quoting the name of the environment that was opened along with its line number. Read the first half, not the \end in the second — the line the error is reported at is almost always the \end side, while the place that actually needs fixing is the line the first half points to. A forgotten \end produces the same shape of message, but with \end{document} as the counterpart: ! LaTeX Error: \begin{itemize} on input line 2 ended by \end{document}. means the itemize opened on line 2 was never closed at all.

A misspelt name behaves a little differently. Write an undefined name such as \begin{itemzie} and you first get ! LaTeX Error: Environment itemzie undefined., followed by ! LaTeX Error: \begin{document} ended by \end{itemzie}. Two errors appear because \begin opens the group before it checks whether the environment exists at all. The group is left standing open, so the matching \end now pairs with nobody. From that comes a practical rule: fix errors one at a time from the top and recompile, ignoring everything below the first one. Around environments especially, the first error is very often the cause of the second.

There is one more clue that tells the two apart. An unclosed curly brace produces a message from TeX itself, not from LaTeX. Near the end of the log you will find just (\end occurred inside a group at level 1), with no line number. A plain, nameless group was left open, so TeX has nothing to answer “where” with. Read it the other way round and it becomes a diagnostic: a message with a line number means an environment mismatch, while this one on its own means a brace. When you need to narrow things down, cutting the suspect environment out into a separate file and compiling it alone is still the fastest route.

Choose an environment by meaning, not by appearance

An environment is not a visual box; it is the name of a part of your manuscript. Pick quote because the material is a quotation, itemize because it is a list of items, tabular because it is a grid, figure or table because it needs a number and cross-references — and captions, numbering, references and lists of figures all follow on naturally later. Keep choosing by appearance instead — “center because I want it centred”, “quote because I want some margin” — and six months on you will not be able to tell what a given box was for, nor change the house style in one pass. If all you want is a visual change, it is more honest to reach for a declaration or a spacing command than for an environment.

  • The contents are a quotation — use quote or quotation, and cite the source in the text or a footnote.
  • The contents are a logical list — use itemize, enumerate, or description; do not fake bullets with \\.
  • A figure or table needs numbering and references — put \caption and \label inside figure or table, and leave the grid to tabular.
  • The same combination recurs — instead of copying local formatting commands, name it with \newenvironment and fix its meaning.

As that last item hints, environments are not only used but defined. \newenvironment takes the code to run at the start and the code to run at the end; \renewenvironment reshapes a name that already exists. The details — how arguments are passed, why the \end side cannot use them, and the modern NewDocumentEnvironment interface — are collected on the page about writing your own. The one thing to carry away here is that an environment you define yourself inherits exactly the grouping guarantee described in this section, at no extra effort.