我主要使用该xparse
库来创建有组织的列表。以下是 MWE:
\documentclass{article}
\usepackage{xparse}
\newcounter{quest}
\newcommand{\quest}{\noindent\stepcounter{quest}{\bf Q}{\bf \arabic{quest}.\ }}
\ExplSyntaxOn
\NewDocumentCommand{\answer}{ m }{ \malmedal_input_add:n { #1 } }
\NewDocumentCommand{\listanswers}{ }{ \malmedal_output_direct: }
\seq_new:N \g_malmedal_input_seq
\seq_new:N \l_malmedal_temp_seq
\int_new:N \l_malmedal_count_int
\cs_new:Npn \malmedal_input_add:n #1
{
\seq_gput_right:Nn \g_malmedal_input_seq { #1 }
}
\cs_new_protected:Npn \malmedal_output_direct:
{
\int_zero:N \l_malmedal_count_int
\seq_map_inline:Nn \g_malmedal_input_seq
{
\int_incr:N \l_malmedal_count_int
\malmedal_print:n { ##1 }
}
}
\cs_new:Npn \malmedal_print:n #1
{
\noindent{\textbf{Question } ~ \bfseries\int_to_arabic:n { \l_malmedal_count_int }} ~ : ~ #1 \par
}
\ExplSyntaxOff
\begin{document}
\section{Math}
\quest $1 + 1 = ?$
\answer{$2$.}
\quest If $a = 2$ and $b = 5$, then $a - b = ?$
\answer{$-3$.}
\quest Are there any natural numbers between $8$ and $9$?
\answer{No.}
\section{Physics}
\quest What's the speed of light in vacuum?
\answer{$\approx 3\cdot 10^8$ m/s}
\quest Name one thing one can't do without atmospheric pressure.
\answer{To drink water with a straw.}
\section{Answer Key}
\listanswers
\end{document}
这将生成:
我想知道是否有办法将生成的列表按每个部分划分,如下所示:
如果你确实想帮忙,请不要只说有更简单的方法来生成答案。我需要 xparse 包中的解决方案。谢谢!