7

I'm using exercise.sty and have two Exercise counters I'm trying to use, one for Examples with in a chapter, and one for Problems for the chapter. Here's an example:

\documentclass[]{scrreprt}
\usepackage[]{exercise}
\usepackage[svgnames]{xcolor}
\usepackage[]{chngcntr}
\newcounter{Example}
\counterwithin{Example}{chapter}
\counterwithin{Exercise}{chapter}

\newcommand{\makeproblem}[3]{%
\begin{Exercise}[ title={#1}, label={#2} ]%
#3%
\end{Exercise}%
}

\newcommand{\makeexample}[3]{%
\begin{Exercise}[ title={#1}, label={#2}, name={Example}, counter={Example} ]%
#3%
\end{Exercise}%
}

\begin{document}

\chapter{Ch1.}

   \makeexample{blah}{ch1:ex1}{
   Given $b=e$, compute $foo(b)$...
   }

   \makeexample{another}{ch1:ex2}{
   Given $b=f$, compute $foo(b)$...
   }

   \makeproblem{compute foo}{ch1:problem:foo}{
   Given $b=a$, compute $foo(b)$.
   }

   \makeproblem{compute bar}{ch1:problem:bar}{
   Given $b=a$, compute $bar(b)$.
   }

\end{document}

However, in the output:

with basic formatting

notice how the counters for the Examples are not shown incrementing (even within one chapter).

EDIT: noticed only after the fact that my example without any change to the formatting also didn't show incrementing Example numbers. Deleted all parts of the question about my formatting modifications.

EDIT2: This issue appears to be due to a mis-interaction of the chngcntr package with the exercise package once a non-default counter is used. Is there any way to deal with this?

Peeter Joot
  • 3,025
  • 2
  • 25
  • 35

1 Answers1

4

You could use \renewcounter, provided by the same package, and redefine \theExercise:

\renewcounter{Exercise}[chapter]
\makeatletter
\renewcommand{\theExercise}{\if@ExeStared\else\thechapter.\arabic{\@ExerciseCounter}\fi}
\makeatother
\newcounter{Example}
\renewcounter{Example}[chapter]

Internally, the package uses \@ExerciseCounter (initialized with Exercise) with \theExercise, that's why just redefining \theExample doesn't work.

Stefan Kottwitz
  • 231,401
  • Thanks. It wasn't clear to me from the documentation how to use that renewcounter command (written for a much more sofisticated latex user than myself). I'd tried, but must have had the syntax wrong. – Peeter Joot May 22 '12 at 17:44