# How to assign TWO parents to LaTeX counter?

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
{Ex.~\thesection.\theexc}% add to toc on subsection level
\noindent\mbox{Exercise~\thesection.\theexc}\\[\baselineskip]
}%
% subsubsection level
{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}

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

\section{Bye!}

\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

\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!)

• 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:

\let\oldsubsection\subsection
\renewcommand\subsection{\setcounter{exc}{0}\oldsubsection}
`
• 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