独自のソリューション環境でパッケージ「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}

最終的な目標は、ラベルと余白のメモを手動で設定しなくても済むようにすることです。1つのアイデアは、演習とソリューションの環境を手動で定義し、同じ「ベースラベル」を引数としてそれぞれに渡すことです。これら2つの環境は、演習のラベルとそのソリューションのラベルを作成し、お互いを参照します。私は別の投稿でこれを試しました( ページ番号のハイパーリンク (演習から解答: 不合格、演習の解答: 合格)) ですが、まだあまり成功していませんでした。

アップデート

私がこのアイデアをどこまで実現できたかをご紹介します質問自体にハイパーリンクされた別の回答を追加する

\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コードを改善しようとしました。には 3 つの引数が必要であることに気付き、それらをすべて正式な/必須の引数にして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}

関連情報