5

I have a weird numbering problem in a manuscript that uses sequentially numbered text examples. When an example demonstrates something which is e.g. wrong and later to be corrected they have suffixes a, b, and so on. In between it happens that there is text and possibly other examples which continues the original numbering, e.g:

(1) ... example ...

... text ...

(2a) ... bad example ...

... enlightening text and example:

(3) ... example ...

... after which we might return to the earlier bad example (2a) and show the correction

(2b) ... good example ...

... back to normal with more examples:

(4) ... example ...

I was asked how one would do this in a smart way in LaTeX. I have tried using the enumitem package as follows, but ran into trouble:

\documentclass{article}

\usepackage{enumitem}

\newlist{examples}{enumerate}{1}
\setlist[examples]{label=(\arabic*)}
\newlist{subexamples}{enumerate}{1}

\begin{document}
First an example, which we call 1.
\begin{examples}
  \item example
\end{examples}
Then a bad example which we would like to call 2a: 
\addtocounter{examplesi}{1}
\begin{subexamples}[series=aname, label=(\theexamplesi\alph*)]
  \item example
\end{subexamples}
We learn something new in examples 3 and 4 (with wrong labels):
\begin{examples}[resume]
  \item example
  \item example
\end{examples}
And we get back to the bad example, which should be 2b: 
\begin{subexamples}[resume*=aname]
  \item example
\end{subexamples}

\end{document}

The above example produces the labels 1, 2a, 2, 3, 3b. The goal was 1, 2a, 3, 4, 2b.

One should be able to reference the individual items, and it should be flexible enough to have more than a single "hanging" example...

  • Please don't cross-post questions. Rather flag them for migration. – Werner Jul 31 '12 at 17:58
  • Don't worry. Most new-comers are unfamiliar with the inter-relationship between networks on Stack Exchange. You can delete your post on StackOverflow. Alternatively, it may be closed as "off topic" there, or migrated here where it will also be closed as a "duplicate" of this question. Regardless, you should synchronise your two accounts by using the same OpenID (at the moment you have two separate accounts, according to Stack Exchange; one for Stack Overflow and one for TeX - LaTeX). – Werner Jul 31 '12 at 18:04
  • If I close it on StackOverflow, will it still be closed here? Is this not a more relevant place to ask the question? And how do I close a question? ... never mind, deleted it on SO. Hope this will stay open then... – Stefan Jul 31 '12 at 18:09
  • You asked a separate question on both networks. It won't be closed here if it is closed on Stack Overflow. If it's migrated here from Stack Overflow, it will be closed here as a duplicate. Either way, this question will be kept (and most likely answered) on TeX - LaTeX. Question regarding the inter-relationship between networks (closing, migration, etc) is best asked on the respective "meta sites": meta.TeX.SE / meta.SO – Werner Jul 31 '12 at 18:12
  • You currently have 2 separate accounts on the StackExchange network. One for Stack Overflow and one for TeX - LaTeX. This is fine, but it may suit you better to have a single account. To do this, log into TeX - LaTeX using your Stack Overflow credentials and then ask one of the moderators in chat to merge your new linked and old TeX.SE account. Even if you post an answer or comment here using your linked account, I can let contact the mods to fix it. – Werner Jul 31 '12 at 20:44
1

Well, I found one way to do it; I wouldn't call it satisfactory but it works. The cons are: you can't mix examples with suffixes and examples without in a single list, and the siffixes a, b, ... are 'hard coded' whereas the main counter is 'automatic'. Also, I would think it could be prettier as the referencing is slightly messy. Here goes:

\documentclass{article}

\usepackage{enumitem}
\newlist{examples}{enumerate}{1}
\setlist[examples]{label=(\arabic*), ref=\arabic*, resume}

\begin{document}

\section{On numbered examples}
First, an example:
\begin{examples}
   \item\label{first} \dots example \dots
\end{examples}
This can be referenced as expected: check out (\ref{first}).
Now, a second example which we can elaborate on later is
\begin{examples}[label=(\arabic*a), ref=\arabic*]
   \item\label{second} \dots example \dots
\end{examples}
The suffix in (\ref{second}a) hard coded as \verb+label=(\arabic*a)+
in the options for this one example environment.

We learn something new in examples 3 and 4, and use the
environment as is:
\begin{examples}
   \item example
   \item example
\end{examples}
And we get back to previous example in examples 2b and 2c:
\begin{examples}
  \item[(\ref{second}b)] \dots example \dots
  \item[(\ref{second}c)] \dots example \dots
\end{examples}
Since \verb+\item+ is used with \verb+[]+ the counter is not
incremented. These are referenced as e.g. \verb+(\ref{second}b)+.
And we test that the counter is as it should be:
\begin{examples}
   \item \dots example \dots
\end{examples}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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