# exsheets: Link Solutions back to Questions

Using exsheets, I'd like to link questions to their solutions and solutions back to their questions. This issue is also discussed in the question How to automatically links questions and solutions with the Exsheets package?, although the accepted answer only solves half the problem (namely, the solution-->question linkage).

I've tried to use solution/pre-body-hook=\label{sol:\CurrentQuestionID} to label the solutions, then insert links to them using question/pre-body-hook. (This approach was suggested as a possible approach in the question I linked above).

My issue is that none of the solution labels seem to be pointing to the right place. Namely, they all point to the solutions section, rather than to the individual solution. See full code below. (Note: version 0.19 of exsheets is required.)

\documentclass{article}
\usepackage[auto-label]{exsheets} %Requires version 0.19
\usepackage{hyperref}
\usepackage{lipsum}

\SetupExSheets{
question/pre-body-hook = {%
\dots ref value is \ref{sol:\CurrentQuestionID}\\%
},
solution/pre-body-hook = {%
\label{sol:\CurrentQuestionID}%
},
solution/post-body-hook = \newpage,
}

\begin{document}

\section{Problems}
\begin{question}
\lipsum[1]
\end{question}
\begin{solution}
\lipsum[2]
\end{solution}

\begin{question}
\lipsum[3]
\end{question}
\begin{solution}
\lipsum[4]
\end{solution}

\newpage

\printsolutions

\end{document}


Amusingly, if I use

solution/pre-body-hook = {%
\begin{align}\label{sol:\CurrentQuestionID}
1+1=2
\end{align}
}


then the question-->solution link works correctly. (Of course, I don't want a silly equation at the beginning of each solution.)

• – clemens Nov 2 '15 at 23:02
• @clemens I have, it is linked in my question. That question currently only links Solutions --> Questions. It does not link Questions --> Solutions. My question is about implementing the suggestion you made in a comment. – Chris Chudzicki Nov 2 '15 at 23:18
• Oh, alright (it's late on this side of the world :) ). – clemens Nov 2 '15 at 23:21
• @clemens I was hoping you would see this, but didn't expect anything till (my) tomorrow =). – Chris Chudzicki Nov 2 '15 at 23:23

Solutions have no own counter so setting a \label is of no use. A \label always sets a reference to the last counter that's been stepped with \refstepcounter. That's why your example with the equation seems to work. But it doesn't: the label refers to the equation rather than the solution.

What you can do is add a \hypertarget and link to that with \hyperlink.

The corresponding question number is available as question property with the name counter. Something like Link to Solution \GetQuestionProperty{counter}{\CurrentQuestionID} should give you the link text you want:

\SetupExSheets{
question/pre-body-hook = {%
\GetQuestionProperty{counter}{\CurrentQuestionID}}\par
},
solution/pre-hook = {
\hypertarget{sol:\CurrentQuestionID}{}%
} ,
solution/pre-body-hook = {%
\GetQuestionProperty{counter}{\CurrentQuestionID}}\par
}
}


A complete document:

\documentclass{article}
\usepackage[auto-label]{exsheets} %Requires version 0.19
\usepackage{hyperref}
\usepackage{lipsum}

\SetupExSheets{
question/pre-body-hook = {%
\GetQuestionProperty{counter}{\CurrentQuestionID}}\par
},
solution/pre-hook = {
\hypertarget{sol:\CurrentQuestionID}{}%
} ,
solution/pre-body-hook = {%
\GetQuestionProperty{counter}{\CurrentQuestionID}}\par
}
}

\begin{document}

\section{Problems}
\begin{question}
\lipsum[1]
\end{question}
\begin{solution}
\lipsum[2]
\end{solution}

\begin{question}
\lipsum[3]
\end{question}
\begin{solution}
\lipsum[4]
\end{solution}

\newpage