I'm trying to get a simple exercise counter that considers the section. This is the code I have so far:


\usepackage[lastexercise]{exercise} % for exercise environment





Even though I have defined the counter properly, and if I use the command \thepbone I get the right number, when I use it with the exercise environment I get:

enter image description here


This post does not help me solve the problem because I need to use a custom counter. In the document I'm preparing, I use the exercise environment not only for exercises but also for problems at the end of each chapter. For exercises I don't provide a counter as an argument, but for the exercises at the end of the chapter I provide a custom counter as a parameter.

  • 5
    Possible duplicate of Number exercises according to chapter Nov 24, 2016 at 12:24
  • That didn't help me.
    – aaragon
    Nov 24, 2016 at 12:52
  • 1
    Please show us in the given MWE what you are doing. Then we can help you to get an better code. Otherwise we can only guess what you are trying and that is not very helpful ...
    – Mensch
    Nov 24, 2016 at 14:31

2 Answers 2


The internals of the exercise package are not well designed for this and need some patching to print custom counters correctly in all situations (the heading of the Exercise, the entry in the list of exercises and in any cross references). Here is such a collection of patches in a test document.

Sample output




  {\csname the\@ExerciseCounter\endcsname}{}{Failed}
\renewcommand{\ExerciseHeaderNB}{\csname the\@ExerciseCounter\endcsname}
  {\csname p@\@ExerciseCounter\endcsname
  \csname the\@ExerciseCounter\endcsname}{}{Failed}
  {\csname the\@ExerciseCounter\endcsname}{}{Failed}



\section{A section}

Now in first section.

  Standard counter.

 Test with custom number.

\section{Another section}

Now in second section.

 Test with custom number.

  Standard exercise.

  Test of star.

  Test with custom number.

Testing an exercise list wiht answers.

  \Exercise Simple
  \Answer Hard
  \Exercise[counter=pbone] Interesting
  \Answer Long


Label \ref{a}; label \ref{b}.


The main problem is that the package defines \theExercise to do something other than just produce a printed form for the Exercise counter. In most situations was is needed is a command producing the printed representation of the counter for this environment. The name of that counter is stored in \@ExerciseCounter and the printed form should be accessed via a macro \the... where ... is that counter name; one can get this by the construct

\csname the\@ExerciseCounter\endcsname

Most of the patches above implement this, replacing instances of \theExercise with the above code. At another point the associated macro \p@Exercise is used and this has been patched too to adjust to the chosen counter.

Lastly the starred form Exercise* of the environment did not behave as advertised, it produced an entry in the list of exercises, I have fixed this too via the last two patches applied to \@@@ExeEnv.

Note that I have adjusted your definition of \thepbone to only produces the relevant characters. The bold face formatting should only be applied when it is used in a bold context, not at every reference.

  • Do you think it may be possible to use something along the lines of the possible duplicate answer? There they use the chngcntr. I'm not saying that your solution is wrong, but it seems complicated and I may have issues when getting the code into the document.
    – aaragon
    Nov 25, 2016 at 11:27
  • My solution is complicated because the package messes up the definition of \theEquation using it in a way not compatible with standard macros associated to counters. If you are just interested in having one type of exercise with one numbering scheme then the solutions to the other question are fine. If you need to mix types, then you need something like the patches above. The package should really be rewritten: spacing around ExerciseList is terrible, etc. etc. Nov 25, 2016 at 14:39

Ok. My first solution did not work as expected. You must use the \renewcounter command provided by the package and redefine \theExercise. You don't need another counter.

\usepackage[lastexercise]{exercise} % for exercise environment


\section{Section 1}

Test 1.1

Test 1.2

\section{Section 2}

Test 2.1

\ref{ex:1.1} \ref{ex:1.2} \ref{ex:2.1}

enter image description here

  • But then I need to refer to the problem using \ref and I don't get the right number, correct?
    – aaragon
    Nov 24, 2016 at 11:49
  • @aaragon. You are right. I edited my answer. Nov 24, 2016 at 12:11
  • And this has to be done at each section?
    – aaragon
    Nov 24, 2016 at 12:40

You must log in to answer this question.

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