0

I want to automatically label examples (as well as remarks, definitions, theorems, etc) based on section number. For example, if I have defined a \section, \subsection or even a \subsubsection environment, I want to be able to automatically label my examples within that section such that I get, taking Section 3.2 for instance, something like

Example 3.2.1 (First example in Section 3.2). This is an example.

Examples 3.2.2 (Second example in Section 3.2). This is another example.

A new example in Section 3.3 would be labelled as

Example 3.3.1 (First example in Section 3.3). Yet another example.

However, I want to also keep denoting the end of the example by some specific character, such as the triangle, as automatically defined in this answer. Any ideas on how to extend this (or other) answer to account for this labelling property and keep the example ending automatic symbol?

2
  • You can use, say, `\counterwithin{example}{section} in you preamble.
    – Bernard
    Commented Jul 7, 2022 at 12:53
  • 1
    With amsmath, one typically does \newtheorem{examplex}{Example}[subsection] to get this behavior, but do you have some sections without subsections or some subsections without subsubsections? If so should some of just be "Example 3.1" (if in a section w/o subsections) whereas others are Example "3.2.2" (if in a subsection w/o subsubsection) or "Example 3.1.2.1" (if in a subsubsection), or will they all be the same nesting level? That would make it a little trickier.
    – frabjous
    Commented Jul 7, 2022 at 13:04

1 Answer 1

2

Make examplex subordinate to subsubsection and define suitably \theexamplex.

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

\theoremstyle{definition}
\newtheorem{examplex}{Example}[subsubsection]
\renewcommand{\theexamplex}{%
  \ifnum\value{subsubsection}>0 \thesubsubsection \else
  \ifnum\value{subsection}>0 \thesubsection \else
  \thesection \fi\fi .\arabic{examplex}%
}
\newenvironment{example}
  {\pushQED{\qed}\renewcommand{\qedsymbol}{$\triangle$}\examplex}
  {\popQED\endexamplex}

\begin{document}

\section{Test}

\begin{example}
Text
\end{example}

\begin{example}
Text
\end{example}

\subsection{Test}

\begin{example}
Text
\end{example}

\begin{example}
Text
\end{example}

\subsubsection{Test}

\begin{example}
Text
\end{example}

\begin{example}
Text
\end{example}

\section{Another}

\begin{example}
Text
\end{example}

\subsection{Another}

\begin{example}
Text
\end{example}

\subsubsection{Another}

\begin{example}
Text
\end{example}

\end{document}

enter image description here

Please, don't inflict this to your readers. 😉

3
  • Brilliant answer! How would you suggest defining the labels?
    – sam wolfe
    Commented Jul 8, 2022 at 18:07
  • Is it possible to simply make it change with chapter? Or section?
    – sam wolfe
    Commented Jul 8, 2022 at 18:19
  • @samwolfe Use either [chapter] or [section] instead of [subsubsection] and don't redefine \theexamplex
    – egreg
    Commented Jul 8, 2022 at 19:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .