打包答案:是否可以将所有解决方案附加到一个输出文件?

打包答案:是否可以将所有解决方案附加到一个输出文件?

我正在尝试使用包答案并找到“请解释 \Newassociation 并概述该软件包的工作原理“非常有用。但是,在处理答案时我遇到了另一个问题。我正在写一本有章节的书,在每一章的末尾我都使用包答案来创建问题并将解决方案写入文件。但是,每一章都有自己的包含解决方案的文件,在书的最后我必须按照正确的顺序输入每个解决方案。所以如果我改变章节的顺序,我必须改变输入命令的顺序。我希望将所有解决方案放在同一个文件中,即不使用\Opensolutionfile,而是使用\Appendsolutionfile-command。然后我可以在解决方案章节中只有一个输入命令。这可能吗?

答案1

这可以通过使用特定于章节的解决方案文件来实现,这里我使用了文件的名称,附加了章节编号,因此每个章节写入等等foo1.texfoo2.tex之后,命令\CollectAllSolutions按照写入的顺序输入所有解决方案文件。

每个章节都会自动完成写作,以便于用户轻松使用该功能(即,他/她不必在\Opensolutionfile/\Closesolutionfile每次开始章节时都进行指定)。我chapter对开始进行了一些重新定义。

当然,还有一些事情可以使其变得更加复杂,例如查询等等。

“示例”练习取自answersJ. Wright 的文档并改编而来。

\documentclass[12pt,a4paper]{book}
\usepackage{answers}
\usepackage{forloop}
\usepackage{totcount}%
\Newassociation{sol}{Solution}{ans}
\newtheorem{ex}{Exercise}


\let\LaTeXStandardChapter\chapter
\makeatletter
\newcommand{\unstarredchapter@noopt}[1]{%
\unstarredchapter@opt{#1}%
}

\newcommand{\unstarredchapter@opt}[2][]{%
\LaTeXStandardChapter[#1]{#2}%
\Closesolutionfile{ans}
\Opensolutionfile{ans}[\jobname\number\value{chapter}]
}

\newcommand{\starredchapter}[1]{%
\LaTeXStandardChapter*[#1]%
}

\newcommand{\unstarredchapter}{%
\@ifnextchar[{\unstarredchapter@opt}{\unstarredchapter@noopt}
}%

\renewcommand{\chapter}{%
\@ifstar{\starredchapter}{\unstarredchapter}
}%
\makeatother

\newcounter{loopcounter}

\newcommand{\CollectAllSolutions}[1]{%
  \setcounter{loopcounter}{1}%
  \forloop{loopcounter}{1}{\number\value{loopcounter} < #1}{%
    \IfFileExists{\jobname\number\value{loopcounter}}{\input{\jobname\number\value{loopcounter}}}{}%
  }%

}%



\begin{document}

\newcounter{chaptercounter}%
\regtotcounter{chapter}%
\chapter{Problems}
\begin{ex}
First exercise
\begin{sol}
First solution.
\end{sol}
\end{ex}
\begin{ex}
Second exercise
\begin{sol}
Second solution.
\end{sol}
\end{ex}


\chapter{Problems 2}
\begin{ex}
Third exercise
\begin{sol}
Third solution.
\end{sol}
\end{ex}
\begin{ex}
Fourth exercise
\begin{sol}
Fourth solution.
\end{sol}
\end{ex}

\chapter{Solutions}

\CollectAllSolutions{\totvalue{chapter}}

\end{document}

在此处输入图片描述

答案2

为什么每章都使用单独的答案文件?下面的大纲不是完整的可编译 MWE,但在我的书籍项目中确实有效。

在序言中:

\usepackage{answers}
\newcommand{\answerFileName}{allanswers}

\Newassociation{sol}{Solution}{\answerFileName}

\renewenvironment{Solution}[1]
{
\vspace{0.15in}\hrule\vspace{0.15in}
{\textbf{Exercise #1}}
\par 
}
{}


% in a macro called at the start of each chapter:
\Writetofile{\answerFileName}{%
\noexpand\chapter{\mychaptername}%
}
}

main.tex

\input{preamble}
\Opensolutionfile{\answerFileName}

%% \include various chapters

\Closesolutionfile{\answerFileName}

然后,您可以\include{\answerfilename}在主文档中或(像我一样)将其包含在单独的解决方案文档中。 我的解决方案文档如下所示:

在此处输入图片描述

...

在此处输入图片描述

chapter2.tex从代码生成

\begin{exx} Drive carefully!

Suppose you drive from Here to There at a speed of 30 miles/hour
and then drive back from There to Here at 60 miles/hour. 

What is your average speed?

\begin{hint}
\emph{Warning}: it's not 45 miles/hour.

The problem does not say how far it is from Here to There. You're free
to pick some convenient distance to work with if you like.
\end{hint}

\begin{sol}
Imagine that the trip from Here to There is 60 miles. Then it takes two
hours to get there and one hour to come back. That's three hours for
the 120 mile round trip, so the average speed is (120~miles)/(3~hours
40~miles/hour.

This conclusion surprises many people. You can see the idea even more
clearly if you imagine that it's possible to make the return trip at
infinite speed -- in no time at all. Then the average speed is
(120~miles)/(2~hours) = 60~miles/hour, which is \emph{not} the average
of 30 and infinity. 

\end{sol}

\end{exx}

环境exx管理练习编号。shint出现在书的末尾,也是通过answers包完成的。

相关内容