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.

With the following M(ish)WE, I expected the numbering of my examples list to be reset at the opening of the second chapter, but it is not.

\documentclass{book}
\usepackage{chngcntr}
\usepackage{enumitem}

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

\begin{document}

\chapter{Foo}
\counterwithin{examplesi}{chapter}
\counterwithin{enumii}{chapter}

\begin{examples}
    \item One
\end{examples}

\begin{examples}[resume]
    \item Two
\end{examples}

\begin{enumerate}
  \item One
\end{enumerate}

\begin{enumerate}[resume]
  \item Two
\end{enumerate}

\chapter{Bar}
\begin{examples}
    \item One expected; three employed
    \item Two expected; four employed
\end{examples}

\begin{enumerate}[resume]
  \item One expected; three employed
\end{enumerate}

\begin{enumerate}[resume]
  \item Two expected; four employed
\end{enumerate}

% The problem is exhibited above.
% Here on additional is material that might shed some light.
\counterwithin{enumi}{page}
\begin{enumerate}[resume]
  \item repeat enough to consume an entire page
  \item on compile, enumerate is reset (several lines after) page break
  \item The several lines after is a puzzle, but not my present concern
  \item It suffices to show chngcntr has effect in
    presence of enumitem and \verb|[resume]|
\end{enumerate}

\begin{examples}
    \item I would never want \emph{this}
    \item But, it seems to show that
\chapter{Baz}
    \item chngcntr can reset the examplesi counter
    \item but only in examples enviros with embedded chapter commands.
\end{examples}

\end{document}

The immediate question is how can I attain my expected effect?

I did try including a \setcounter{examplesi}{0} after the \chapter commands, but this did not help.

To avoid the X-Y Problem, here's my motivation:

I am working on a textbook and want to have numbered example sentences with a continuous numbering independent of all other numbering in the document, save that I also want the numbering to reset with each new chapter. Ultimately, I want cross-refs within each chapter to use only the example number and from one chapter to an example in another chapter to use the form \thechapter.\theexamplesi I was going to worry about the difference between inter and intra chapter references once I got the numbering sorted; I mention it here chiefly to avoid (if possible) solutions to the present problem which will complicate the second stage.

share|improve this question
1  
Don't use enumerate instead newtheorem: tex.stackexchange.com/questions/21227/example-environment/… –  Marco Daniel Nov 11 '11 at 17:09
    
@Marco: Thanks. I'm not very familiar with the amsthm package. I'll poke, but the first go makes the numbering be bold and chpnum.egnum where the example occurs whereas (egnum) (as in my MWE) is what's wanted. Still, gets some of what is desired :-) –  vanden Nov 11 '11 at 17:19
2  
Using \makeatletter\@addtoreset{examplesi}{chapter}\makeatother seems to solve the first problem. –  Werner Nov 11 '11 at 17:21
    
@lockstep: I thought about using the lists tag. Comparing the tag descriptions, numbering was encouraged, whereas the lists description makes it sound like it ought not be used if a more specific list-related tag applies. If community consensus is that lists ought to be used as is numbering, perhaps the description should be changed? (I'm not active enough to have a sense of community practise.) –  vanden Nov 11 '11 at 17:22
    
@Werner How so? I've tried it in preamble and body, with and without chngcntr material, and see no effect. Thanks. –  vanden Nov 11 '11 at 17:33

2 Answers 2

up vote 3 down vote accepted

You can use the enumitem command \restartlist at the beginning of chapter in combination with etoolbox:

\usepackage{etoolbox}
 \preto\chapter{%
   \restartlist{examples}%
} 
share|improve this answer
    
This depends on a more recent enumitem than I had, but it works once that is provided. Thanks. –  vanden Nov 11 '11 at 20:29
    
@vanden: What do you mean? –  Marco Daniel Nov 11 '11 at 20:40
    
My distro ships an old TeXLive. I have enumitem 2009/05/18 v2.2 installed. With that, your code fails to compile. Dropping enumitem.sty for enumitem 2011/09/28 v3.5.2 into my working dir fixes it. –  vanden Nov 11 '11 at 21:20

The ntheorem and amsthm packages let you define an environment called example. If you provide the following code in your document

\usepackage{amsmath,ntheorem}
\makeatletter
\newtheoremstyle{example}%
    {\item[\hskip\labelsep \theorem@headerfont ##1 ##2\theorem@separator]}%
    {\item[\hskip\labelsep \theorem@headerfont ##1 ##2: ##3\theorem@separator]}
\makeatother
\theoremstyle{example}
\theoremheaderfont{\itshape}
\theorembodyfont{\upshape}
\theoremseparator{.} 
\theoremnumbering{arabic}
\newtheorem{example}{Example} 
\numberwithin{example}{chapter} % the \numberwithin command is provided by amsmath package

you could, for example [bad pun warning!], use the commands \begin{example} ... \end{example} to start and end each example. For instance, the second and third example in Chapter 3 of a document, coded with the commands

\begin{example} 
This is just an example. 
\end{example}
\begin{example}[How to get stuff done] 
This is yet another example. 
\end{example}

would generate the following output:

enter image description here

Of course, you're free to change the definitions of \theoremheaderfont, \theorembodyfont, \theoremseparator, etc.

If you want an automatic line break after the "Example x.y" line, you'd use the following setting of the \newtheoremstyle command:

\makeatletter
\newtheoremstyle{example}%
   {\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont
      ##1 ##2\theorem@separator}\hbox{\strut}}}]}%
   {\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont
      ##1 ##2: ##3\theorem@separator}\hbox{\strut}}}]}
\makeatother

Last but not least, if you just want the example's number but not the string "Example " to start off each example, you would just eliminate the "##1" snippet from the theoremstyle definitions.

share|improve this answer

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.