使用 xparse 和任务生成多项选择的答案

使用 xparse 和任务生成多项选择的答案

我正在使用以下代码生成答案。当我在需要在\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}

在此处输入图片描述

相关内容