在撰写包含练习和解决方案的讲义时,我想创建从练习到其解决方案的超链接,反之亦然。我使用包框架,超链接和答案(出于某些原因,我至少希望坚持前两个)。下面的例子几乎满足了我的要求。然而,从练习到其解决方案的超链接现在是姓名(“练习”)的练习,而我希望它是它的数字(“1”)。我该如何实现这一点?
\documentclass{book}
\usepackage{answers}
\usepackage{hyperref}
\usepackage[framemethod=tikz]{mdframed}
\Newassociation{sol}{answ}{ans}\Opensolutionfile{ans}
\mdtheorem{ex}{\hyperlink{ex:\theex}{Exercise}\hypertarget{sol:\theex}{}}
\renewenvironment{answ}[1]{{\hypertarget{ex:#1}{}\hyperlink{sol:#1}{#1}}\quad}{}
\begin{document}
\begin{ex}
This is an exercise.
\begin{sol}
This is the solution.
\end{sol}
\end{ex}
\Closesolutionfile{ans}
\input{ans}
\end{document}
答案1
另一个建议是使用xsim
而answers
不是使用您的要求mdframed
:
\documentclass{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{xsim}
\usepackage{hyperref}
\DeclareExerciseEnvironmentTemplate{myex}{%
\begin{mdframed}[%
frametitle={%
\hypertarget{ex:\ExerciseID}{\XSIMmixedcase{\GetExerciseName}}
\hyperlink{sol:\ExerciseID}{\GetExerciseProperty{counter}}%
}]%
}{\end{mdframed}}
\DeclareExerciseEnvironmentTemplate{mysol}{%
\par\noindent
\hyperlink
{ex:\ExerciseID}
{\hypertarget{sol:\ExerciseID}{\GetExerciseProperty{counter}}}%
\quad
}{\par}
\xsimsetup{
exercise/template = myex ,
solution/template = mysol ,
print-solutions/headings = false
}
\begin{document}
\begin{exercise}
This is an exercise.
\end{exercise}
\begin{solution}
This is the solution.
\end{solution}
\begin{exercise}
This is another exercise.
\end{exercise}
\begin{solution}
This is the other solution.
\end{solution}
\printsolutions
\end{document}
如果您稍后想要切换到tcolorbox
,则只需要更改模板定义myex
:
\DeclareExerciseEnvironmentTemplate{myex}{%
\begin{tcolorbox}[sharp corners, colback = white]
\textbf{%
\hypertarget{ex:\ExerciseID}{\XSIMmixedcase{\GetExerciseName}}
\hyperlink{sol:\ExerciseID}{\GetExerciseProperty{counter}}%
}\par\smallskip
}{\end{tcolorbox}}
也可以定义环境ex
,sol
如果您坚持使用这些名称:
使用选项加载包blank
,它现在没有定义exercise
和solution
\usepackage[blank]{xsim}
并定义ex
和sol
:
\DeclareExerciseType {exercise} {
exercise-env = ex ,
solution-env = sol ,
exercise-name = \XSIMtranslate {exercise} ,
exercises-name = \XSIMtranslate {exercises} ,
solution-name = \XSIMtranslate {solution} ,
solutions-name = \XSIMtranslate {solutions} ,
exercise-template = myex ,
solution-template = mysol
}
正如现在只需要在上面的例子中设置选项\xsimsetup{print-solutions/headings = false}
并且可以改变环境:
\begin{ex}
This is an exercise.
\end{ex}
\begin{sol}
This is the solution.
\end{sol}
\begin{ex}
This is another exercise.
\end{ex}
\begin{sol}
This is the other solution.
\end{sol}
答案2
尽管您想坚持使用,但mdframed
我建议使用tcolorbox
因为它可以提供mdframed+answer
行为。此解决方案也用于hyperref
链接。
例如answers
,tcolorbox
提供一些命令和环境来定义带有零件的盒子,这些零件将保存在外部文件中并在稍后打印。
下面的代码是示例的简化版tcolorbox
文档中的第 8.3 节(可以看到类似的东西这里)
每个ex
框都会将解决方案(之后的文本\tcblower
)保存到一个独立的文件中。
命令\tcbstartrecording[file1]
将开始保存file1
要纳入解决方案列表的所有练习的名称。
命令\tcbstoprecording
按照其所说的去做。
命令\tcbinputrecords[file1]
加载所有解决方案并打印它们。
关于参考,我假设每个练习都包含一个解决方案,并且它们各自的名称都是对其解决方案/练习的引用。
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{hyperref}
\NewTColorBox[auto counter]{ex}{+O{}}{%
enhanced, colframe=black, colback=white, sharp corners,
attach title to upper=\par, coltitle=black, fonttitle=\bfseries,
title={Exercise~\ref{solution@\thetcbcounter}},
label={exercise@\thetcbcounter},
lowerbox=ignored,
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solution{\thetcbcounter}%
{solutions/exercise-\thetcbcounter.tex}},
#1}
\NewTotalTColorBox[auto counter]{\solution}{mm}{%
enhanced, blanker,
coltitle=black, fonttitle=\bfseries,
title={\ref{exercise@\thetcbcounter}},
label={solution@\thetcbcounter},
attach title to upper={\ },
}{\input{#2}}
\begin{document}
% We start to record all `tcblower` parts in file `file1` for later processing
\tcbstartrecording[file1]
%Every exercise box contains an upper part which is processed and
%printed and a lower (solution) which printing is delayed.
%
\begin{ex}
This is an exercise.
\tcblower
This is the solution for first exercise
\end{ex}
\begin{ex}
This is an exercise.
\tcblower
This is the solution for second exercise
\end{ex}
\tcbstoprecording
%previous recorded files with solutions are processed and printed
\tcbinputrecords[file1]
\end{document}
答案3
您可以\theex
在 的开头mdtheorem
使用startcode
选项重新定义:
\documentclass{book}
\usepackage{answers}
\usepackage{hyperref}
\usepackage[framemethod=tikz]{mdframed}
\Newassociation{sol}{answ}{ans}\Opensolutionfile{ans}
\mdtheorem[startcode={\let\theexold\theex\renewcommand{\theex}{\hyperlink{ex:\theexold}{\theexold}\hypertarget{sol:\theexold}{}}}]{ex}{Exercise}
\renewenvironment{answ}[1]{{\hypertarget{ex:#1}{}\hyperlink{sol:#1}{#1}}\quad}{}
\begin{document}
\begin{ex}
This is an exercise.
\begin{sol}
This is the solution.
\end{sol}
\end{ex}
\begin{ex}
This is an exercise.
\begin{sol}
This is the solution.
\end{sol}
\end{ex}
\Closesolutionfile{ans}
\input{ans}
\end{document}