Environments

A region wrapped between \begin{...} and \end{...} is called an environment. It is a way to mark off a span of your document and apply some behavior or formatting to everything inside it — and almost every LaTeX structure (lists, displayed equations, figures, tables, quotations) takes this shape. This page covers what an environment is, how environments nest, the common ones, and how to define your own.

What an environment is

An environment is a delimited block that begins with \begin{name} and ends with \end{name}, applying some effect to its contents. A center environment centers what is inside it; an itemize environment turns its contents into a bulleted list. The crucial rule: the name at the beginning must exactly match the name at the end. You cannot open \begin{center} and close it with \end{flushleft}.

latex
\begin{center}
この段落は中央に寄ります。
\end{center}

Equally important: an environment forms an implicit group — a local scope. Any change to fonts or spacing made inside it is undone at \end{...} and does not leak out. If a quote environment changes the indentation and margins, the surrounding text returns to normal once you leave it. Think of an environment as carrying the same scoping you would get from a brace group { ... }.

The document itself is, in fact, just one environment: \begin{document}\end{document}. The basic rule that all body text goes inside it follows directly from this.

How environments nest

Environments can be nested, but you must close them in the reverse of the order you opened them. If you open \begin{b} inside \begin{a}, you close the inner one first with \end{b}, then the outer one with \end{a} — that is, \begin{a}\begin{b}...\end{b}\end{a}.

latex
\begin{quote}
  引用の中に箇条書きを置く例:
  \begin{itemize}
    \item 一つ目
    \item 二つ目
  \end{itemize}
\end{quote}

You cannot let the spans overlap, as in \begin{a}\begin{b}...\end{a}\end{b}. This is one of the most common LaTeX errors, producing a message like \begin{...} ended by \end{...} (the opened and closed environments do not match). In long documents, indenting each environment to show how it pairs up helps you avoid forgetting or mismatching an \end.

Common environments

Standard LaTeX provides many environments grouped by purpose. The representative ones are listed below by role. Each has its own page for the details, but they all share the same \begin / \end shape.

GroupEnvironmentsWhat they do
textTextcenter, flushleft, flushright, quote, quotation, verbatimCentered / left / right alignment, quotations, verbatim output
listsListsitemize, enumerate, descriptionBulleted, numbered, and definition lists
mathMathequation, alignA displayed equation; multiple aligned equations
floatsFloatsfigure, tableLet figures and tables float to a good position
tabularTablestabularBuild a table of rows and columns

quote and quotation both indent quoted material, but quotation also indents the first line of each paragraph and suits longer, multi-paragraph quotations. The verbatim environment prints its contents exactly as typed in a monospaced font, taking even special characters like \ and { literally rather than as commands — handy for showing source code.

Some environments take arguments. tabular requires a column specification: you give the alignment of each column (c center, l left, r right) in the argument right after \begin{tabular}, as in \begin{tabular}{cc}. Inside the table, columns are separated by & and each row ends with \\.

latex
\begin{tabular}{cc}
  名前 & 役割 \\
  TeX & 組版エンジン \\
  LaTeX & 文書フォーマット \\
\end{tabular}

Many environments also have a starred form, written with a * after the name. The align environment numbers every line, for example, while \begin{align*}\end{align*} is the unnumbered version. Likewise equation* gives a displayed equation with no number.

Defining your own environment

If you repeat the same combination of formatting throughout a document, you can define your own environment with **\newenvironment**. Once defined, you call it with \begin / \end under your own name. The syntax is as follows ([nargs] and [default] are optional):

latex
\newenvironment{名前}[nargs][default]{開始時のコード}{終了時のコード}
  • name — the new environment’s name (no backslash; it may not begin with end).
  • nargs — how many arguments it takes (0–9), referred to as #1, #2, … in the body.
  • default — supplying this makes the first argument optional, with this as its default value.
  • begin-code — run at \begin{name}; this is where you use arguments like #1.
  • end-code — run at \end{name}; **it may not use arguments like #1**.

As an example, here is an important environment that highlights a note in a framed box. We let the heading word be passed as an optional argument, defaulting to “Important.” Because we supplied a default, #1 receives that argument.

latex
\newenvironment{important}[1][重要]{%
  \begin{quote}\noindent\textbf{#1:}\itshape
}{%
  \end{quote}%
}

% 使い方
\begin{important}
  締め切りは厳守してください。
\end{important}

\begin{important}[注意]
  バックアップを忘れずに。
\end{important}

The first use puts the default “重要:” (Important:) in the heading; the second passes [注意] for “注意:” (Note:) — both set the following text in italics. Notice that the begin-code opens \begin{quote} and the end-code closes the matching \end{quote}. Because the environment is itself a group, the effect of \itshape (italics) is automatically undone at \end{important} and never reaches the surrounding text.

To redefine an existing environment, use **\renewenvironment**. Its syntax matches \newenvironment, but it applies to a name that is already defined (an undefined name is an error). Conversely, \newenvironment errors if the name already exists. Both \newenvironment and \renewenvironment also have starred forms, which differ in how they treat trailing spaces in arguments.