Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}{}
share|improve this question
    
Welcome to TeX.sx! A tip: If you indent lines by 4 spaces, then they are marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). Also, You can use backticks ` to mark your inline code‌​. –  Gonzalo Medina Aug 25 '11 at 19:07
    
Thanks, you were faster than me. I was editing! –  Brownian Aug 25 '11 at 19:10

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Thanks a lot: you made my day (I went back to my definition of ex in order to use thmbox). –  Brownian Aug 25 '11 at 19:54

Add hyperref's \phantomsection to your definition of the Soln environment:

\renewenvironment{Soln}[1]{\par\bigskip\noindent\phantomsection {\bfseries Solution  of exercise #1}\quad}{}

Now, in your document, using \hyperref[<reference>]{<text>} will jump you to the appropriate hyperlink/label given for the solution. For example, try the following:

\begin{document}
\begin{ex}
Here is an exercise. Solution is \hyperref[sol:first]{here}. \label{ex:first}
\end{ex}

\begin{ex}
Here is another exercise. Solution is \hyperref[sol:second]{here}. \label{ex:second}
\end{ex}

\newpage

\begin{Soln}{\ref{ex:first}}
Here is the solution. \label{sol:first}
\end{Soln}

\begin{Soln}{\ref{ex:second}}
Here is the solution. \label{sol:second}
\end{Soln}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.