# How to automatically link questions and solutions with the Exsheets package?

I'm writing up some lecture notes that contain questions/problems for the reader scattered accross the text. The solutions are provided in the end of each chapter. Exsheets appears to be a nice package for doing so.

Students will use the document mainly electronically and so I'd like to hyperlink each question to the corresponding answer and vice-versa. Now exsheets has the option to auto-label each question with \label{qu:id} and the id is generated automatically as well. So far so good.

Using the headings, solution/pre-hook or solution/post-hook options I tried referencing the corresponding label, but don't manage to construct it. I tried using \CurrentQuestionID which, however, is empty when in the solution environment.

Any ideas how to accomplish the cross-linking automatically? Thanks!

• This is a missing feature I guess. – cgnieder Jan 12 '15 at 22:45

Edit

With version 0.19 (2015/07/04) the hack isn't necessary anymore. The following preamble to the code below suffices:

\documentclass{article}
\usepackage[auto-label]{exsheets}[2015/07/04]

attach = {
main[l,vc]title[l,vc](0pt,0pt) ;
main[r,vc]points[l,vc](\marginparsep,0pt)
} ,
title-post-code = \bfseries\space
to question \GetQuestionProperty{ref}{\CurrentQuestionID}
}

\usepackage{hyperref}

\begin{document}


The problem your having is (I guess) that when the solutions are printed \CurrentQuestionID doesn't hold the corresponding ID but in fact expands to an empty token list. This is probably a missing feature or even some kind of bug since arguably \CurrentQuestionID should work in for the solutions as well...

Here is a suggestion which will probably find its way into exsheets in the next few days:

\documentclass{article}
\usepackage[auto-label]{exsheets}

attach = {
main[l,vc]title[l,vc](0pt,0pt) ;
main[r,vc]points[l,vc](\marginparsep,0pt)
} ,
title-post-code = \bfseries\space
to question \GetQuestionProperty{ref}{\CurrentQuestionID}
}

\ExplSyntaxOn
\cs_set_protected:Npn \__exsheets_print_solution:nnnn #1#2#3#4
{
\tl_set:Nn \CurrentQuestionID {#3} % <<< this is needed
\tl_use:N \l__exsheets_solutions_pre_hook_tl
\exsheets_solutions_print_name:nnn {#1} {#2} {#3}
\tl_use:N \l__exsheets_solutions_pre_body_hook_tl
\use:n {#4}
\tl_use:N \l__exsheets_solutions_post_body_hook_tl
\tl_use:N \l__exsheets_solutions_post_hook_tl
\exsheets_h_or_vspace:N \l__exsheets_solutions_skip_below_dim
}
\ExplSyntaxOff

\usepackage{hyperref}

\begin{document}

\section{Problems}
\begin{question}
foo bar baz
\end{question}
\begin{solution}
blah blah
\end{solution}
\begin{question}
foo bar baz
\end{question}
\begin{solution}
blah blah
\end{solution}