GeneralIssues with trying to hyperref Exercises and Solutions

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
awegawef
Posts: 3
Joined: Mon Sep 14, 2009 10:47 pm

Issues with trying to hyperref Exercises and Solutions

Post by awegawef »

Hey, I'm trying to use the hyperref package to link Exercises to Solution (and maybe hints) and vice versa. IE:

Exercise 1. Do this (Link to Hint) (Link to Solution)

and then the solutions page would have

Solution 1. This is the solution. (Link to Original Problem)

The code that I tried to throw together is as follows

Code: Select all

\theoremstyle{definition}
\newtheorem{exer}{Exercise}
\newtheorem{sol}{Solution}

\newcounter{exercises}
\newcounter{solutions}
\setcounter{exercises}{0}
\setcounter{solutions}{0}

\newcommand{\ex}{ %to start an exercise
	\begin{exer}
		\hypertarget{\value{exercises}-exer}{}
	}
\newcommand{\eex}{ % to end an exercise
		\hyperlink{\value{exercises}-sol}{Solution}
	\end{exer}
	\addtocounter{exercises}{1}
	}
\newcommand{\so}{ % start a solution
	\begin{sol}
		\hypertarget{\value{solutions}-sol}{}
	}
\newcommand{\eso}{ % end a solution
		\hyperlink{\value{solutions}-exer}{Exercise}
	\end{sol}
	\addtocounter{solutions}{1}
	}
What I tried to do was create my own counter, but what happens is that \value{exercises} and \value{solutions} are always printed out as nothing. Does anyone know how to fix this? I've researched but I haven't been able to figure out counters. I'm sure there is a better way to do this, too, so if anyone has input on that, please let me know.

Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Issues with trying to hyperref Exercises and Solutions

Post by frabjous »

I think it should work if you use \theexercises and \thesolutions instead of \value{solution} or \value{exercises}.

Also, I'd start your counters at 1, not 0, unless you want them not to match their labels.

E.g.:

Code: Select all

\documentclass{article}
\usepackage{amsthm}
\usepackage{hyperref}
\theoremstyle{definition}
\newtheorem{exer}{Exercise}
\newtheorem{sol}{Solution}

\newcounter{exercises}
\newcounter{solutions}
\setcounter{exercises}{1}
\setcounter{solutions}{1}

\newcommand{\ex}{ %to start an exercise
   \begin{exer}
      \hypertarget{\theexercises-exer}{}
   }
\newcommand{\eex}{ % to end an exercise
      \hyperlink{\theexercises-sol}{Solution}
   \end{exer}
   \addtocounter{exercises}{1}
   }
\newcommand{\so}{ % start a solution
   \begin{sol}
      \hypertarget{\thesolutions-sol}{}
   }
\newcommand{\eso}{ % end a solution
      \hyperlink{\thesolutions-exer}{Exercise}
   \end{sol}
   \addtocounter{solutions}{1}
   }
\begin{document}
\ex
This is exercise \theexercises.
\eex
\clearpage
\ex
This is exercise \theexercises.
\eex
\clearpage
\so
This is solution \thesolutions.
\eso 
\clearpage
\so
This is solution \thesolutions.
\eso 

\end{document}
awegawef
Posts: 3
Joined: Mon Sep 14, 2009 10:47 pm

Re: Issues with trying to hyperref Exercises and Solutions

Post by awegawef »

Thanks for the reply! Your method works great. I wasn't worried about the counters matching the labels because the labels are going to be specific to the subsections, ie Ex 2.1.1, Ex 2.1.2, etc. I don't think it really matters how the link numbers are chosen as long as the solutions appear sequentially at the end, which they will of course.

Once again, thanks for the help!
sitex
Posts: 70
Joined: Sat May 09, 2009 12:37 pm

Issues with trying to hyperref Exercises and Solutions

Post by sitex »

I am happy that the strategy posted by frabjous worked for you. If you are interested in a completely automated solution (one that automatically generates cross-references, etc) you can look at Don Story's exerquiz package which is a part of his AcroTeX eDucation Bundle (http://www.math.uakron.edu/~dpstory/webeq.html). I began using this free package when Don wrote it. It is easy to implement and provides for considerable flexibility. Your input is simply
<

Code: Select all

\begin{exercise}
  <statement of the exercise>
\begin{solution}
  <exercise solution>
\end{solution}
\end{exercise}
The rest is done by exerquiz. Options permit you to place some or all solutions after the exercises, at the end of the document, or omit the solutions entirely (in which case you can leave space for the student to work the problem). I have used eCard package to include hints.

Tom
Post Reply