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}
\thmboxoptions{headstyle=\bfseries\boldmath#1 #2~,thickness=0.6pt, cut=false}
\newtheorem[M]{exc}[moncompteur]{ \textbf{Exercise}}{}
\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:


  {\par\bigskip\noindent{\bfseries \hypertarget{ex:#1}{}\hyperlink{sol:#1}{Solution  of exercise #1}}\quad}

First exercise.
First solution.

Second exercise.
Second solution.

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:

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

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


Here is the solution. \label{sol:first}

Here is the solution. \label{sol:second}
share|improve this answer

Your Answer


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.