4

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}
\hypersetup{colorlinks=true, linkcolor=blue}
\usepackage{lipsum}

\SetupExSheets{
    question/pre-body-hook = {%
        \hyperref[sol:\CurrentQuestionID]{Link to Solution}%
        \dots ref value is \ref{sol:\CurrentQuestionID}\\%
        },
    solution/pre-body-hook = {%
        \label{sol:\CurrentQuestionID}%
        \hyperref[qu:\CurrentQuestionID]{Link to Question}\\
    },
    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

\section{Answers}
\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.)

3

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 = {%
    \hyperlink{sol:\CurrentQuestionID}{Link to Solution
      \GetQuestionProperty{counter}{\CurrentQuestionID}}\par
  },
  solution/pre-hook = {
    \hypertarget{sol:\CurrentQuestionID}{}%
  } ,
  solution/pre-body-hook = {%
    \hyperref[qu:\CurrentQuestionID]{Link to Question
      \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 = {%
    \hyperlink{sol:\CurrentQuestionID}{Link to Solution
      \GetQuestionProperty{counter}{\CurrentQuestionID}}\par
  },
  solution/pre-hook = {
    \hypertarget{sol:\CurrentQuestionID}{}%
  } ,
  solution/pre-body-hook = {%
    \hyperref[qu:\CurrentQuestionID]{Link to Question
      \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

\section{Answers}
\printsolutions

\end{document}
  • Is it also possible that you can click directly on the "question" or "solution" without needing the additional text? – student Feb 6 '17 at 17:34
  • @student currently unfortunately not... – clemens Feb 8 '17 at 12:56

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.