10

I'm attempting to use enumitem to make a list that runs consecutively in a section and then resets.

This question and accepted answer looks promising: How to use chngcntr with an enumitem newlist with resume?, but it does not quite seem to work when I try it.

The requirement from the author needing it was just that the Problems env should be usable several times within the same section, consecutive numbering within the section, and reset at et new section.

Any ideas?

\documentclass[a4paper]{article}
\usepackage{enumitem}
\newlist{xxx}{enumerate}{1}
\setlist[xxx]{
    label=\thesection.\arabic*.,
    % no end dot in ref
    ref=\thesection.\arabic*,
    resume=myProblems,
  }
\usepackage{etoolbox}
\preto\section{%
  \restartlist{xxx}%
} 

\newenvironment{Problems}{%
  \paragraph{\textbf{Exercises}}%
  \begin{xxx}}{\end{xxx}}

\begin{document}


\section{test}

\begin{Problems}
  \item A
\end{Problems}

\subsection{subtest}

\begin{Problems}
  \item B
\end{Problems}

\section{test 2}

\begin{Problems}
  \item C
\end{Problems}

\end{document}
daleif
  • 54,450

1 Answers1

8

The counter resetting (resume=myProblems) is stored in macro \enit@resume@series@myProblems. Redefining it as empty seems to help:

\documentclass[a4paper]{article}
\usepackage{enumitem}
\newlist{xxx}{enumerate}{1}
\setlist[xxx]{
    label=\thesection.\arabic*.,
    % no end dot in ref
    ref=\thesection.\arabic*,
    resume=myProblems,
  }
\usepackage{etoolbox}
\makeatletter
\preto\section{%
  \let\enit@resume@series@myProblems\@empty
}
\makeatother

\newenvironment{Problems}{%
  \paragraph{\textbf{Exercises}}%
  \begin{xxx}}{\end{xxx}}

\begin{document}


\section{test}

\begin{Problems}
  \item A
\end{Problems}

\subsection{subtest}

\begin{Problems}
  \item B
\end{Problems}

\section{test 2}

\begin{Problems}
  \item C
\end{Problems}

\end{document}

Result

Heiko Oberdiek
  • 271,626
  • Ahh, that was better, the \restartlist only sets \enit@resume@myProblems to empty. That is not enough in this case. – daleif Feb 02 '15 at 10:46
  • Wouldn't it be safer \pretocmd? In this case it's the same as \preto but I think it's better \pretocmd. – Manuel Feb 02 '15 at 19:44
  • 1
    @Manuel I have just copied the choice from daleif's code. \pretocmd might be more defensive, \preto is faster and more efficient. – Heiko Oberdiek Feb 02 '15 at 19:54