![如何以「類似部分」的環境劃分 Xparse 產生的清單?](https://rvso.com/image/335479/%E5%A6%82%E4%BD%95%E4%BB%A5%E3%80%8C%E9%A1%9E%E4%BC%BC%E9%83%A8%E5%88%86%E3%80%8D%E7%9A%84%E7%92%B0%E5%A2%83%E5%8A%83%E5%88%86%20Xparse%20%E7%94%A2%E7%94%9F%E7%9A%84%E6%B8%85%E5%96%AE%EF%BC%9F.png)
我主要使用該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 套件中的解決方案。已經謝謝了!