![使用 xparse 和任务生成多项选择的答案](https://linux22.com/image/432260/%E4%BD%BF%E7%94%A8%20xparse%20%E5%92%8C%E4%BB%BB%E5%8A%A1%E7%94%9F%E6%88%90%E5%A4%9A%E9%A1%B9%E9%80%89%E6%8B%A9%E7%9A%84%E7%AD%94%E6%A1%88.png)
我正在使用以下代码生成答案。当我在需要在\answerkey
每个部分添加的长文档中尝试它时,打印的答案会重复前面部分的对应内容。如何修改代码以便在添加后\answerkey
重置该过程?
\documentclass[12pt,a4paper]{article}
\usepackage{amsmath, mathptmx, amssymb, amsthm}
\usepackage[left=1.5cm,right=1.5cm,top=1.5cm,bottom=1.5cm]{geometry}
\usepackage{graphicx}
\usepackage{tikz}
%=========================================================
%Answer Keys
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \use:nn {nV}
\NewDocumentCommand \answerkey {}
{
\tl_clear:N \l_tmpa_tl
\seq_map_inline:Nn \section_I
{ \tl_put_right:Nn \l_tmpa_tl { \task \circled{##1} } }
\use:nV
{ \begin{tasks}[label = \arabic*](10) }
\l_tmpa_tl
\end {tasks}
}
\NewDocumentCommand \ans {}
{
\seq_gput_right:Nx \section_I
{ \use:c { @currentlabel } }
}
\seq_new:N \section_I
\ExplSyntaxOff
%=========================================================
%Enumitem & Tasks
\usepackage{enumitem}
\usepackage{tasks}[newest]
\settasks{
item-indent = 2em,
label-width = 1.5em,
label-offset = 0.25em,
label = \Alph*.,
}
%=========================================================
\begin{document}
\section{Section 1}
\begin{enumerate}
\item Question 1:
\begin{tasks}(4)
\task 1 \ans
\task 2
\task 3
\task 4
\end{tasks}
\item Question 2:
\begin{tasks}(4)
\task 1
\task 2 \ans
\task 3
\task 4
\end{tasks}
\end{enumerate}
\begin{center}
\textbf{Answer keys of section 1}
\end{center}
\answerkey
\section{Section 2}
\begin{enumerate}
\item Question 1:
\begin{tasks}(4)
\task 1
\task 2
\task 3 \ans
\task 4
\end{tasks}
\item Question 2:
\begin{tasks}(4)
\task 1
\task 2
\task 3
\task 4 \ans
\end{tasks}
\end{enumerate}
\begin{center}
\textbf{Answer keys of section 2}
\end{center}
\answerkey
\end{document}
答案1
您需要清除末尾的序列\answerkey
。此外,无关紧要的是,您应该正确命名序列。
\documentclass[12pt,a4paper]{article}
\usepackage{amsmath, mathptmx, amssymb, amsthm}
\usepackage[left=1.5cm,right=1.5cm,top=1.5cm,bottom=1.5cm]{geometry}
\usepackage{graphicx}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\usepackage{xparse}
\ExplSyntaxOn
\seq_new:N \g_xuan_answers_seq
\cs_generate_variant:Nn \use:nn {nV}
\NewDocumentCommand \answerkey {}
{
\tl_clear:N \l_tmpa_tl
\seq_map_inline:Nn \g_xuan_answers_seq
{ \tl_put_right:Nn \l_tmpa_tl { \task \circled {##1} } }
\use:nV
{ \begin{tasks}[label = \arabic*](10) }
\l_tmpa_tl
\end {tasks}
\seq_gclear:N \g_xuan_answers_seq
}
\NewDocumentCommand \ans {}
{
\seq_gput_right:Nx \g_xuan_answers_seq
{ \use:c {@currentlabel} }
}
\ExplSyntaxOff
\usepackage{enumitem}
\usepackage{tasks}[newest]
\settasks{
item-indent = 2em,
label-width = 1.5em,
label-offset = 0.25em,
label = \Alph*.
}
\begin{document}
\section{Section 1}
\begin{enumerate}
\item Question 1:
\begin{tasks}(4)
\task 1 \ans
\task 2
\task 3
\task 4
\end{tasks}
\item Question 2:
\begin{tasks}(4)
\task 1
\task 2 \ans
\task 3
\task 4
\end{tasks}
\end{enumerate}
\begin{center}
\textbf{Answer keys of section \thesection}
\end{center}
\answerkey
\section{Section 2}
\begin{enumerate}
\item Question 1:
\begin{tasks}(4)
\task 1
\task 2
\task 3 \ans
\task 4
\end{tasks}
\item Question 2:
\begin{tasks}(4)
\task 1
\task 2
\task 3
\task 4 \ans
\end{tasks}
\end{enumerate}
\begin{center}
\textbf{Answer keys of section \thesection}
\end{center}
\answerkey
\end{document}