Theorem environment numbered according to subsection

I was wondering if there was a way to modify the theorem environment so that it interacted with sections/subsections as follows:

\section{}
\subsection{}
\begin{ex} Whatever
\end{ex}

\begin{ex}Whatever
\end{ex}

\subsection{}

\begin{ex} Whatever

\end{ex}


Would output something like

1
1.1

Exercise 1.1.1

Exercise 1.1.2

1.2

Exercise 1.2.1


Where \begin{ex} is just an environment for an "exercise".

-
Sounds very doable :) could you turn your snippet into a complete MWE :) –  cmhughes Dec 20 '12 at 3:25
–  lockstep Dec 20 '12 at 10:49

You can use the built-in feature for theorem numbering:

\documentclass{article}

\usepackage{amsthm} % not necessary

\newtheorem{ex}{Exercise}[subsection]

\begin{document}

\section{}

\subsection{}

\begin{ex}
Whatever
\end{ex}

\begin{ex}
Whatever
\end{ex}

\subsection{}

\begin{ex}
Whatever
\end{ex}

\end{document}

-
This is a standard feature, not only of amsthm. –  egreg Dec 20 '12 at 8:37
@egreg And there exists \newtheorem without amsthm? –  tohecz Dec 20 '12 at 8:49
Yes, of course; amsthm and the similar ntheorem just add to the kernel functionality. –  egreg Dec 20 '12 at 8:50