# How to use chngcntr with an enumitem newlist with resume?

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.

-
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
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

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

\usepackage{etoolbox}
\preto\chapter{%
\restartlist{examples}%
}

-
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}%
\makeatother
\theoremstyle{example}
\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:

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}%