1

I read Hyperlink with answers and have tried to apply the technique to my environments that I have defined using the ntheorem package.

In the MWE below the answer links correctly go to the questions, but the questions do not go to the answers. How can this be fixed?

\documentclass{report}
\usepackage{answers}
\usepackage{ntheorem}
\usepackage{changepage}
\usepackage{lipsum}
\usepackage{hyperref}

\makeatletter
\newtheoremstyle{hanging}%
{\item[\theorem@headerfont \llap{\hyperlink{soln:##2}{##2}}]}% 
\makeatother

% problem environment
\newlength{\problabel}
\theoremstyle{hanging}
\theoremprework{%
\hypertarget{prob:\theproblem}{}%
\settowidth{\problabel}{\textbf{\theproblem} }%
\begin{adjustwidth}{\problabel}{}}
\theorempostwork{\end{adjustwidth}}
\newtheorem{problem}{}

% solution environment
\Newassociation{sol}{Soln}{solutionfilehandle}
\renewenvironment{Soln}[1]
  {\par{ \hyperlink{prob:#1}{#1}}\quad\hypertarget{soln:#1}{}}%
  {}

\begin{document}

\Opensolutionfile{solutionfilehandle}[ans1]

\section{Problems}
\begin{problem}
    \lipsum[1]
    \begin{sol}
        First solution.
    \end{sol}
\end{problem}

\begin{problem}
    \lipsum[2]
    \begin{sol}
        second solution.
    \end{sol}
\end{problem}

\Closesolutionfile{solutionfilehandle}
\newpage
\section{Solutions}
\input{ans1}
    \end{document}

EDIT: It also helped to have the target first:

    % solution environment
\Newassociation{sol}{Soln}{solutionfilehandle}
\renewenvironment{Soln}[1]
  {\par\hypertarget{soln:#1}{}\hyperlink{prob:#1}{#1}\quad}%
  {}
1

I compiled your MWE (TeXlive 2011), and it worked just fine: questions link to answers, and answers link to questions. Did you make a rerun? Could you add a \listfiles before \begin{document} and post the result (especially: which hyperref version did you use)?

  • Thanks very much, that did it. I also changed my compile method from pdflatex to LaTeX, and changed the position of the anchor (see edit above) – cmhughes Aug 27 '11 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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