[Tex/LaTex] customized exercise counter that considers section

countersexercises

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:

enter image description here

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.

Best Answer

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

\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}{\addcontentsline}%
  {\if@ExeStared\else\addcontentsline}{}{Failed}
\patchcmd{\@@@ExeEnv}{\endgroup}{\fi\endgroup}{}{Failed}
\renewcommand{\ExerciseHeaderNB}{\csname the\@ExerciseCounter\endcsname}
\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
  \Answer Hard
  \Exercise[counter=pbone] Interesting
  \Answer Long
\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.

Related Question