[Tex/LaTex] Hyperlink with answers package

answersexerciseshyperref

I'm using the answers package for an exercises book. I would like to create clickable hyperlinks (in the generated pdf file) from the exercise to the corresponding solution and back from the solution to the exercise.

Here is my ex (exercise) environment:

\documentclass[12pt, openany]{extbook}
\usepackage{pstricks,pstricks-add,pst-math,pst-xkey}
\usepackage[frenchb]{babel}
\usepackage{slashbox}
\usepackage{graphicx}
\usepackage{amsmath,amssymb,amstext}
\usepackage[latin1]{inputenc}
\usepackage[OT1]{fontenc}
\usepackage{fancybox}
\usepackage{a4wide}
\usepackage{rotating}
\usepackage{epic}
\usepackage{answers}
\usepackage{fancyvrb}
\usepackage{thmbox}
\thmboxoptions{headstyle=\bfseries\boldmath#1 #2~,thickness=0.6pt, cut=false}
\usepackage[hidelinks]{hyperref}
\newcounter{moncompteur}
\newtheorem[M]{exc}[moncompteur]{ \textbf{Exercise}}{}
\newenvironment{ex}{\begin{exc}\normalfont}{\end{exc}}
\Newassociation{sol}{Soln}{corr}
\renewenvironment{Soln}[1]{\par\bigskip\noindent {\bfseries Solution  of exercise #1}\quad}{}

Best Answer

You can use the \hyperlink, \hypertarget mechanism provided by the hyperref package. A little example:

\documentclass[12pt,a4paper]{article}
\usepackage{answers}
\usepackage{amsthm}
\usepackage{hyperref}

\newcounter{moncompteur}
\theoremstyle{definition}
\newtheorem{ex}{%
  \hyperlink{ex:\theex}{Exercise}\hypertarget{sol:\theex}{}}
\Newassociation{sol}{Soln}{corr}
\renewenvironment{Soln}[1]
  {\par\bigskip\noindent{\bfseries \hypertarget{ex:#1}{}\hyperlink{sol:#1}{Solution  of exercise #1}}\quad}
  {\par\bigskip}

\begin{document}
\Opensolutionfile{corr}[ans1]
\section{Problems}
\begin{ex}
First exercise.
\begin{sol}
First solution.
\end{sol}
\end{ex}

\begin{ex}
Second exercise.
\begin{sol}
Second solution.
\end{sol}
\end{ex}
\Closesolutionfile{corr}
\section{Solutions}
\input{ans1}
\end{document}

Note that I modified the definition of the ex theorem-like structure using \theoremstyle{definition} from the amsthm package and removed the counter moncompteur.

Related Question