下面的代码有助于获取可点击的问题数量,以便在问题和其答案之间进行最佳导航。
solution
其他环境中的环境problem
意味着问题有解决方案。单击链接后,它会转到已定义的\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}
但是如果问题没有解决方案怎么办?那么solution
里面就没有环境problem
。但\hyperlink{solution:##2}{##2.}}
仍然在这里并链接到缺失的目标。
如果目标缺失,如何防止超链接?
不幸的是,这解决方案对我来说不起作用,因为\csname link@#1 \endcsname
\link@1.
不允许TeX
。
我有解决方案。这意味着手动标记此类任务(\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)}
}
但这不是正确的方法。我想要一个自动检查。
答案1
不幸的是,这个解决方案对我来说不起作用,因为 \csname link@#1 \endcsname \link@1. 在 TeX 中不允许。
是的,确实如此,但是你可以解决这个问题,例如,\@roman{#1}
将数字改为罗马数字。
下面,我对您的代码做了一些修改,包括:
\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)}
以及对环境的相应修正Solution
:
\label{solution\@roman{#1}}
以下是完整的 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}