7

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. – clemens Jan 12 '15 at 22:45
6

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]

\DeclareInstance{exsheets-heading}{myblock}{default}{
  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}

Original answer

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}

\DeclareInstance{exsheets-heading}{myblock}{default}{
  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}

\section{Answers}
\SetupExSheets{headings=myblock}
\printsolutions

\end{document}

enter image description here

  • Thanks! In the direction solution->question this does exactly what I was looking for. Ideally, I'd like to place a link to the solution somewhere in the question as well (e.g. in the heading or post-hook). But the solutions are not auto-labelled, are they? – lukasimir Jan 13 '15 at 7:33
  • 1
    That's true, there isn't. There are reasons for this: since it it possible for a solution to be printed more than once this could result in multiple labels; also solutions don't have a counter that gets stepped so there is nothing to refer to other than the page. – clemens Jan 13 '15 at 8:55
  • That being said it should be possible with the fix in my answer and the question and solution hooks to build something. – clemens Jan 13 '15 at 8:56
  • @lukasimir I updated the answer – clemens Jul 5 '15 at 22:04
  • @clemens How can I add links from all Exercises to solutions and reverse? – minthao_2011 Jul 9 '16 at 0:53

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.