3

I have several environments in my .tex file. I'd like to know if there is a way to reset all counters at once?

To clarify what I mean, suppose we have environments theorem, proposition, etc., and instead of using individual commands \setcounter{theorem}{0}, \setcounter{proposition}{0}, etc, for resetting the counters to 0 I'd like to know if there is a way for resetting all counters using a single command.

Thanks

  • 3
    By "all counters" you probably mean just a subset of counters. You don't want the page counter reset as well, right? – Werner Jun 4 '14 at 19:00
  • there's nothing to keep you from defining a command, say \resetthmcounters, to reset the subset of counters you want reset, but you'll still have to specify each counter individually within the expansion of that command. i know of nothing automatic that knows which counters you care about. – barbara beeton Jun 4 '14 at 19:03
7

You can't just reset all counters. Well, that's not entirely true; but it's probably not what you want (for example, the page counter should most likely not be reset...).

You could create a command that takes a list of counters for possible resetting. Here's such an implementation:

\usepackage{chngcntr,etoolbox}
\newcounter{resetdummycounter}
\newcommand{\resetcounterlist}[1]{%
  % Add each item to clear-list for resetdummycounter
  \renewcommand*{\do}[1]{\counterwithin*{##1}{resetdummycounter}}%
  \docsvlist{#1}}% Process list
\newcommand{\resetcounters}{\stepcounter{resetdummycounter}}

\resetcounterlist{<CSV list>} takes a <CSV list> of counters that you want to have reset. Then, issuing \resetcounters will reset the counters in <CSV list> to zero. Here's a complete example:

enter image description here

\documentclass{article}

\newtheorem{theorem}{Theorem}
\newtheorem{proposition}{Proposition}
\newtheorem{corollary}{Corollary}

\usepackage{chngcntr,etoolbox}
\newcounter{resetdummycounter}
\newcommand{\resetcounterlist}[1]{%
  \renewcommand*{\do}[1]{\counterwithin*{##1}{resetdummycounter}}%
  \docsvlist{#1}}
\newcommand{\resetcounters}{\stepcounter{resetdummycounter}}

% These counters should be reset via \resetcounters
\resetcounterlist{theorem, proposition, corollary}

\begin{document}

\begin{theorem} Some theorem \end{theorem}

\begin{proposition} Some proposition \end{proposition}

\begin{theorem} Some theorem \end{theorem}

\begin{proposition} Some proposition \end{proposition}

\begin{corollary} Some corollary \end{corollary}

\noindent\hrulefill

\resetcounters% Reset all counters previously marked

\begin{theorem} Some theorem \end{theorem}

\begin{proposition} Some proposition \end{proposition}

\begin{theorem} Some theorem \end{theorem}

\begin{proposition} Some proposition \end{proposition}

\begin{corollary} Some corollary \end{corollary}

\end{document}

Note that this might confuse hyperref.

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.