2

Code below is helped to get clickable numbers of problems for best navigation between problem and its answer.

An environment solution inside other environment problem imply the problem has a solution. After clicking link it goes to defined \hypertarget{solution:#1}{...}

\documentclass[]{article}

\usepackage{ntheorem}
\usepackage{answers}
\usepackage{hyperref}
\makeatletter
\newtheoremstyle{problemstyle}%
{\hypertarget{problem:##2}{}\item[\theorem@headerfont{\hyperlink{solution:##2}{##2.}}] }
{\hypertarget{problem:##2}{}\item[\theorem@headerfont{\hyperlink{solution:##2}{##2.}}] (##3)}

\makeatother

\theoremstyle{problemstyle}
\newtheorem{problem}{}
%\renewcommand{\theproblem}{\thechapter.\arabic{problem}}
\Newassociation{solution}{Solution}{answer}
\renewenvironment{Solution}[1]{%
    \par%
    \hypertarget{solution:#1}{\noindent\hyperlink{problem:#1}{\bfseries #1.}}
}{%
    \par%
}

\begin{document}
\Opensolutionfile{answer}[Answers]

\begin{problem}
Problem with solution
\begin{solution}
Solution
\end{solution}
\end{problem}

\begin{problem}
Problem with NO solution
\end{problem}

\Closesolutionfile{answer}
\newpage
\input{Answers}
\end{document}

But what if problem has no solution? Then no environment solution inside problem. But \hyperlink{solution:##2}{##2.}} is still here and link to missing target.

How to prevent hyperlinking, if target is missing?

Unfortunately, this solution does not work for me, because of \csname link@#1 \endcsname \link@1. does not allowed in TeX.

I have solution. It imply manual marking such tasks (\begin{problem}[noanswer] ... \end{problem} ):

\newtheoremstyle{problemstyle}%
{\hypertarget{problem:##2}{}\item[\theorem@headerfont{\hyperlink{solution:##2}{##2.}}] }
{
\ifthenelse{\equal{##3}{noanswer}}
{\item[\theorem@headerfont{##2.}]}
{\hypertarget{problem:##2}{}\item[\theorem@headerfont{\hyperlink{solution:##2}{##2.}}] (##3)}
}

But this is not true way. I want an automatic check.

1

Unfortunately, this solution does not work for me, because of \csname link@#1 \endcsname \link@1. does not allowed in TeX.

Yes, indeed, but you can get round this using, for example, \@roman{#1} to change numbers into roman numerals.

In the following, I've made a few amendments to your code, including:

\newtheoremstyle{problemstyle}%
{%
    \hypertarget{problem:\@roman{##2}}{}%
    \ifcsname r@solution\@roman{##2}\endcsname
        \item[\theorem@headerfont{\hyperlink{solution:\@roman{##2}}{##2.}}]
    \else
        \item[\theorem@headerfont{##2}]
    \fi
}
{\hypertarget{problem:\@roman{##2}}{}\item[\theorem@headerfont{\hyperlink{solution:\@roman{##2}}{##2.}}] (##3)}

and the corresponding amendment to the Solution environment:

\label{solution\@roman{#1}}

Here's a complete MWE

\documentclass[]{article}

\usepackage{ntheorem}
\usepackage{answers}
\usepackage{hyperref}
\usepackage[left]{showlabels}   % <------- remove these lines once you're happy!
\showlabels{hypertarget}        % <------- remove these lines once you're happy!
\showlabels{hyperlink}          % <------- remove these lines once you're happy!
\makeatletter
\newtheoremstyle{problemstyle}%
{%
    \hypertarget{problem:\@roman{##2}}{}%
    \ifcsname r@solution\@roman{##2}\endcsname
        \item[\theorem@headerfont{\hyperlink{solution:\@roman{##2}}{##2.}}]
    \else
        \item[\theorem@headerfont{##2}]
    \fi
}
{\hypertarget{problem:\@roman{##2}}{}\item[\theorem@headerfont{\hyperlink{solution:\@roman{##2}}{##2.}}] (##3)}

\theoremstyle{problemstyle}
\newtheorem{problem}{}
%\renewcommand{\theproblem}{\thechapter.\arabic{problem}}
\Newassociation{solution}{Solution}{answer}
\renewenvironment{Solution}[1]{%
    \par%
    \label{solution\@roman{#1}}%
    \hypertarget{solution:\@roman{#1}}{\noindent\hyperlink{problem:\@roman{#1}}{\bfseries #1.}}
}{%
    \par%
}

\begin{document}
\Opensolutionfile{answer}[Answers]

\begin{problem}
Problem with solution
\begin{solution}
    Solution
\end{solution}
\end{problem}

\begin{problem}
Problem with NO solution
\end{problem}

\Closesolutionfile{answer}
\newpage
\input{Answers}
\end{document}
  • Thank you! It seems, the \@roman{...} does not necessary in you realization, all work fine without it. Also, a r@<ref>became a new knowledge for me. – sergiokapone Jul 17 '17 at 22:32

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.