我正在写一本问答题书。
我希望问题和它们的答案在源中是接近的,但是在编译时,所有问题都聚集在输出的一个部分中,而所有答案都聚集在文档的不同部分中。
例如,输入可能是:
Question 1
Answer 1
Question 2
Answer 2
Question 3
Answer 3
而输出可能是:
Questions About The Calculus of Puppies
Answers About The Calculus of Puppies
Questions About The Algebra of Cats
Answers About The Algebra of Cats
顺便说一句,我认为这可以通过使用浮动环境和刷新来实现,可能类似于包endfloat
所做的。但也许有更好的方法?
答案1
这是一种非常小的快速而粗略的question/solution
方法。这些tcolorbox
东西太花哨了,不是真正需要的。(除了推进计数器)
每个\Question
都有一个强制参数(针对问题)和第三个可选参数(针对解决方案内容)。第一个可选参数用于选项(由于时间不足,这里未使用)。
解决方案被写入名为的外部文件\jobname.sol
,问题文件(再次)未在这里使用。
(诚然,我从一个无法正常工作的软件包中抓取了它,它还没有准备好发布:-()
\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{morewrites}
\usepackage{xparse}
\newcounter{QuestionBox}[section]
\newcounter{AnswerBox}[section]
\newtcolorbox[use counter=QuestionBox,number within=section]{QuestionBox}{title={Question~\theQuestionBox},colbacktitle={green},coltitle=black}
\newtcolorbox[use counter=AnswerBox,number within=section]{AnswerBox}{title={Answer~\theAnswerBox},colbacktitle={yellow},coltitle=black}
\newwrite\questionfile
\newwrite\solutionfile
\NewDocumentCommand{\Answer}{+O{}+m}{%
\begin{AnswerBox}
#2
\end{AnswerBox}
}
\NewDocumentCommand{\Question}{+O{}+m+O{}}{%
\immediate\write\solutionfile{%
\string\setcounter{section}{\number\value{section}}
\string\setcounter{AnswerBox}{\number\value{QuestionBox}}
}
\begin{QuestionBox}
#2
\end{QuestionBox}
\IfValueTF{#3}{%
\immediate\write\solutionfile{%
\string\Answer{\unexpanded{#3}}
}
}{}
}
\AtBeginDocument{%
\immediate\openout\questionfile=\jobname.quest
\immediate\openout\solutionfile=\jobname.sol
}
\NewDocumentCommand{\CollectSolutions}{+O{}}{%
\clearpage
\immediate\closeout\questionfile%
\immediate\closeout\solutionfile%
\setcounter{QuestionBox}{0}
\setcounter{AnswerBox}{0}
\section*{Solutions}
\InputIfFileExists{\jobname.sol}{}{}
}
\AtEndDocument{%
\immediate\closeout\questionfile%
}%
\begin{document}
\section{Relativity}
\Question{What's the meaning of \(E=mc^2\)}[It's the total relativistic energy (of a particle)]
\Question{Proof that \textcolor{red}{\[\gamma = \dfrac{1}{\sqrt{1-\dfrac{v^2}{c^2}}}\]}}[\textbf{It's completely straightforward}]
\section{Other stuff}
\Question{Find the anti-derivative of \[e^{-x^2}\]}[Ooops...]
\CollectSolutions
\end{document}