# 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. Jun 19, 2017 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… Jun 19, 2017 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. Jun 19, 2017 at 12:10

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? Jun 19, 2017 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. Jun 19, 2017 at 13:06

I think you premise is wrong:

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.

They do math up automatically with the answers package:

\documentclass{article}

\newtheorem{exercise}{Exercise}

\begin{document}

\section{Problems}
\begin{exercise}
First exercise
\begin{solution}
First solution.
\end{solution}
\end{exercise}
\begin{exercise}
Second exercise
\end{exercise}
\begin{exercise}
Third exercise
\begin{solution}
Third solution.
\end{solution}
\end{exercise}

\section{Solutions}

\end{document}


And adding hyperlinks is not that hard, either. And in order to avoid warnings about inexistent hypertargerts we can use @Werner's solution from here

\documentclass{article}
\usepackage{hyperref}

\let\oldhypertarget\hypertarget
\renewcommand{\hypertarget}[2]{%
\label{ht@#1}\oldhypertarget{#1}{#2}%
}
}

\begin{document}

\section{Problems}
\begin{exercise}
First exercise
\begin{solution}
First solution.
\end{solution}
\end{exercise}
\begin{exercise}
Second exercise
\end{exercise}
\begin{exercise}
Third exercise
\begin{solution}
Third solution.
\end{solution}
\end{exercise}