I am working on a own LaTeX (using KOMA-Script) class, based on scrartcl.

The document structure is supposed to provide sections and subsections, as well as own environments (exercises, solutions, examples &c.).

Those environments are to follow in the hierarchy directly after section, IF no subsection is declared and after subsection IF it is declared, respectively.

I am looking for a way to automatically reset the counters of such environments, say the counter exc of the exercise-environment, everytime the counters section OR subsection are changed.

You can find a minimal example here:




    % begin
    \refstepcounter{exc}% count
    \ifnumequal{\value{subsection}}{0}% find correct level
        % subsection level
            {Ex.~\thesection.\theexc}% add to toc on subsection level
        % subsubsection level
            {Ex.~\thesubsection.\theexc}% add to toc on subsubsection level
    % end




  \begin{exc} Lalala. \end{exc}

  \begin{exc} Lalala. \end{exc}


\subsection{my little world}

  \begin{exc} Lalala. \end{exc}

\subsection{your big life}

 \begin{exc} Lalala. \end{exc} %% HERE'S THE PROBLEM.


 \begin{exc} Lalala. \end{exc}

\end{document} `
  • Welcome to TeX - LaTeX! A counter \newcounter{foo}[subsection] will be reset both when section or subsection are incremented. – clemens Oct 13 '15 at 22:33
  • @clemens That is true from latex releases 2015/01/01 not older latex. – David Carlisle Oct 13 '15 at 22:34
  • @DavidCarlisle yes – I probably should have mentioned that… – clemens Oct 13 '15 at 22:35
  • @clemens mention it an answer and I'll give you a vote:-) – David Carlisle Oct 13 '15 at 22:37

In current LaTeX releases (since 2015/01/01) you only need


and the counter exc will be reset when subsection is either stepped or reset itself, i.e., also when section or chapter are stepped.

With older releases you can load the chngcntr package and then do


(Thanks to @egreg for the reminder!)

  • 2
    And with an older release, \usepackage{chngcntr} followed by \newcounter{exc}[subsection], \counterwithin*{exc}{chapter} and \counterwithin*{exc}{section}. – egreg Oct 13 '15 at 22:49
  • Thanks a lot for the quick response! -- I had tried that and it didn't work; probably because I had messed around with the \section command's definition... Now it works. -- Cheers to you all! Fabulous website, this!! – gigno-labati Oct 13 '15 at 22:52
  • as promised :-) – David Carlisle Oct 13 '15 at 23:21
  • @DavidCarlisle :) – clemens Oct 14 '15 at 7:01

Renew the \subsection command to reset the counter as well:

  • 3
    That should not be needed with current latex releases. – David Carlisle Oct 13 '15 at 22:35
  • @DavidCarlisle Thanks for the info...I didn't know that was the case. Unless there is an objection, I'll keep this answer up as an example of a more general approach. – Guho Oct 13 '15 at 22:47

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.