Environments in LaTeX: \begin, \end and Defining Your Own

Quick answer

  • Use one: \begin{name} ... \end{name}, e.g. itemize, center, figure, equation.
  • Define one: \newenvironment{name}{begin code}{end code}; change an existing one with \renewenvironment.
  • Numbered example: \usepackage{amsthm}, \theoremstyle{definition}, \newtheorem{example}{Example}.
  • With an argument: \newenvironment{name}[1]{... #1 ...}{...}.
\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{example}{Example}

\begin{document}
\begin{example}
  The sum of the first $n$ odd numbers is $n^2$: $1 + 3 + 5 = 9$.
\end{example}

\begin{example}[Geometric series]
  $1 + \tfrac12 + \tfrac14 + \dots = 2$.
\end{example}
\end{document}

Environments are the building blocks of a LaTeX document. Everything from the document itself to a bulleted list, an aligned equation or a figure is an environment, opened with \begin and closed with \end. This guide explains how environments work, lists the ones you will use most, and shows how to define your own: an example environment with \newenvironment, a numbered one with amsthm, environments with arguments, and changes to existing ones with \renewenvironment. Every snippet compiles as shown.

How \begin and \end Work

An environment is everything between \begin{name} and \end{name}. Some take arguments right after the \begin, in braces when required and in square brackets when optional. Environments can be nested, as long as the inner one closes first:

\documentclass{article}
\begin{document}               % the document environment itself

\begin{center}                 % no arguments
  A centered line.
\end{center}

\begin{minipage}{5cm}          % required argument: the width
  \begin{itemize}              % nested environment
    \item Inside a 5\,cm box.
  \end{itemize}                % inner one closes first
\end{minipage}

\begin{tabular}{lr}            % required argument: column spec
  Left & Right \\
\end{tabular}

\begin{figure}[h]              % optional argument: placement
  \centering
  \fbox{A figure}
  \caption{Placed here if possible.}
\end{figure}

\end{document}

An environment is also a group: a declaration such as \bfseries or \small inside it stops at \end. Many environments have a starred version with slightly different behaviour, for example equation* (no number) and figure* (both columns).

Common LaTeX Environments

EnvironmentPurposePackage
documentThe body of the document–
itemize, enumerate, descriptionBulleted, numbered and labelled lists– (enumitem to customize)
center, flushleft, flushrightCentered, left- or right-aligned text–
quote, quotation, verseIndented quotations and poetry (see quotes)–
abstractThe abstract in article and report–
figure, tableFloats with a caption and number–
tabularThe grid of a table–
minipageA box of fixed width, e.g. for side-by-side content–
verbatimText printed exactly as typed, in monospace–
equation, align, gatherNumbered display math (see multiline equations)amsmath for align and gather
cases, pmatrix, bmatrixPiecewise functions and matricesamsmath
proof, theorem-likeTheorems and proofsamsthm
titlepageA custom title page–
frameOne slide in Beamerbeamer class
tikzpictureA TikZ drawingtikz
lstlistingSource code with syntax highlightinglistings

Defining an Example Environment with \newenvironment

\newenvironment creates an environment from two pieces of code: what happens at \begin and what happens at \end. An unnumbered example environment:

\documentclass{article}

\newenvironment{example}
  {\par\medskip\noindent\textbf{Example.}\ }   % begin code
  {\par\medskip}                                % end code

\begin{document}
Some text before.

\begin{example}
  To convert 3\,km to metres, multiply by 1000: 3000\,m.
\end{example}

Some text after.
\end{document}

The begin code starts a new paragraph, adds a little space and prints the label; the end code finishes the paragraph and adds space again. Anything can go in either part, including other environments, as long as they are opened in the begin code and closed in the end code. Here the example is indented and set in a smaller font by wrapping it in a quote:

\documentclass{article}

\newenvironment{example}
  {\begin{quote}\small\textbf{Example.}\ }
  {\end{quote}}

\begin{document}
\begin{example}
  Indented on both sides and in a smaller font, because
  the environment opens a quote and closes it again.
\end{example}
\end{document}

For a shaded box with a title, the tcolorbox package defines the environment for you with \newtcolorbox:

\documentclass{article}
\usepackage{tcolorbox}

\newtcolorbox{examplebox}[1][Example]{
  colback=blue!5, colframe=blue!50!black, fonttitle=\bfseries, title=#1}

\begin{document}
\begin{examplebox}
  A boxed example with the default title.
\end{examplebox}

\begin{examplebox}[Example: unit conversion]
  $3\,\mathrm{km} = 3000\,\mathrm{m}$.
\end{examplebox}
\end{document}

Let AI write the preamble

Describe the environment you need, such as a numbered example, a boxed exercise or a solution that can be hidden, and Octree's AI writes the definition and shows the PDF next to the code. Sign up free.

Open Octree →

Numbered Example Environment with amsthm

For examples numbered like theorems (“Example 1.2”), use \newtheorem from amsthm. The definition style sets the body upright, which suits examples better than the italic plain style used for theorems:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]          % numbered within sections
\theoremstyle{definition}
\newtheorem{example}[theorem]{Example}          % shares the theorem counter
\newtheorem*{remark}{Remark}                    % unnumbered

\begin{document}
\section{Series}

\begin{theorem}
  If $|r| < 1$, then $\sum_{k=0}^{\infty} r^k = \frac{1}{1-r}$.
\end{theorem}

\begin{example}[Halves]\label{ex:halves}
  With $r = \tfrac12$ the sum is $2$.
\end{example}

\begin{remark}
  Example~\ref{ex:halves} uses Theorem~1.1.
\end{remark}
\end{document}
DefinitionNumbering
\newtheorem{example}{Example}Example 1, 2, 3 … through the document
\newtheorem{example}{Example}[section]Example 1.1, 1.2, 2.1 … reset each section
\newtheorem{example}[theorem]{Example}Shares the counter with theorem: Theorem 1.1, Example 1.2
\newtheorem*{example}{Example}No number

Styles, proofs and QED symbols are covered in theorems and proofs in LaTeX.

Environments with Arguments

An optional number in square brackets after the name gives the environment arguments, used as #1, #2 and so on in the begin code. A second pair of brackets makes the first argument optional and sets its default:

\documentclass{article}

% one required argument
\newenvironment{exercise}[1]
  {\par\medskip\noindent\textbf{Exercise (#1 points).}\ }
  {\par\medskip}

% first argument optional, default "Note"
\newenvironment{note}[1][Note]
  {\par\noindent\textit{#1:}\ }
  {\par}

\begin{document}
\begin{exercise}{10}
  Prove that $\sqrt{2}$ is irrational.
\end{exercise}

\begin{note}
  Uses the default label.
\end{note}

\begin{note}[Hint]
  Assume the opposite.
\end{note}
\end{document}

The arguments are not available in the end code; #1 there causes Illegal parameter number. The newer \NewDocumentEnvironment, built into LaTeX since 2020, has no such limit and a more flexible argument syntax (O{default} for an optional argument, m for a mandatory one):

\documentclass{article}

\NewDocumentEnvironment{solution}{O{Solution} m}
  {\par\noindent\textbf{#1 to #2.}\ }
  {\hfill\textit{(end of #1)}\par}

\begin{document}
\begin{solution}{Exercise 3}
  $x = 4$.
\end{solution}

\begin{solution}[Answer]{Exercise 4}
  $y = 2$.
\end{solution}
\end{document}

Changing Existing Environments with \renewenvironment

\renewenvironment takes the same arguments as \newenvironment but replaces an environment that already exists. Often you only need to add something at the start, and then a hook is safer because it keeps the original definition:

\documentclass{article}

% Replace quote entirely: italic, with a rule on the left
\renewenvironment{quote}
  {\par\smallskip\noindent\hspace{1em}\vrule\hspace{0.8em}%
   \begin{minipage}{0.85\linewidth}\itshape}
  {\end{minipage}\par\smallskip}

% Only add to an environment: every center block in sans serif
\AddToHook{env/center/begin}{\sffamily}

\begin{document}
\begin{quote}
  A quotation in italics with a rule on the left.
\end{quote}

\begin{center}
  Centered, now in sans serif.
\end{center}
\end{document}

\AddToHook is built into LaTeX since 2020; in older installations the etoolbox package offers \AtBeginEnvironment for the same purpose. Fonts like \sffamily are explained in fonts in LaTeX.

Common Errors and Fixes

MessageCauseFix
LaTeX Error: \begin{itemize} on input line 3 ended by \end{enumerate}Mismatched names, or nested environments closed in the wrong orderMatch every \end to its \begin; close the inner environment first
LaTeX Error: \begin{center} on input line 3 ended by \end{document}An environment was never closedAdd the missing \end{center} after the line the message names
LaTeX Error: Environment example undefinedNo class or package defines it; typo in the nameDefine it with \newtheorem or \newenvironment, or load the package (amsmath, mathtools, subcaption…)
LaTeX Error: Command \example already defined\newenvironment or \newtheorem with a name that exists, e.g. example in Beamer or quoteChoose another name, or use \renewenvironment to replace it on purpose
Illegal parameter number in definition of \endexample#1 used in the end code of \newenvironmentUse \NewDocumentEnvironment, or save the argument in a macro in the begin code
Missing $ inserted at cases or pmatrixA math-only environment used in normal textPut it inside \[ ... \], equation or $...$
Package amsmath Error: Erroneous nesting of equation structuresalign or equation inside \[ ... \] or another displayUse align on its own; inside another display use aligned

Frequently Asked Questions

What is an environment in LaTeX?

An environment is a block of the document that starts with \begin{name} and ends with \end{name} and is formatted in a particular way: a list, a centered block, a table, an equation, a figure or a theorem. Settings made inside an environment, such as a font change, end with it.

How do I create an example environment in LaTeX?

For a numbered example, load amsthm and write \theoremstyle{definition} and \newtheorem{example}{Example}[section] in the preamble, then use \begin{example} ... \end{example}. For an unnumbered example with your own formatting, define it with \newenvironment{example}{\par\noindent\textbf{Example.} }{\par}.

What is the difference between \newenvironment and \renewenvironment?

\newenvironment defines a new environment and stops with “Command \name already defined” if the name exists. \renewenvironment replaces an existing environment and stops with an error if it does not exist. Use \renewenvironment only to change environments such as quote or abstract that the class already defines.

How do I pass an argument to a LaTeX environment?

Give the number of arguments in square brackets: \newenvironment{note}[1]{\textbf{#1:} }{}. A second pair of brackets makes the first argument optional with a default: \newenvironment{note}[1][Note]{...}{...}. The arguments can only be used in the begin code; for access in the end code, use \NewDocumentEnvironment.

How do I fix “\begin{itemize} ended by \end{enumerate}”?

Every \begin needs a matching \end with the same name, and nested environments must close in reverse order. The message names the environment that is still open and the line where it started; correct the \end on that line or add the missing one.

Why do I get “Environment example undefined”?

No package or class defines an environment of that name. Example environments are not built into LaTeX; define one with \newtheorem{example}{Example} (amsthm) or \newenvironment, or load the package that provides it. In Beamer, example is already defined and works without any setup.

Next Steps

Free LaTeX Tools

Fix environment errors with AI

Octree's AI finds the unclosed \begin, defines the environments your document is missing and explains every error, with the PDF compiled next to your code.

Try Octree Free →