0

After Ulrike Fischer helped me out at this thread, I just had one troubling aspect remaining while getting the formatting together for some textbooks that I have written. I was thinking of ignoring it, but it's the only issue left and I am hoping that someone can help me to figure this out.

The overview is that I am using the answers package and I would like a problem to hyperlink to its solution and vice versa by clicking on each environment's displayed name and counter. The issue that remains is that the current code does excludes the theorem/problem's counter in the hyperlink to the solution. How can this be done (while not breaking the usage of cleveref)?

Here are two screenshots of the two separate pages (one of the chapter and one of the solutions):

enter image description here enter image description here

In the other thread, Willie Wong said

I don't think that's easily doable using amsthm. You could just define a separate problem environment, not using amsthm

But I have no idea how to do that. Is not using amsthm the only way? Either way, I would be interested in seeing a solution. Here is a minimum working example of what I currently have:

\documentclass[openany, 12pt]{book}
\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsbsy}

\usepackage{hyperref}
\usepackage[capitalize,nameinlink,noabbrev]{cleveref}

\theoremstyle{definition}
\newtheorem{theorem}{Theorem}[chapter]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{conjecture}[theorem]{Conjecture}
\newtheorem{example}[theorem]{Example}
\newtheorem{definition}[theorem]{Definition}

\makeatletter

\newcommand{\linkdest}[1]{\Hy@raisedlink{\hypertarget{#1}{}}}

\usepackage{answers, xpatch}

\newtheorem{problem}[theorem]{%
\hyperlink{problem:\theproblem}{Problem}\Hy@raisedlink{\hypertarget{probsol:\theproblem}{}}}
\Newassociation{probsol}{Solution}{ans}
\renewenvironment{Solution}[1]
{\par\bigskip\noindent{\bfseries \hyperlink{probsol:#1}{Solution #1.}\Hy@raisedlink{\hypertarget{problem:#1}{}}}\quad}
{\par\bigskip}

\makeatother

\newcommand{\lemmaautorefname}{Lemma}
\newcommand{\corollaryautorefname}{Corollary}
\newcommand{\conjectureautorefname}{Conjecture}
\crefname{problem}{Problem}{Problems} \Crefname{problem}{Problem}{Problems}
\newcommand{\exampleautorefname}{Example}
\newcommand{\definitionautorefname}{Definition}

\begin{document}

\Opensolutionfile{ans}[ans1]

\chapter{First}

\begin{problem}\label{prb:example-prob}
This is a problem.
    \begin{probsol}
    This is a solution.
    \end{probsol}
\end{problem}

I am referring to \cref{prb:example-prob}.

\Closesolutionfile{ans}
\chapter*{Solutions}
\addcontentsline{toc}{chapter}{Solutions}
\input{ans1}

\end{document}

1 Answer 1

0

I managed to hack together a solution where problems and solutions hyperlink to each other in the answers package, without breaking anything else like cleveref. based off this thread and this thread. Any feedback would be appreciated. If I don't see a better solution, I will accept mine.

enter image description here enter image description here

\documentclass[openany, 12pt]{book}
\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsbsy}

\usepackage{hyperref}
\usepackage[capitalize,nameinlink,noabbrev]{cleveref}

\theoremstyle{definition}
\newtheorem{theorem}{Theorem}[chapter]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{conjecture}[theorem]{Conjecture}
\newtheorem{example}[theorem]{Example}
\newtheorem{definition}[theorem]{Definition}

\makeatletter

%\newcommand{\linkdest}[1]{\Hy@raisedlink{\hypertarget{#1}{}}}

\usepackage{answers, xpatch}

\newtheoremstyle{hypered}%
{}{}%
{}{}%
{\bfseries}{}%
{ }%
{\Hy@raisedlink{\hypertarget{probsol:\theproblem}{}}\hyperlink{\theproblem}{\thmname{#1}\thmnumber{ #2}.}\thmnote{\normalfont (#3).}}

\theoremstyle{hypered}
\newtheorem{problem}[theorem]{Problem}
\Newassociation{probsol}{Solution}{ans}
\newcommand{\adj}[1]{\Hy@raisedlink{\hypertarget{#1}{}}}
\renewenvironment{Solution}[1]
{\par\bigskip\parindent0pt{\phantomsection{\bfseries\Hy@raisedlink{\hypertarget{problem:#1}{}} \hyperlink{probsol:#1}{\adj{#1}Solution #1.}}}}
{\par\bigskip}

\makeatother

\newcommand{\lemmaautorefname}{Lemma}
\newcommand{\corollaryautorefname}{Corollary}
\newcommand{\conjectureautorefname}{Conjecture}
\crefname{problem}{Problem}{Problems} \Crefname{problem}{Problem}{Problems}
\newcommand{\exampleautorefname}{Example}
\newcommand{\definitionautorefname}{Definition}

\begin{document}

\Opensolutionfile{ans}[ans1]

\chapter{First}

\begin{problem}[Gauss's theorem]\label{prb:example-prob}
This is a problem.
    \begin{probsol}
    I am referring to \cref{prb:example-prob-2}.
    \end{probsol}
\end{problem}

\begin{problem}\label{prb:example-prob-2}
This is a problem.
    \begin{probsol}
    I am referring to \cref{prb:example-prob}. This is a solution. This is a solution. This is a solution. This is a solution. This is a solution. This is a solution. This is a solution. This is a solution. This is a solution.
    \end{probsol}
\end{problem}

\Closesolutionfile{ans}
\chapter*{Solutions}
\addcontentsline{toc}{chapter}{Solutions}
\input{ans1}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .