Sunday, March 1, 2009

Continuing an Environment in Latex

I was looking for a way to continue an example in Latex.
Pretty much like this:

Example 1 (Caption 1)
ABC....

Example 2 (Caption 2)
ABC....

Example 1 continued
DEF...

Example 3 (Caption 3)

I found a solution here such that you have to save a temp counter for the example instances and update it manually.
However, I find that solutions that involve temp counter are rather cumbersome.
So I came up with an easier solution which I would like to share here. ;)
It should also work with other theorem-like environments.

The idea is to come up with another example environment which does not have any counter. This can be easily done through \newtheorem* command in Ams-Latex package.
Then, when we want to continue an example, the continued instance should use this no-counting theorem and refer to the number of first instance of the example.
This means you will also need to create a new theorem style for continued instance so that it can refer back to the number of the first instance of the example.

I know it is confusing, so let's see how it is actually done.

I use the following environment setting for my (normal - with counter) example:
\newtheoremstyle{example}
{\topsep}
{\topsep}%
{\upshape}% Body font

{}% Indent amount (empty = no indent, \parindent = para indent)
{\bfseries\scshape}% Thm head font
{}% Punctuation after thm head

{\newline}% Space after thm head (\newline = linebreak)

{}% Thm head spec


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

The style above should be used for the first instance of the example such as:
\begin{example}[Caption 1]\label{ex:example1}
ABC...
\end{example}


Then, I declare another environment for the continuing instances:

\newtheoremstyle{example_contd}
{\topsep}
{\topsep}%
{\upshape}% Body font

{}% Indent amount (empty = no indent, \parindent = para indent)

{\bfseries\scshape}% Thm head font

{}% Punctuation after thm head
{\newline}% Space after thm head (\newline = linebreak)

{\thmname{#1}\thmnumber{ #2}\thmnote{#3}\enspace(continued)}% Thm head spec


\theoremstyle{example_contd}

\newtheorem*{example_contd}{Example}


They look pretty much the same, except the last line (the head spec).
In the last line, I added the word "(continued)" after usual theorem declaration.

Now, here is the tricky part. When I open a new environment, instead of specifying usual caption, I use \ref command to refer to the first instance:

\begin{example_contd}[\ref{ex:example1}]
DEF....
\end{example_contd}


This way, I can always create continuing instances of an example by referring them to the first one.

The result looks like this:
EXAMPLE 1 (Caption 1)
ABC....

EXAMPLE 1 (CONTINUED)
DEF...

Hope it helps. ;)

2 comments:

Adrian said...

Thanks, very helpful!

However, there's a small bug in your code. You need to define the 'definition' counter first for it to work, i.e. put

\newcounter{definition}

in front of the first example style definition.

BiGGA said...

Thanks a lot for the correction.