如何将包‘answers’与自己的解决方案环境一起使用?

如何将包‘answers’与自己的解决方案环境一起使用?

我想使用该answers包创建练习和答案(打印在文档的不同部分)。练习应该引用带有边注的解决方案(通过marginnote),反之亦然。下面的 MWE 已经运行得很好,但我不知道如何定义我自己的解决方案环境(参见“FAILS”)。它应该像练习环境一样,只是标签为“解决方案”而不是“练习”。

\documentclass{scrreprt}
\usepackage[american]{babel}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{etoolbox}
\usepackage{marginnote}
\usepackage{answers}
\usepackage{hyperref}

\declaretheoremstyle[%
  spaceabove=0.5em,
  spacebelow=0.5em,
  headfont=\sffamily\bfseries,
  notefont=\sffamily\bfseries,
  notebraces={(}{)},
  headpunct={},
  bodyfont=\normalfont%
]{mythmstyle}
\declaretheorem[style=mythmstyle, numberwithin=chapter]{exercise}
% \declaretheorem[style=mythmstyle, sibling=exercise]{solution}% => FAILS

\Newassociation{solution}{sol}{solutions}

\begin{document}
\Opensolutionfile{solutions}

\section{Section 1}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 1}
\end{Filesave}

\begin{exercise}[Header 1]\label{exlabel1}\marginnote{Sol.\ p.~\pageref{sollabel1}}\par\noindent
  First exercise
  \begin{solution}[Header 1]\label{sollabel1}\marginnote{Ex.\ p.~\pageref{exlabel1}}\par\noindent
    First solution.
  \end{solution}
\end{exercise}

\clearpage
\section{Section 2}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 2}
\end{Filesave}

\begin{exercise}[Header 2]\label{exlabel2}\marginnote{Sol.\ p.~\pageref{sollabel2}}\par\noindent
  \begin{enumerate}
  \item Part 1
  \item Part 2
  \end{enumerate}
  \begin{solution}[Header 2]\label{sollabel2}\marginnote{Ex.\ p.~\pageref{exlabel2}}
    \begin{enumerate}
    \item Solution Part 1
    \item Solution Part 2
    \end{enumerate}
  \end{solution}
\end{exercise}

\Closesolutionfile{solutions}

\clearpage

\input{solutions}

\end{document}

最终目标是不必手动设置标签和边注。一个想法是手动定义练习和解决方案环境,并将相同的“基本标签”作为参数传递给它们。然后,这两个环境为练习创建一个标签,为其解决方案创建一个标签,并相互引用。我在另一篇文章中尝试过(参见 页码超链接(练习到解决方案:失败;练习到解决方案:确定)),但还不算太成功。

更新

以下是我的想法添加另一个与问题本身超链接的答案

\documentclass{scrreprt}
\usepackage[american]{babel}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{etoolbox}
\usepackage{marginnote}
\usepackage{answers}
\usepackage{hyperref}

\newcounter{counter}
\numberwithin{counter}{section}
\makeatletter
\newenvironment{exercise}[2][]{\refstepcounter{counter}\par% #1 = header; #2 = label
  \normalfont\topsep6\p@\@plus6\p@\relax
  \trivlist
  \labelsep 0pt
  \def\mysollabel{#2}
  \preto\mysollabel{sol:}
  \def\myexlabel{#2}
  \preto\myexlabel{ex:}
\item[\hskip\labelsep\sffamily\bfseries Exercise~\thecounter\ #1]\label{\myexlabel}% this '\label' correctly refers to the exercise
  \marginnote{Solution p.~\pageref{\mysollabel}}%
  \ignorespaces%
}{%
  \endtrivlist\@endpefalse
}
\makeatother

\Opensolutionfile{solutions}
\Newassociation{solution}{Soln}{solutions}

\begin{document}
\section{Section 1}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 1}
\end{Filesave}

\begin{exercise}[Header 1]{ex:1:label}
  First exercise
  \begin{solution}[Header 1]{ex:1:label}
    First solution.
  \end{solution}
\end{exercise}

\clearpage
\section{Section 2}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 2}
\end{Filesave}

\begin{exercise}[Header 2]{ex:2:label}
  \begin{enumerate}
  \item Part 1
  \item Part 2
  \end{enumerate}
  \begin{solution}[Header 2]{ex:2:label}
    \begin{enumerate}
    \item Solution Part 1
    \item Solution Part 2
    \end{enumerate}
  \end{solution}
\end{exercise}

\Closesolutionfile{solutions}

% Renew the solution environment so that it hyperlinks back to the exercise
\makeatletter
\renewenvironment{Soln}[2][]{\par% #1 = header; #2 = label
  \normalfont\topsep6\p@\@plus6\p@\relax
  \trivlist
  \labelsep 0pt
  \def\myexlabel{#2}
  \preto\myexlabel{ex:}
  \def\mysollabel{#2}
  \preto\mysollabel{sol:}
\item[\hskip\labelsep\sffamily\bfseries Solution~\ref{\myexlabel}\ #1]\hypertarget{\mysollabel}{}%
  \marginnote{\hyperlink{\myexlabel}{Exercise p.~\pageref{\myexlabel}}}%
  \ignorespaces%
}%
{%
  \popQED\endtrivlist\@endpefalse
}%
\makeatother

\clearpage
\IfFileExists{solutions.tex}{\input{solutions.tex}}{}

\end{document}

答案1

从我的更新我尝试改进代码。我意识到Soln需要三个参数,我将它们全部设为正式/必需参数,以便answers可以使用它们(这部分最初失败了)。我不需要hyperlink特别使用(链接正确显示)。我将把这个 MWE 转换为原始文档,看看一切是否仍然有效(如果不行,我会报告)。欢迎对代码的可能改进发表评论。

\documentclass{scrreprt}
\usepackage[american]{babel}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{etoolbox}
\usepackage{marginnote}
\usepackage{answers}
\usepackage{hyperref}

\newcounter{counter}
\numberwithin{counter}{section}

\newenvironment{exercise}[2]{\refstepcounter{counter}\par% #1 = header; #2 = label
  \trivlist
  \labelsep 0pt
  \def\mysollabel{#2}
  \preto\mysollabel{sol:}
  \def\myexlabel{#2}
  \preto\myexlabel{ex:}
\item[\hskip\labelsep\sffamily\bfseries Exercise~\thecounter~(#1)]\label{\myexlabel}% this '\label' refers to the exercise
  \marginnote{Solution~p.~\pageref{\mysollabel}}%
  \ignorespaces%
}{%
  \endtrivlist
}

\Opensolutionfile{solutions}
\Newassociation{solution}{Soln}{solutions}

\begin{document}
\section{Exercises Section 1}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 1}
\end{Filesave}

\newcommand*{\headerOne}{Header 1}
\begin{exercise}{\headerOne}{ex:1:label}\\
  First exercise
  \begin{solution}{\headerOne}{ex:1:label}\\
    First solution.
  \end{solution}
\end{exercise}

\clearpage
\section{Exercises Section 2}
\begin{Filesave}{solutions}
  \clearpage
  \section{Solutions Section 2}
\end{Filesave}

\newcommand*{\headerTwo}{Header 2}
\begin{exercise}{\headerTwo}{ex:2:label}
  \begin{enumerate}
  \item Part 1
  \item Part 2
  \end{enumerate}
  \begin{solution}{\headerTwo}{ex:2:label}
    \begin{enumerate}
    \item Solution Part 1
    \item Solution Part 2
    \end{enumerate}
  \end{solution}
\end{exercise}

\Closesolutionfile{solutions}

\renewenvironment{Soln}[3]{% #1 = label (from 'answers'; #2 = header; #3 = label
  \pushQED{\qed}
  \trivlist
  \labelsep 0pt
  \def\myexlabel{#3}
  \preto\myexlabel{ex:}
  \def\mysollabel{#3}
  \preto\mysollabel{sol:}
\item[\hskip\labelsep\sffamily\bfseries Solution~#1~(#2)]\label{\mysollabel}%
    \marginnote{Exercise~p.~\pageref{\myexlabel}}%
    \ignorespaces%
}{%
  \popQED\endtrivlist
}

\clearpage
\IfFileExists{solutions.tex}{\input{solutions.tex}}{}

\end{document}

相关内容