2

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) enter image description here

\documentclass[11pt]{scrbook}

\usepackage{marginnote}
\usepackage{xcolor}
\usepackage[linkcolor=red,urlcolor=red]{hyperref}

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

\normalmarginpar
\marginnote{\footnotesize\sffamily
[Sol~\refAnswer{\ExerciseLabel}]}
}{%
\end{Exercise}
}
%
\newenvironment{sol}{\begin{Answer}[ref=\ExerciseLabel]}
{\end{Answer}
}


\begin{document}

\chapter{ABCD}

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


\section{Solutions}
\shipoutAnswer

\chapter{ABCD}

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

\section{Solutions}
\shipoutAnswer

\end{document}
2

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[linkcolor=red,urlcolor=red]{hyperref}
\usepackage{ifthen}

\usepackage[answerdelayed]{exercise}
\renewcounter{Exercise}[chapter]
\renewcounter{Answer}[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
        \ifthenelse{\boolean{#2}}{\marginnote{\footnotesize\sffamily [Sol~\refAnswer{\ExerciseLabel}]}}{}
    }{%
    \end{Exercise}
}
%
\newenvironment{sol}{\begin{Answer}[ref=\ExerciseLabel]}
    {\end{Answer}
}


\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}
    \shipoutAnswer

    \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}
    \shipoutAnswer

\end{document}

You can change the ifthenelse to something more advanced like the package xargs if you want to customize the parameters, have default values, etc. There are numerous examples on how to use this.

  • Thank you geguze ! I have completed my question with a second part : if there is no answer, would it be possible to hide [sol ****] in the right margin ? – jowe_19 Jun 16 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.