
我想使用該answers
包來創建練習和答案(列印在文件的不同部分)。練習應該參考帶有旁注的解決方案(通過marginnote
),反之亦然。下面的 MWE 已經運作得很好,但我不知道如何定義我自己的解決方案環境(請參閱「失敗」)。它應該像鍛煉環境一樣,只是標籤為“解決方案”而不是“鍛煉”。
\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}