customized exercise counter that considers section

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

\documentclass{article}

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

\begin{document}

\newcounter{pbone}
\renewcommand*{\thepbone}{%
\textbf{%
\thesection.\arabic{pbone}
}%
}

\begin{Exercise}[counter={pbone}]
Test
\end{Exercise}

\end{document}


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:

UPDATE

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.

• Possible duplicate of Number exercises according to chapter – Eric Domenjoud Nov 24 '16 at 12:24
• That didn't help me. – aaragon Nov 24 '16 at 12:52
• 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 '16 at 14:31

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.

\documentclass{article}

\usepackage{etoolbox}
\usepackage[lastexercise]{exercise}

\newcounter{pbone}[section]
\renewcommand*{\thepbone}{\thesection.\arabic{pbone}}

\renewcommand{\theExercise}{\arabic{Exercise}}
\makeatletter
\patchcmd{\@@@ExeEnv}{\theExercise}%
{\csname the\@ExerciseCounter\endcsname}{}{Failed}
\patchcmd{\@@@ExeEnv}{\endgroup}{\fi\endgroup}{}{Failed}
\patchcmd{\@getExerciseInfo}{\p@Exercise\theExercise}%
{\csname p@\@ExerciseCounter\endcsname
\csname the\@ExerciseCounter\endcsname}{}{Failed}
\patchcmd{\@@@ExeCmd}{\theExercise}%
{\csname the\@ExerciseCounter\endcsname}{}{Failed}
\makeatother

\begin{document}

\listofexercises

\section{A section}

Now in first section.

\begin{Exercise}
\label{ex:1}
Standard counter.
\end{Exercise}

\begin{Exercise}[counter=pbone,label=a]
\label{ex:2}
Test with custom number.
\end{Exercise}

\section{Another section}

Now in second section.

\begin{Exercise}[counter=pbone]
\label{ex:3}
Test with custom number.
\end{Exercise}

\begin{Exercise}[label=b]
\label{ex:4}
Standard exercise.
\end{Exercise}

\begin{Exercise*}
Test of star.
\end{Exercise*}

\begin{Exercise}[counter=pbone]
\label{ex:5}
Test with custom number.
\end{Exercise}

\bigbreak
Testing an exercise list wiht answers.
\bigbreak

\begin{ExerciseList}
\Exercise Simple
\Exercise[counter=pbone] Interesting
\end{ExerciseList}

First~\ref{ex:1};
second~\ref{ex:2};
third~\ref{ex:3};
forth~\ref{ex:4};
fifth~\ref{ex:5}.

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

\end{document}


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 '16 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. – Andrew Swann Nov 25 '16 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.

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

\renewcounter{Exercise}[section]
\renewcommand\theExercise{\textbf{\thesection.\arabic{Exercise}}}
\begin{document}

\section{Section 1}

\begin{Exercise}[label=ex:1.1]
Test 1.1
\end{Exercise}

\begin{Exercise}[label=ex:1.2]
Test 1.2
\end{Exercise}

\section{Section 2}

\begin{Exercise}[label=ex:2.1]
Test 2.1
\end{Exercise}

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


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