8

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:


\documentclass{scrartcl}

\usepackage{etoolbox}

\newcounter{exc}[section]
%%% HERE OUGHT TO BE SOMETHING LIKE [section, subsection], IF YOU KNOW, WHAT I MEAN 

\newenvironment{exc}%
    % begin
    {\par%
    \refstepcounter{exc}% count
    \ifnumequal{\value{subsection}}{0}% find correct level
        % subsection level
        {\addxcontentsline{toc}{subsection}%
            {Ex.~\thesection.\theexc}% add to toc on subsection level
          \noindent\mbox{Exercise~\thesection.\theexc}\\[\baselineskip] 
        }%
        % subsubsection level
        {\addxcontentsline{toc}{subsubsection}%
            {Ex.~\thesubsection.\theexc}% add to toc on subsubsection level
          \noindent\mbox{Exercise~\thesubsection.\theexc}\\[\baselineskip]
        }%
    }%
    % end
    {\par\clearpage}

\begin{document}

\tableofcontents

\section{Hello}

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

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

\section{World}

\subsection{my little world}

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

\subsection{your big life}

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

\section{Bye!}

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

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

2 Answers 2

9

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

\newcounter{exc}[subsection]

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

\newcounter{exc}[subsection]
\counterwithin*{exc}{chapter}
\counterwithin*{exc}{section}

(Thanks to @egreg for the reminder!)

4
  • 2
    And with an older release, \usepackage{chngcntr} followed by \newcounter{exc}[subsection], \counterwithin*{exc}{chapter} and \counterwithin*{exc}{section}.
    – egreg
    Oct 13, 2015 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!! Oct 13, 2015 at 22:52
  • as promised :-) Oct 13, 2015 at 23:21
  • @DavidCarlisle :)
    – cgnieder
    Oct 14, 2015 at 7:01
2

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

\let\oldsubsection\subsection
\renewcommand\subsection{\setcounter{exc}{0}\oldsubsection}
2
  • 3
    That should not be needed with current latex releases. Oct 13, 2015 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, 2015 at 22:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .