3

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}

\usepackage[colorlinks, backref]{hyperref}

\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.

3
  • 1
    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 '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…
    – cgnieder
    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
2

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}
\usepackage[colorlinks, backref]{hyperref}

\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
    \item[\hskip \labelsep{\bfseries Exercise \hyperlink{#1}{\theexercise}}]\itshape
  \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}
2
  • 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. Jun 19 '17 at 13:06
1

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}
\usepackage{answers}

\Newassociation{solution}{Solution}{answers}
\newtheorem{exercise}{Exercise}

\begin{document}

\Opensolutionfile{answers}
\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}
\Closesolutionfile{answers}

\section{Solutions}
\input{answers}

\end{document}

enter image description here

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{answers}
\usepackage{hyperref}

\Newassociation{solution}{Solution}{answers}
\newtheorem{exercise}{\hyperlink{ex:\theexercise}{Exercise}\hypertarget{sol:\theexercise}{}}
\renewcommand{\solutionstyle}[1]{\textbf{\hyperlink{sol:#1}{Exercise} \hypertarget{ex:#1}{#1}}}

\let\oldhypertarget\hypertarget
\let\oldhyperlink\hyperlink
\renewcommand{\hypertarget}[2]{%
  \label{ht@#1}\oldhypertarget{#1}{#2}%
}
\renewcommand{\hyperlink}[2]{%
  \ifcsname r@ht@#1\endcsname\oldhyperlink{#1}{#2}\else#2\fi
}

\begin{document}

\Opensolutionfile{answers}
\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}
\Closesolutionfile{answers}

\section{Solutions}
\input{answers}

\end{document}

enter image description here

0

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.