我正在编写一本教科书,并尝试将其用于xsim
问题/练习及其答案。
我想要做的事情很简单:
- 在每一章中放置小问题/练习题,大约每个小节一个,意味着每章有 10 到 20 个。
- 打印问题/练习到位即,如果第 12.3.1 节末尾有两个问题,那么这些问题必须出现在第 12.3.1 节末尾。
- 在书末的专门章节中打印所有问题/练习的答案。
编辑:这是我想要的模型:
主要内容:
Chapter 14
14.1 Cats as pets
Text text text...
EXERCISES: 14.1
1. Draw a cat.
2. Bathe a cat.
3. Trace the history of cats through time.
Text text text...
14.2 Frogs as pets
Text text text...
EXERCISES: 14.2
1. Lick a frog.
2. Bathe a frog.
3. Feed a frog.
Text text text...
Chapter 21
21.1 The joys of fish
Text text text...
EXERCISES: 21.1
1. Catch a fish.
2. Draw a fish.
Text text text...
在本书最后的一章中:
SOLUTIONS TO EXERCISES
Chapter 14
14.1
1. Solution A
2. Solution B
3. Solution C
14.2
1. Solution X
2. Solution Y
3. Solution X
Chapter 15
15.1
1. Solution P
2. Solution Q
问题在于,为了将答案放在书末的章节中,问题必须放在xsim
“集合”中(\collectexercises{foo}
... \collectexercisesstop{foo}
),但将它们放在集合中防止它们被现场打印。如果您的目标是在单独的测试文档中打印问题,那么这很有意义,但这不是我需要做的。
对于我想做的事情,有什么建议吗?xsim 可以做到这一点吗,还是我选择了错误的工具?
答案1
您不需要集合来实现您想要的。您可以使用它\ForEachUsedExerciseByType
来构建自己的列表。此命令映射到文档中使用的所有练习。在命令的参数中,您可以引用练习类型与#1
和练习ID使用#2
。这允许您检查或调用练习的属性,例如使用\ExercisePropertyGet{<type>}{<ID>}{<property>}
。(可用的命令和属性均列在手册中。)使用\XSIMprint{exercise|solution}{<type>}{<ID>}
您可以打印相应的练习或解决方案。
对于下面的例子,我定义
\newcommand\printsectionexercises{%
\ForEachUsedExerciseByType{%
\ifnum\ExercisePropertyGet{##1}{##2}{chapter-value}=\value{chapter}
\ifnum\ExercisePropertyGet{##1}{##2}{section-value}=\value{section}
\XSIMprint{exercise}{##1}{##2}%
\fi
\fi
}%
}
检查当前部分的值,并打印相应的练习和
\renewcommand\printsolutions{%
\def\currentchapter{}%
\def\currentsection{}%
\def\lastchapter{}%
\def\lastsection{}%
\ForEachUsedExerciseByType{%
\let\lastchapter\currentchapter
\let\lastsection\currentsection
\edef\currentchapter{\ExercisePropertyGet{##1}{##2}{chapter-value}}%
\edef\currentsection{\ExercisePropertyGet{##1}{##2}{section-value}}%
\ifx\lastchapter\currentchapter\else
\section*{Chapter \ExercisePropertyGet{##1}{##2}{chapter}}
\fi
\ifx\lastsection\currentsection\else
\subsection*{Exercises \ExercisePropertyGet{##1}{##2}{section}}
\fi
\XSIMprint{solution}{##1}{##2}%
}%
}
检查章节和部分的值,如果它们已经改变,则插入合适的标题并打印解决方案。
与包设置一起
\xsimsetup{
exercise/template = number-only ,
solution/template = number-only ,
exercise/within = section ,
exercise/the-counter = \arabic{exercise}. ,
exercise/print = false
}
以及自定义练习模板
\DeclareExerciseEnvironmentTemplate{number-only}{%
\par\noindent
\textbf{\GetExerciseProperty{counter}}%
\GetExercisePropertyT{subtitle}{ \textit{#1}} %
}{\par}
然后把练习放在某一节的某个地方,在本节的末尾,我们使用
\subsection*{Exercises \thesection}
\printsectionexercises
后来在书中
\chapter*{Solutions to the exercises}
\printsolutions
下面的完整示例给出
完整代码:
\documentclass[twocolumn]{book}
\usepackage{xsim}
\DeclareExerciseEnvironmentTemplate{number-only}{%
\par\noindent
\textbf{\GetExerciseProperty{counter}}%
\GetExercisePropertyT{subtitle}{ \textit{#1}} %
}{\par}
\newcommand\printsectionexercises{%
\ForEachUsedExerciseByType{%
\ifnum\ExercisePropertyGet{##1}{##2}{chapter-value}=\value{chapter}
\ifnum\ExercisePropertyGet{##1}{##2}{section-value}=\value{section}
\XSIMprint{exercise}{##1}{##2}%
\fi
\fi
}%
}
\renewcommand\printsolutions{%
\def\currentchapter{}%
\def\currentsection{}%
\def\lastchapter{}%
\def\lastsection{}%
\ForEachUsedExerciseByType{%
\let\lastchapter\currentchapter
\let\lastsection\currentsection
\edef\currentchapter{\ExercisePropertyGet{##1}{##2}{chapter-value}}%
\edef\currentsection{\ExercisePropertyGet{##1}{##2}{section-value}}%
\ifx\lastchapter\currentchapter\else
\section*{Chapter \ExercisePropertyGet{##1}{##2}{chapter}}
\fi
\ifx\lastsection\currentsection\else
\subsection*{Exercises \ExercisePropertyGet{##1}{##2}{section}}
\fi
\XSIMprint{solution}{##1}{##2}%
}%
}
\xsimsetup{
exercise/template = number-only ,
solution/template = number-only ,
exercise/within = section ,
exercise/the-counter = \arabic{exercise}. ,
exercise/print = false
}
\begin{document}
\setcounter{chapter}{13}
\chapter{}
\section{Cats as pets}
Text text text\dots
\begin{exercise}
Draw a cat.
\end{exercise}
\begin{solution}
Drawing a cat.
\end{solution}
Text text text\dots
\begin{exercise}
Bathe a cat.
\end{exercise}
\begin{solution}
Bathing a cat.
\end{solution}
Text text text\dots
\begin{exercise}
Trace the history of cats through time.
\end{exercise}
\begin{solution}
Tracing the history of cats through time.
\end{solution}
\subsection*{Exercises \thesection}
\printsectionexercises
\section{Frogs as pets}
Text text text\dots
\begin{exercise}
Lick a frog.
\end{exercise}
\begin{solution}
Licking a frog.
\end{solution}
Text text text\dots
\begin{exercise}
Bathe a frog.
\end{exercise}
\begin{solution}
Bathing a frog.
\end{solution}
Text text text\dots
\begin{exercise}
Feed a frog.
\end{exercise}
\begin{solution}
Feeding a frog.
\end{solution}
\subsection*{Exercises \thesection}
\printsectionexercises
\setcounter{chapter}{20}
\chapter{}
\section{The joys of fish}
Text text text\dots
\begin{exercise}
Catch a fish.
\end{exercise}
\begin{solution}
Catching a fish.
\end{solution}
Text text text\dots
\begin{exercise}
Draw a fish.
\end{exercise}
\begin{solution}
Drawing a fish.
\end{solution}
\subsection*{Exercises \thesection}
\printsectionexercises
\chapter*{Solutions to the exercises}
\printsolutions
\end{document}