Tx · LaTeX & TikZ

How to Create a Numbered Example Environment in LaTeX

Published By the 9to5Science editorial team

At a glance

  1. 1. Load amsthm

    Add the amsthm package in your document preamble.

  2. 2. Declare the environment

    Use newtheorem to create an Example environment with an automatically incrementing counter.

  3. 3. Choose numbering

    Add an optional sectioning level to restart example numbers at each section.

  4. 4. Reference examples

    Give an example a label and refer to it with ref.

To make LaTeX number examples automatically, define an example environment in the preamble. For most documents, the simplest approach is the amsthm package and its \newtheorem command. You can choose whether numbering runs through the whole document or restarts at each section.

Define a numbered example environment

Put these lines in the preamble, before \begin{document}:

\usepackage{amsthm}
\newtheorem{example}{Example}

Now write an example using the environment:

\begin{example}
The sum of two even integers is even.
\end{example}

LaTeX prints a heading such as Example 1, then the environment’s contents. The next example environment is numbered 2, and so on. You do not need to increment a counter yourself.

Complete compilable example

This document numbers examples within each section and shows how to refer to one:

\documentclass{article}
\usepackage{amsthm}

\newtheorem{example}{Example}[section]

\begin{document}

\section{Even numbers}

\begin{example}\label{ex:even-sum}
The sum of two even integers is even. If they are $2a$ and $2b$,
then their sum is $2(a+b)$, which is even.
\end{example}

As Example~\ref{ex:even-sum} shows, the sum has the required form.

\begin{example}
The product of two odd integers is odd: $(2a+1)(2b+1)$ is one
more than an even integer.
\end{example}

\end{document}

The [section] option makes the counter restart in each section. In the first section, the examples are numbered Example 1.1 and Example 1.2. Without [section], they would instead be numbered Example 1 and Example 2 across the document.

Place \label inside the environment, usually immediately after it begins. Refer to it with \ref; compile the document twice if the reference initially appears as ??. The nonbreaking space in Example~\ref{ex:even-sum} keeps the word “Example” attached to its number.

Number examples within chapters or sections

The general syntax is:

\newtheorem{environment-name}{Printed heading}[parent-counter]

For example, in a book or report, use [chapter] to reset the example number in each chapter:

\newtheorem{example}{Example}[chapter]

The environment name is what you use after \begin and \end. The second argument controls the heading printed in the document. The optional final argument sets the counter that the example counter follows. Common choices are section for articles and chapter for books and reports.

If you prefer continuous numbering, omit the optional argument:

\newtheorem{example}{Example}

Share numbering with another environment

Sometimes definitions, theorems, and examples should use one shared sequence. Declare one environment with a counter, then name that environment as the optional argument for the others:

\newtheorem{theorem}{Theorem}[section]
\newtheorem{example}[theorem]{Example}

Now a theorem and an example in the same section draw from the same counter. For instance, after Theorem 2.1, the next example is Example 2.2. If examples need their own sequence, do not use this shared-counter form.

Common mistakes

  • Defining the environment after \begin{document}: Put \usepackage and \newtheorem declarations in the preamble.
  • Assuming amsthm is required for \newtheorem: The command is provided by LaTeX itself, so the package is optional. Load amsthm if you want its theorem-style features and formatting.
  • Expecting [section] to print the section number by itself: It both resets the counter and makes the section number part of the displayed number, as in 1.1. The numbering style can be customized if needed.
  • Typing the number manually: Avoid headings such as \textbf{Example 1}. Manual numbers will not update when you add, remove, or reorder examples, and cannot be referenced reliably.
  • Using \newtheorem twice with the same environment name: Declare each environment name only once. To share a counter, use the shared-counter syntax instead.

For ordinary numbered examples, \newtheorem is less error-prone than building a counter and heading by hand. It handles the numbering and integrates naturally with labels and cross-references.

References

  1. The amsthm package

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.