# Give definitions and examples the same counter

I want an example to have the same number as the current definition. So if the last definition is definition 2, the example should be example 2. (Is this a good idea?). Currently with the following code it is example 2.1 for definition 2. (I don't think that I am going to have multiple examples for one definition).

\documentclass[]{scrbook}

\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}

\usepackage{lipsum}
\usepackage{amsthm}

\usepackage[english]{cleveref}

% Config
\theoremstyle{definition}
\newtheorem{definition}{Definition}

\theoremstyle{definition}
\newtheorem{example}{Example}[definition]

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]

\begin{document}
\begin{definition}[Foo]\label{def:foo}
\lipsum[2]
\end{definition}
\begin{example}[Example for Foo]\label{ex:foo}
\lipsum[2]
\end{example}

% But: not every example needs a definition.
\begin{definition}[Bar]
\lipsum[2]
\end{definition}

% The numbering should depend on the definition here, which it does.
\begin{definition}[FooBar]
\lipsum[2]
\end{definition}
\begin{example}[Example for FooBar]
\lipsum[2]
\end{example}

\end{document}


Which gives the output (except for the cref line, this is another post):

If your examples are always tied to definitions, it's quite simple: define example as an unnumbered theorem-like environment.

% Config
\theoremstyle{definition}
\newtheorem{definition}{Definition}
\newtheorem*{example}{Example \thedefinition}


I see obvious problems in case you need to refer to some example.

However, I would simply avoid numbering the examples: repeating the number will be confusing.

• I used individual labels to refer to the examples, so this worked fine. – inktrap Nov 6 '17 at 12:15

Maybe you can create a new environment that link the definition counter and the example one, like this

\documentclass[]{scrbook}

\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}

\usepackage{lipsum}
\usepackage{amsthm}

\usepackage[english]{cleveref}

% Config
\theoremstyle{definition}
\newtheorem{definition}{Definition}

\theoremstyle{definition}
\newtheorem{example}{Example}

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]

\newenvironment{definit}{\setcounter{example}{\thedefinition}\begin{definition}}{\end{definition}}

\begin{document}
\begin{definit}[Foo]\label{def:foo}
\lipsum[2]
\end{definit}
\begin{example}[Example for Foo]\label{ex:foo}
\lipsum[2]
\end{example}

% But: not every example needs a definition.
\begin{definit}[Bar]
\lipsum[2]
\end{definit}

% The numbering should depend on the definition here, which it does.
\begin{definit}[FooBar]
\lipsum[2]
\end{definit}
\begin{example}[Example for FooBar]
\lipsum[2]
\end{example}

\end{document}