Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to create an example environment, so I've done the very simple

\newtheorem{example}{Example}[section]

which I can then use as

\begin{example}
  Example 1
\end{example}

What I would really like to do is (sometimes) have example 12.1(a), 12.1(b), 12.1(c) and then also 12.2,12.3 etc.

Is there a nice simple way to do this?

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

Using your theorem definition for examples as a basis, the following MWE provides a subexample theorem with a counter of its own that links up with the example theorem:

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newbool{insubexample}
\newtheorem{example}{Example}[section]% Example
\newtheorem{subexample}{Example}[example]% Sub-example
\renewcommand{\thesubexample}{\theexample(\alph{subexample})}%
\AtBeginEnvironment{example}{\global\boolfalse{insubexample}}%
\AtBeginEnvironment{subexample}{%
  \ifbool{insubexample}{}{\global\booltrue{insubexample}%
    \refstepcounter{example}}%
}%
\begin{document}

\setcounter{section}{11}% Just because...
\section{A section}
\begin{example}Here's an example.\end{example}
\begin{subexample}Here's a sub-example.\end{subexample}
\begin{subexample}Here's a sub-example.\end{subexample}
\begin{subexample}Here's a sub-example.\end{subexample}
\begin{subexample}Here's a sub-example.\end{subexample}
\begin{example}Here's an example.\end{example}
\begin{example}Here's an example.\end{example}
\begin{subexample}Here's a sub-example.\end{subexample}
\begin{subexample}Here's a sub-example.\end{subexample}
\begin{example}Here's an example.\end{example}

\end{document}

Example and sub-example theorems

The etoolbox package provides support for boolean variables (insubexample in this case) that is used to condition on whether or not you are using the regular example theorem, or the newly defined subexample theorem. Based on this, the regular example theorem counter is either incremented or not.

share|improve this answer
    
Thank you - that is perfect! –  Qwirk Oct 7 '11 at 8:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.