[Tex/LaTex] How to reset all counters at once

counters

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

Best Answer

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.

Related Question