TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 7 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
    
@GonzaloMedina I've been trying to do the same with thmtools' hooks, which one can define in declaretheorem. Something like postheadhook={\hypertarget{exe:\theexercise}{}}, etc., but the links and targets are all wrong. Is there a way to make it work that way? – Dominique Sep 20 '15 at 15:46
    
@Dominique Please feel free to open a fresh new question. – Gonzalo Medina Sep 20 '15 at 16:53
    
@GonzaloMedina How can I \newtheorem{ex}[section] and Can I use your code with ntheorem package? – minthao_2011 Jun 28 at 13:19

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.