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
| Environment | Purpose | Package |
|---|---|---|
document | The body of the document | – |
itemize, enumerate, description | Bulleted, numbered and labelled lists | – (enumitem to customize) |
center, flushleft, flushright | Centered, left- or right-aligned text | – |
quote, quotation, verse | Indented quotations and poetry (see quotes) | – |
abstract | The abstract in article and report | – |
figure, table | Floats with a caption and number | – |
tabular | The grid of a table | – |
minipage | A box of fixed width, e.g. for side-by-side content | – |
verbatim | Text printed exactly as typed, in monospace | – |
equation, align, gather | Numbered display math (see multiline equations) | amsmath for align and gather |
cases, pmatrix, bmatrix | Piecewise functions and matrices | amsmath |
proof, theorem-like | Theorems and proofs | amsthm |
titlepage | A custom title page | – |
frame | One slide in Beamer | beamer class |
tikzpicture | A TikZ drawing | tikz |
lstlisting | Source code with syntax highlighting | listings |
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}| Definition | Numbering |
|---|---|
\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
| Message | Cause | Fix |
|---|---|---|
| LaTeX Error: \begin{itemize} on input line 3 ended by \end{enumerate} | Mismatched names, or nested environments closed in the wrong order | Match 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 closed | Add the missing \end{center} after the line the message names |
| LaTeX Error: Environment example undefined | No class or package defines it; typo in the name | Define 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 quote | Choose another name, or use \renewenvironment to replace it on purpose |
| Illegal parameter number in definition of \endexample | #1 used in the end code of \newenvironment | Use \NewDocumentEnvironment, or save the argument in a macro in the begin code |
Missing $ inserted at cases or pmatrix | A math-only environment used in normal text | Put it inside \[ ... \], equation or $...$ |
| Package amsmath Error: Erroneous nesting of equation structures | align or equation inside \[ ... \] or another display | Use 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
- Theorems and Proofs – amsthm styles, proofs and QED symbols
- Lists in LaTeX – itemize, enumerate and description
- Multiline Equations – align, gather and split
- Piecewise Functions – The cases environment
- Sections and Chapters – Structuring the document around your environments
- Learn LaTeX: Tutorial for Beginners – The basics every document builds on
Free LaTeX Tools
- LaTeX Preview – Test a new environment instantly
- AI LaTeX Generator – Describe an environment and get the code
- Lecture Notes Template – Example, theorem and definition environments ready to use
Related Articles
Angle Brackets ⟨ ⟩: Symbol, Copy and Paste, and LaTeX
Compile LaTeX from Claude Code or Cursor with the Octree MCP Server
Diameter Symbol (⌀): Meaning, ⌀ vs Ø, and How to Type It on Windows, Mac, Word, LaTeX
LaTeX on iPad: How to Write and Compile LaTeX on a Tablet
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.