# creating hyperlinks from exercises to solutions, when not all exercises have solutions

I'm new to LaTeX and completing someone else's work. I'm using MiKTeX 2.9 on Windows 10.

I am trying to create clickable hyperlinks from in-text exercises to their solutions in an appendix. I would like to not have to define a solution environment or use the answers package, as only a random selection of exercises have solutions and I'd like the exercise and solution numbers to match up. Currently I'm using hyperref to refer to exercises from their solutions, but I don't know how to label the solutions and reference them in the exercises as well without messing up the numbering.

Here's what I'm working with:

\documentclass{article}

\newtheorem{exercise}{Exercise}

\begin{document}

\begin{exercise}\label{ex:one}
exercise text
\end{exercise}

\begin{exercise}
exercise without solution
\end{exercise}

\begin{exercise}\label{ex:three}
exercise text
\end{exercise}

{Exercise}~\ref{ex:one}
solution text

{Exercise}~\ref{ex:three}
solution text

\end{document}


Any advice would be much appreciated.

• Welcome to TeX.SX! Can you please provide a minimal working example (MWE) of what you have so far? This will help us help you. – sk8forether Jun 19 '17 at 2:09
• I don't understand this sentence: “I would like to not have to define a solution environment or use the answers package, as only a random selection of exercises have solutions and I'd like the exercise and solution numbers to match up.” I don't get what either environments or the answers package have to do with possibly not matching numbers of exercises and solutions… – clemens Jun 19 '17 at 7:38
• In my (very brief) attempt at creating a solution environment, it automatically numbered every solution and I think the situation is the same with the answers package. As I have many more exercises than solutions, I'm worried that with the exercise environment and the solution environment both independently numbering, the solution numbers won't correspond with the exercise numbers. It's possible that I just misunderstand what can and can't be done though. – Maria Jun 19 '17 at 12:10
• @Maria I don't know about the answers package but I do know that this is very well possible with other packages like (my own) xsim. It even has an example on how to add cross-references (BTW: we only get notified of comments if you add @<username> to the comment – clemens Jun 19 '17 at 13:49
• I'll add an answer using the xsim package if you like… – clemens Jun 19 '17 at 13:55

There is \hyperlink and \hypertaget. In many ways they are superior to \label and \ref. Besides, using labels which incorporate the counter values sort of makes the whole use of counters pointless.

I dug through source2e (available from ctan) to use the same formatting as \newtheorem.

\documentclass{article}

\newcounter{exercise}

\newenvironment{exercise}[1][\empty]{% #1 = answer target (optional)
\refstepcounter{exercise}%
\trivlist
\ifx\empty#1\relax
\item[\hskip \labelsep{\bfseries Exercise \theexercise}]\itshape
\else
\fi}{\endtrivlist}

\begin{document}
\begin{exercise}[ans:one]\label{ex:one}
exercise text
\end{exercise}

\begin{exercise}
exercise without solution
\end{exercise}

\begin{exercise}[ans:three]\label{ex:three}
exercise text
\end{exercise}

\hypertarget{ans:one}{Exercise}~\ref{ex:one}
solution text

\hypertarget{ans:three}{Exercise}~\ref{ex:three}
solution text

\end{document}

• Thank you. In my document itself, exercises are labelled by topic and do not incorporate the counter values. Do you know of a way to link directly from the exercise number so the user can just click on "Exercise 1" rather than having the "answer" link below the exercise? – Maria Jun 19 '17 at 12:22
• It would be easier just to create a new environment directly rather than use \newtheorem and replace/edit the environment thus created. – John Kormylo Jun 19 '17 at 13:06