无法使用 xparse 使用任务来生成答案

无法使用 xparse 使用任务来生成答案

我尝试编译以下代码,这些代码是我从中收集并修改的:将某项标记为“随机枚举”由@egreg 撰写。

\documentclass[12pt,a4paper]{article}
\usepackage{amsmath, mathptmx, amssymb}
\usepackage[left=1.5cm,right=1.5cm,top=1.5cm,bottom=1.5cm]{geometry}

%Shuffle Questions
\usepackage{xparse,environ}

\ExplSyntaxOn
\NewEnviron{randomlist}[1][]
 {
  \keys_set:nn { lucas/randomlist } { #1 }
  \lucas_randomlist:V \BODY
 }
\NewDocumentCommand{\answerkey}{}
 {
  \subsubsection*{Answer ~ key}
  \begin{tasks}[label = \arabic*.](10)
  \seq_map_inline:Nn \g_lucas_randomlist_answers_seq { \task ##1 }
  \end{tasks}
 }
\NewDocumentCommand{\ans}{}
 {
  \seq_gput_right:Nx \g_lucas_randomlist_answers_seq { \use:c { @currentlabel } }
 }

\keys_define:nn { lucas/randomlist }
 {
  environment .tl_set:N = \l__lucas_randomlist_env_tl,
  seed .code:n = \sys_gset_rand_seed:n { #1 },
 }

\seq_new:N \g_lucas_randomlist_answers_seq
\cs_new_protected:Nn \lucas_randomlist:n
 {
  \seq_set_split:Nnn \l__lucas_randomlist_items_seq { \item } { #1 }
  % discard the empty first item
  \seq_pop_left:NN \l__lucas_randomlist_items_seq \l_tmpa_tl
  \seq_shuffle:N \l__lucas_randomlist_items_seq
  \begin{\l__lucas_randomlist_env_tl}
  \item \seq_use:Nn \l__lucas_randomlist_items_seq { \item }
  \end{\l__lucas_randomlist_env_tl}
 }
\cs_generate_variant:Nn \lucas_randomlist:n { V }

\ExplSyntaxOff

\usepackage[inline]{enumitem}
\setlist[enumerate]{
    label = \textbf{Q \arabic*.},
    align = left,
    labelwidth = \parindent, 
    leftmargin = \parindent,
    style = standard
}

\usepackage{tasks}[newest]
\newcommand*\tasklabelformat[1]{\textbf{#1.}}
\settasks{
    item-indent = 2em,
    label-width = 1.5em,
    label-offset = 0.25em,
    label = \Alph*,
    label-format = \tasklabelformat
}

%=========================================================
\begin{document}
\begin{randomlist}[environment=enumerate, seed=1]
\item How many letters in the word ``Tea'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ \ans
        \task $2$
        \task $4$.
   \end{tasks}

\item How many letters in the word ``Hi'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$ \ans
        \task $4$.
   \end{tasks}

\item How many letters in the word ``Cake'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$
        \task $4$. \ans
   \end{tasks}

\item How many letters in the word ``Tree'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$
        \task $4$. \ans
   \end{tasks}

\item How many letters in the word ``Dog'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ \ans
        \task $2$ 
        \task $4$.
   \end{tasks}

\item How many letters in the word ``House'' ?
    \begin{tasks}(4)
        \task $2$
        \task $3$ 
        \task $5$ \ans
        \task $4$.
   \end{tasks}

\item How many letters in the word ``Tea'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ \ans
        \task $2$
        \task $4$.
   \end{tasks}

\item How many letters in the word ``Hi'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$ \ans
        \task $4$.
   \end{tasks}

\item How many letters in the word ``Cake'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$
        \task $4$. \ans
   \end{tasks}

\item How many letters in the word ``Tree'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$
        \task $4$. \ans
   \end{tasks}

\item How many letters in the word ``Dog'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ \ans
        \task $2$ 
        \task $4$.
   \end{tasks}

\item How many letters in the word ``House'' ?
    \begin{tasks}(4)
        \task $2$
        \task $3$ 
        \task $5$ \ans
        \task $4$.
   \end{tasks}
\end{randomlist}
\answerkey
\end{document}

它弹出下面的结构并且不生成答案键。

You've placed (tasks)(tasks) \seq_map_inline:Nn \g_lucas_randomlist_answers_seq
(tasks) {\task ##1} (tasks)(tasks) before the first \task

当我用以下方式替换代码的相应部分时:

\NewDocumentCommand{\answerkey}{}
 {
  \subsubsection*{Answer ~ key}
  \begin{enumerate*}[nosep, label = \arabic*., mode = unboxed]
  \seq_map_inline:Nn \g_lucas_randomlist_answers_seq {\item \makebox[2cm][l]{##1}}
  \end{enumerate*}
 }

可以正常使用,但是两行答案键没有对齐,如下所示: 在此处输入图片描述

如何解决?欢迎提出任何解决方案,我将不胜感激。

答案1

包中的环境tasks需要查看其主体,以便能够在每次出现时拆分内容\task并根据所选设置构建列表。这意味着您无法在 中即时构建它\begin{tasks} ... \end{tasks}。您需要事先构建环境主体,然后使用它。

\answerkey我建议修改如下定义:

\cs_generate_variant:Nn \use:nn {nV}

\NewDocumentCommand \answerkey {}
  {
    \subsubsection* {Answer~ key}
    \tl_clear:N \l_tmpa_tl
    \seq_map_inline:Nn \g_lucas_randomlist_answers_seq
      { \tl_put_right:Nn \l_tmpa_tl { \task ##1 } }
    \use:nV
      { \begin{tasks}[label = \arabic*](10)}
      \l_tmpa_tl
    \end {tasks}
 }

\use:nn读取两个参数并将它们放入输入流中。变体\use:nV读取变量的内容作为第二个参数,并将其内容用作参数。之前我们只需要将环境主体构建tasks为变量的内容。

顺便说一句:使用足够最新的,xparse您可以使用参数类型读取环境的主体,b这意味着您不需要environ。我相应地修改了代码:

\documentclass[12pt,a4paper]{article}
\usepackage{amsmath, mathptmx, amssymb}
\usepackage[left=1.5cm,right=1.5cm,top=1.5cm,bottom=1.5cm]{geometry}

%Shuffle Questions
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentEnvironment {randomlist} {o+b}
  {
    \IfNoValueF {#1} { \keys_set:nn { lucas/randomlist } {#1} }
    \lucas_randomlist:n {#2}
  }
  {}

\cs_generate_variant:Nn \use:nn {nV}

\NewDocumentCommand \answerkey {}
  {
    \subsubsection* {Answer~ key}
    \tl_clear:N \l_tmpa_tl
    \seq_map_inline:Nn \g_lucas_randomlist_answers_seq
      { \tl_put_right:Nn \l_tmpa_tl { \task ##1 } }
    \use:nV
      { \begin{tasks}[label = \arabic*](10)}
      \l_tmpa_tl
    \end {tasks}
 }

\NewDocumentCommand \ans {}
  {
    \seq_gput_right:Nx \g_lucas_randomlist_answers_seq
      { \use:c { @currentlabel } }
  }

\keys_define:nn { lucas/randomlist }
  {
    environment .tl_set:N = \l__lucas_randomlist_env_tl,
    seed .code:n = \sys_gset_rand_seed:n { #1 },
  }

\seq_new:N \g_lucas_randomlist_answers_seq
\cs_new_protected:Nn \lucas_randomlist:n
  {
    \seq_set_split:Nnn \l__lucas_randomlist_items_seq { \item } {#1}
    % discard the empty first item
    \seq_pop_left:NN \l__lucas_randomlist_items_seq \l_tmpa_tl
    \seq_shuffle:N \l__lucas_randomlist_items_seq
    \begin {\l__lucas_randomlist_env_tl}
      \item \seq_use:Nn \l__lucas_randomlist_items_seq { \item }
    \end {\l__lucas_randomlist_env_tl}
  }

\ExplSyntaxOff

\usepackage[inline]{enumitem}
\setlist[enumerate]{
    label = \textbf{Q \arabic*.},
    align = left,
    labelwidth = \parindent, 
    leftmargin = \parindent,
    style = standard
}

\usepackage{tasks}[newest]
\newcommand*\tasklabelformat[1]{\textbf{#1.}}
\settasks{
    item-indent = 2em,
    label-width = 1.5em,
    label-offset = 0.25em,
    label = \Alph*,
    label-format = \tasklabelformat
}

%=========================================================
\begin{document}
\begin{randomlist}[environment=enumerate, seed=1]
\item How many letters in the word ``Tea'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ \ans
        \task $2$
        \task $4$.
   \end{tasks}

\item How many letters in the word ``Hi'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$ \ans
        \task $4$.
   \end{tasks}

\item How many letters in the word ``Cake'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$
        \task $4$. \ans
   \end{tasks}

\item How many letters in the word ``Tree'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$
        \task $4$. \ans
   \end{tasks}

\item How many letters in the word ``Dog'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ \ans
        \task $2$ 
        \task $4$.
   \end{tasks}

\item How many letters in the word ``House'' ?
    \begin{tasks}(4)
        \task $2$
        \task $3$ 
        \task $5$ \ans
        \task $4$.
   \end{tasks}

\item How many letters in the word ``Tea'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ \ans
        \task $2$
        \task $4$.
   \end{tasks}

\item How many letters in the word ``Hi'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$ \ans
        \task $4$.
   \end{tasks}

\item How many letters in the word ``Cake'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$
        \task $4$. \ans
   \end{tasks}

\item How many letters in the word ``Tree'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ 
        \task $2$
        \task $4$. \ans
   \end{tasks}

\item How many letters in the word ``Dog'' ?
    \begin{tasks}(4)
        \task $1$
        \task $3$ \ans
        \task $2$ 
        \task $4$.
   \end{tasks}

\item How many letters in the word ``House'' ?
    \begin{tasks}(4)
        \task $2$
        \task $3$ 
        \task $5$ \ans
        \task $4$.
   \end{tasks}
\end{randomlist}
\answerkey
\end{document}

在此处输入图片描述

相关内容