具有正确选择环境的多项选择题

具有正确选择环境的多项选择题

我正在写一些笔记,其中需要写一些客观题。我还需要在一些题目中有一个或两个选项是正确的。我该如何定义一个正确的选择环境并在最后打印所有答案。

我在 stackexchange 上查看了一些答案,但它们不符合我的需求。提前致谢。

    \documentclass{article}
    \usepackage{tasks}
    \NewTasks[style=enumerate,counter-format=($tsk[a]$),label-width=4ex]{choice}[\choice](1)
    \NewTasks[style=enumerate,counter-format=($tsk[a]$),label-width=4ex]{choice2}[\choice](2)
    \NewTasks[style=enumerate,counter-format=($tsk[a]$),label-width=4ex]{choice4}[\choice](4)


    \begin{document}

    \begin{enumerate}
    \item What is the product of $-2$ and $3$?
    \begin{choice}
      \choice $-6$
      \choice $6$
      \choice $5$
      \choice $-5$
    \end{choice}
    \item What is the sum of the sides of a polygon called?
    \begin{choice2}
      \choice Leg
      \choice Perimeter
      \choice Area
      \choice Volume
    \end{choice2}
    \item What is the product of $-2$ and $3$?
    \begin{choice4}
      \choice $-6$
      \choice $6$
      \choice $5$
      \choice $-5$
    \end{choice4}
    \end{enumerate}
    \end{document} 

答案1

这是一个想法(也将其作为示例写入xsim手册):

建议的语法是

\section{Problems}
\begin{questions}
  \begin{exercise}
    What is the product of $-2$ and $3$?
    \begin{choice}(4)
      \choice \answer{$-6$}
      \choice $6$
      \choice $5$
      \choice $-5$
    \end{choice}
  \end{exercise}
    ...
\end{questions}

\section{Answers}
\getanswers

它可以通过软件包xsimtasks以下前言设置获得:

\usepackage[no-files]{xsim}% the `no-files` option is not necessary
\usepackage{tasks}

\DeclareExerciseEnvironmentTemplate{item}
  {\item[\GetExerciseProperty{counter}]}
  {}

\DeclareExerciseProperty{answer}

\newcommand*\answer[1]{%
  \expanded{%
    \SetExerciseProperty{answer}
      { (\noexpand\textit{\alph{task}}) \unexpanded{#1}}}%
  #1%
}

\newcommand*\getanswers{%
  \def\betweenanswers{\def\betweenanswers{\hspace{2em}}}%
  \ForEachUsedExerciseByID{%
    \betweenanswers##3\ExercisePropertyGet{##1}{##2}{answer}%
  }%
}

\xsimsetup{
  exercise/template = item,
  exercise/the-counter = \arabic{exercise}.
}

\NewTasksEnvironment[
  label = (\textit{\alph*}) ,
  label-width = 14pt
]{choice}[\choice]

\newenvironment{questions}
   {\itemize}
   {\enditemize}

并给出以下结果:

在此处输入图片描述

完整代码:

\documentclass{article}
\usepackage[no-files]{xsim}
\usepackage{tasks}

\DeclareExerciseEnvironmentTemplate{item}
  {\item[\GetExerciseProperty{counter}]}
  {}

\DeclareExerciseProperty{answer}

\newcommand*\answer[1]{%
  \XSIMexpandcode{%
    \SetExerciseProperty{answer}
      { (\noexpand\textit{\alph{task}}) \unexpanded{#1}}}%
  #1%
}

\newcommand*\getanswers{%
  \def\betweenanswers{\def\betweenanswers{\hspace{2em}}}%
  \ForEachUsedExerciseByID{%
    \betweenanswers##3\ExercisePropertyGet{##1}{##2}{answer}%
  }%
}

\xsimsetup{
  exercise/template = item,
  exercise/the-counter = \arabic{exercise}.
}

\NewTasksEnvironment[
  label = (\textit{\alph*}) ,
  label-width = 14pt
]{choice}[\choice]

\newenvironment{questions}
   {\itemize}
   {\enditemize}

\begin{document}

\section{Problems}
\begin{questions}
  \begin{exercise}
    What is the product of $-2$ and $3$?
    \begin{choice}(4)
      \choice \answer{$-6$}
      \choice $6$
      \choice $5$
      \choice $-5$
    \end{choice}
  \end{exercise}
  \begin{exercise}
    What is the sum of the sides of a polygon called?
    \begin{choice}(2)
      \choice Leg
      \choice \answer{Perimeter}
      \choice Area
      \choice Volume
    \end{choice}
  \end{exercise}
  \begin{exercise}
    What is the sum of $-2$ and $-3$?
    \begin{choice}(4)
      \choice $-6$
      \choice $6$
      \choice $5$
      \choice \answer{$-5$}
    \end{choice}
  \end{exercise}
\end{questions}

\section{Answers}
\getanswers

\end{document}

相关内容