如何在特定情况下修复随机答案考试的答案代码?(查看更多)

如何在特定情况下修复随机答案考试的答案代码?(查看更多)

以下代码的要点已在关联

如您所见,设置为 \answerkey 的命令正在累积起作用。如何修复它?

\documentclass{article}
\usepackage{xparse,environ,enumitem}

\ExplSyntaxOn
\NewEnviron{randomList}[1][]
 {
  \keys_set:nn { lucas/randomlist } { #1 }
  \lucas_randomlist:V \BODY
 }
\NewDocumentCommand{\answerkey}{}
 {
  \subsubsection*{Answer ~ key}
  \begin{enumerate}[nosep]
  \seq_map_inline:Nn \g_lucas_randomlist_answers_seq { \item ##1 }
  \end{enumerate}
 }
\NewDocumentCommand{\correct}{}
 {
  \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

\newlist{Testlist}{enumerate}{1}
\setlist[Testlist]{label*=\alph*.}
\setlist{nosep}\parindent=0pt% for more compact output


\usepackage{pgfplotstable}
% from https://tex.stackexchange.com/a/445369/121799
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{#3}\of{#1}%
    \let#4\pgfplotsretval
}


\usepackage{filecontents}
\begin{filecontents*}{students.csv}
name 
{Student 1}
{Student 2}
{Student 3}
\end{filecontents*}



\begin{document}
\pgfplotstableread{students.csv}\loadedtable
\pgfplotstablegetrowsof{\loadedtable}
\pgfmathtruncatemacro{\rownum}{\pgfplotsretval-1}
\foreach \X in {0,...,\rownum}
{\ReadOutElement{\loadedtable}{\X}{name}{\tmpname}
\xdef\myname{\tmpname}

\begin{center}
\myname
\end{center}

\begin{enumerate} % outer list of exercises

\item First exercise
  \begin{randomList}[environment=Testlist]
  \item\correct 1
  \item 2
  \item 3
  \item 4
  \item 5
  \end{randomList}

\item Second exercise
  \begin{randomList}[environment=Testlist]
  \item 1
  \item\correct 2
  \item 3
  \item 4
  \item 5
  \end{randomList}



\answerkey

}
\end{document}

在此处输入图片描述

答案1

您必须清除答案键的序列。

这是包含学生名单的方法。修改它以使用外部文件并不困难。

\documentclass{article}
\usepackage[a6paper]{geometry} % small pages for the picture
\usepackage{xparse,environ,enumitem}

\ExplSyntaxOn
\NewDocumentCommand{\studentlist}{m}
 {
  \seq_set_split:Nnn \g_lucas_randomlist_students_seq { , } { #1 }
 }
\NewDocumentCommand{\newstudent}{}
 {
  \clearpage
  \int_gincr:N \g_lucas_randomlist_set_int
  \seq_gclear:N \g_lucas_randomlist_answers_seq
  \section*
   {
    Test ~ for ~ 
    \seq_item:Nn \g_lucas_randomlist_students_seq { \g_lucas_randomlist_set_int }
   }
 }
\NewEnviron{randomList}[1][]
 {
  \keys_set:nn { lucas/randomlist } { #1 }
  \lucas_randomlist:V \BODY
 }
\NewDocumentCommand{\answerkey}{}
 {
  \subsubsection*{Answer ~ key}
  \begin{enumerate}[nosep]
  \seq_map_inline:Nn \g_lucas_randomlist_answers_seq { \item ##1 }
  \end{enumerate}
 }
\NewDocumentCommand{\correct}{}
 {
  \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 },
 }

\int_new:N \g_lucas_randomlist_set_int
\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

\newlist{Testlist}{enumerate}{1}
\setlist[Testlist]{label*=\alph*.}

\begin{document}

\studentlist{lucas,egreg,marmot,duck}

\newstudent

\begin{enumerate} % outer list of exercises

\item First exercise
  \begin{randomList}[environment=Testlist]
  \item\correct First item

  \item Second item

  \item Third item
  \end{randomList}

\item Second exercise
  \begin{randomList}[environment=Testlist]
  \item First item

  \item\correct Second item

  \item Third item
  \end{randomList}

\item Third exercise
  \begin{randomList}[environment=Testlist]
  \item\correct First item

  \item Second item

  \item Third item
  \end{randomList}

\end{enumerate}

\answerkey

\newstudent

\begin{enumerate} % outer list of exercises

\item First exercise
  \begin{randomList}[environment=Testlist]
  \item\correct First item

  \item Second item

  \item Third item
  \end{randomList}

\item Second exercise
  \begin{randomList}[environment=Testlist]
  \item First item

  \item\correct Second item

  \item Third item
  \end{randomList}

\item Third exercise
  \begin{randomList}[environment=Testlist]
  \item\correct First item

  \item Second item

  \item Third item
  \end{randomList}

\end{enumerate}

\answerkey

\newstudent

\begin{enumerate} % outer list of exercises

\item First exercise
  \begin{randomList}[environment=Testlist]
  \item\correct First item

  \item Second item

  \item Third item
  \end{randomList}

\item Second exercise
  \begin{randomList}[environment=Testlist]
  \item First item

  \item\correct Second item

  \item Third item
  \end{randomList}

\item Third exercise
  \begin{randomList}[environment=Testlist]
  \item\correct First item

  \item Second item

  \item Third item
  \end{randomList}

\end{enumerate}

\answerkey

\newstudent

\begin{enumerate} % outer list of exercises

\item First exercise
  \begin{randomList}[environment=Testlist]
  \item\correct First item

  \item Second item

  \item Third item
  \end{randomList}

\item Second exercise
  \begin{randomList}[environment=Testlist]
  \item First item

  \item\correct Second item

  \item Third item
  \end{randomList}

\item Third exercise
  \begin{randomList}[environment=Testlist]
  \item\correct First item

  \item Second item

  \item Third item
  \end{randomList}

\end{enumerate}

\answerkey

\end{document}

在此处输入图片描述

相关内容