我正在使用该answers
软件包创建一组讲义中的练习列表,并将这些练习的解决方案作为与讲义分开的文件。我希望解决方案文件包含相应问题的文本。我尝试存储练习的文本,然后将其显示在解决方案中,但没有填写。例如,
\begin{exercise}
\def\problemtext{
Prove Proposition~\ref{prop:solnspan}.
}
\problemtext
\begin{solution}
\problemtext \\
\textbf{Solution}\\
(Solution goes here...)
\end{solution}
\end{exercise}
解决方案的相应 .tex 文件虽然字面上包含\problemtext
,但未填写相应文本。这造成了一些问题,因为我是单独构建此 TeX 文件而不是注释的。
答案1
\@namedef
一个带有和的解决方案\@nameuse
——由于计数器不一样,所以必须有两个宏来检索问题文本(或者\let...
根据当前处于活动状态的环境,或多或少复杂的语句!)
\documentclass{article}
\usepackage{answers}
\Newassociation{sol}{Solution}{ans}
\newtheorem{exercise}{Exercise}
\makeatletter
\newcommand{\storeproblemtext}[1]{%
\global\@namedef{problemtext\theexercise}{#1}%
}
\newcommand{\retrieveproblemtext}{%
\@nameuse{problemtext\theexercise}%
}
\newcommand{\solutionproblemtext}{%
\@nameuse{problemtext\theSolution}% Counter is named Solution
}
\makeatother
\begin{document}
\Opensolutionfile{ans}[ans1]
\begin{exercise}\label{prop:solnspan}
\storeproblemtext{%
Prove Proposition~\ref{prop:solnspan}.%
}
\retrieveproblemtext{1}%
\begin{sol}
Some text just for testing here:
\solutionproblemtext%
\textbf{Solution}\\
(Solution goes here...)
\end{sol}
\end{exercise}
\begin{exercise}\label{einstein}
\storeproblemtext{%
Prove $E=mc^2$.%
}
\retrieveproblemtext%
\begin{sol}
Some text just for testing here:
\solutionproblemtext%
\textbf{Solution}\\
(Solution goes here...)
\end{sol}
\end{exercise}
\Closesolutionfile{ans}
\input{ans1}
\end{document}
更新(感谢touhami的建议!)
\documentclass{article}
\usepackage{answers}
\Newassociation{sol}{Solution}{ans}
\newtheorem{exercise}{Exercise}
\makeatletter
\newcommand{\storeproblemtext}[1]{%
\global\@namedef{problemtext\@currentlabel}{#1}%
}
\newcommand{\retrieveproblemtext}{%
\@nameuse{problemtext\@currentlabel}%
}
\makeatother
\begin{document}
\Opensolutionfile{ans}[ans1]
\begin{exercise}\label{prop:solnspan}
\storeproblemtext{%
Prove Proposition~\ref{prop:solnspan}.%
}
\retrieveproblemtext{1}%
\begin{sol}
Some text just for testing here:
\retrieveproblemtext%
\textbf{Solution}\\
(Solution goes here...)
\end{sol}
\end{exercise}
\begin{exercise}\label{einstein}
\storeproblemtext{%
Prove $E=mc^2$.%
}
\retrieveproblemtext%
\begin{sol}
Some text just for testing here:
\retrieveproblemtext%
\textbf{Solution}\\
(Solution goes here...)
\end{sol}
\end{exercise}
\Closesolutionfile{ans}
\input{ans1}
\end{document}