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 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}%
  {}
share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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)?

share|improve this answer
    
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
add comment

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.