# Exercise package : link between exercises and answers. Issue with chapters

I would like to create links between answers and exercices using the exercise package. I reset the exercise counter at the beginning of a chapter.

But sometimes the hyperlink is not valid (shuffles between chapters). For example, in the following MWE, [Sol 1] after the exercise 1 of chapter 1 points to the solution of the exercise 1 in chapter 2. How could I fix it ? Moreover, if there is no answer, would it be possible to hide [sol ****] in the right margin ? ([Sol 3] for exercises 1 and 2 in the MWE)

\documentclass[11pt]{scrbook}

\usepackage{marginnote}
\usepackage{xcolor}

\renewcounter{Exercise}[chapter]
\renewcommand{\theExercise}{\arabic{Exercise}}
%
\newenvironment{exo}[1][\unskip]{%
\begin{Exercise}[title={#1},label={\the\value{Exercise}}]

\normalmarginpar
\marginnote{\footnotesize\sffamily
}{%
\end{Exercise}
}
%
}

\begin{document}

\chapter{ABCD}

\begin{exo}
ABCD
\end{exo}
\begin{sol}
ABD
\end{sol}

\section{Solutions}

\chapter{ABCD}

\begin{exo}
ABCD
\end{exo}
\begin{sol}
ABD
\end{sol}

\section{Solutions}

\end{document}

The line

\renewcounter{Exercise}[chapter]

resets the counter of the exercise every time the chapter counter increases. This gives you the numbering of the exercise to be always 1 even if the chapter increases. However by doing this all the labels of the exercise are the same.

To solve the problem, you just need to change

\begin{Exercise}[title={#1},label={\the\value{Exercise}}]

to

\begin{Exercise}[title={#1},label={\the\value{chapter}}]

so that the right increasing numbering (with the chapter) is obtained.

EDIT

I have been a bit sloppy in the response above, as I really only fixed the very specific problem you mentioned but I did not look into the situation where you have multiple exercises/answers in each chapter. I changed the code a bit to fix that and added the possibility of having/not having the Sol. printed at the margin. See if this works:

\documentclass[11pt]{scrbook}

\usepackage{marginnote}
\usepackage{xcolor}
\usepackage{ifthen}

\renewcounter{Exercise}[chapter]
\renewcommand{\theExercise}{\arabic{Exercise}}
\newcounter{myexerciselabel}
\newcounter{myexercisenumber}[chapter]
%
\newenvironment{exo}[2][\unskip]{\stepcounter{myexerciselabel}\stepcounter{myexercisenumber}
\begin{Exercise}[title={#1},label={\the\value{myexerciselabel}}, number={\the\value{myexercisenumber}}, counter={myexercisenumber}]
\normalmarginpar
}{%
\end{Exercise}
}
%
}

\begin{document}

\chapter{ABCD}

\begin{exo}[Shows Sol]{true}
ABCD
\end{exo}
\begin{sol}
ABD
\end{sol}

\begin{exo}[No Sol]{false}
EFGH
\end{exo}
\begin{sol} % THIS WOULD NOT BE INCLUDED
EFG
\end{sol}

\begin{exo}[Shows Sol]{true}
HIJK
\end{exo}
\begin{sol}
HIJ
\end{sol}

\section{Solutions}

\chapter{ABCD}

\begin{exo}[No Sol]{false}
ABCD
\end{exo}
\begin{sol}  % THIS WOULD NOT BE INCLUDED
ABD
\end{sol}

\begin{exo}[Shows Sol]{true}
EFGH
\end{exo}
\begin{sol}
EFG
\end{sol}

\section{Solutions}