章节末尾有多项选择题和答案

章节末尾有多项选择题和答案

我想建立一个多项选择题的环境,这样正确答案的列表和一些注释行将排版在章节末尾。

像这样:

\question
This is the question
\choice First guess
\truechoice Second guess {This is a comment that goes at the end of the chapter}
\choice Third guess

在章节结束时,我希望有一个问题编号(自动写入)、正确答案(正确编号)和评论的列表。

我该怎么办?

答案1

这是使用该包的一些技巧endnotes。我尝试内联一些注释。

\documentclass{article}
\usepackage{endnotes}

% Define two new counters for ease of using.
\newcounter{questionnumber}
\newcounter{choicenumber}[questionnumber]
% Formatting the counters, this is where you change how the counters appear.
\renewcommand*\thequestionnumber{\arabic{questionnumber}.}
\renewcommand*\thechoicenumber{\alph{choicenumber})}

% Define the \question and \choice commands to be similar to what is 
% given in the OP
\newcommand*\question{\item}
\newcommand*\choice{\item}
% Here's a bit of a hack: \endnotetext stores the \meaning of the its argument
% in the endnotetext file, so the macros aren't expanded. I use \edef to fully
% expand the current \thechoicenumber, and use \expandafter to stuff it into
% the argument for \endnotetext. Suggestions for improvements are welcome!
\newcommand*\entreplace[1]{\endnotetext[\value{questionnumber}]{#1}}
\makeatletter
\newcommand\truechoice[1]{\item \protected@edef\currentcount{\thechoicenumber~~#1} \expandafter\entreplace\expandafter{\currentcount}}
\makeatother
% Define a choices environment, just a list basically. 
\newenvironment{choices}{\begin{list}{\thechoicenumber}{\usecounter{choicenumber}}}{\end{list}}

% These are to set up the endnotes. The first makes the endnote marks look 
% like the question numbering (as opposed to being in superscript). The second
% sets the endnotes heading to read "Answers". 
\renewcommand*\makeenmark{\theenmark.~~}
\renewcommand*\notesname{Answers:}

\begin{document}
\begin{list}{\thequestionnumber}{\usecounter{questionnumber}}
\question Some question
\begin{choices}
\choice False choice
\choice Bad choice
\truechoice{This is true for no good reason} True choice.
\end{choices}

\question Who won the 2012 Presidential Election in US?
\begin{choices}
\choice Mitt Romney
\truechoice{Without even counting Florida} Barack Obama
\end{choices}
\end{list}

% Print the "answers"
\theendnotes
\end{document}

输出如下:

在此处输入图片描述


编辑:

回应下面 zar 的评论,如果我们希望输入语法是

\truechoice{Choice text}{Justification/commentary}

代替

\truechoice{Justification/commentary} Choice text

可以将定义改为

\newcommand\truechoice[2]{\item #1 \protected@edef\currentcount{\thechoicenumber~~#2} \expandafter\entreplace\expandafter{\currentcount}}

但是这样你就必须把选择文本放在括号中,例如,你必须写

\truechoice{True Choice!}{That choice was true for no good reason} 

编辑2新加坡沙特注意到滥用该\edef功能存在一些问题。下面是一个更复杂但功能更好的破解方法(我在下面附上了完整文件,以便于复制粘贴)

\documentclass{article}
\usepackage{endnotes}

% Define two new counters for ease of using.
\newcounter{questionnumber}
\newcounter{choicenumber}[questionnumber]
% Formatting the counters, this is where you change how the counters
% appear.
\renewcommand*\thequestionnumber{\arabic{questionnumber}.}
\renewcommand*\thechoicenumber{\alph{choicenumber})}

% Define the \question and \choice commands to be similar to what is 
% given in the OP
\newcommand*\question{\item}
\newcommand*\choice{\item}
% Here's a bit of a hack: \endnotetext stores the \meaning of the its argument
% in the endnotetext file, so the macros aren't expanded. I use \edef to fully
% expand the current \thechoicenumber, and use \expandafter to stuff it into
% the argument for \endnotetext. Suggestions for improvements are welcome!
\newcommand*\entreplace[1]{\endnotetext[\value{questionnumber}]{#1}}
\newcommand\truechoice[2]{\item #1 \edef\tempchoice{\thechoicenumber} \expandafter\def\expandafter\currentcount\expandafter{\tempchoice \ \ #2} \expandafter\entreplace\expandafter{\currentcount}}

% Define a choices environment, just a list basically. 
\newenvironment{choices}{\begin{list}{\thechoicenumber}{\usecounter{choicenumber}}}{\end{list}}

% These are to set up the endnotes. The first makes the endnote marks look 
% like the question numbering (as opposed to being in superscript). The second
% sets the endnotes heading to read "Answers". 
\renewcommand*\makeenmark{\theenmark.~~}
\renewcommand*\notesname{Answers:}

\begin{document}
\begin{list}{\thequestionnumber}{\usecounter{questionnumber}}
\question Some question
\begin{choices}
\choice False choice
\choice Bad choice
\truechoice{True choice}{This is true for no good reason $1+1$. Let me
add more text. There should be four or five lines. Can I put displayed
math in here? \emph{test} \[ E = mc^2\] Some more text.}
\end{choices}
\end{list}

% Print the "answers"
\theendnotes
\end{document}

这就是它的输出

在此处输入图片描述

我们可以看出它正确地处理了\emph命令和数学(显示和内联)环境。

答案2

您可以使用exsheets包裹结合tasks使用。它的目的是将练习的解决方案打印在文档的另一部分。但是,它在设计时并没有考虑到多项选择测试,这意味着你必须输入两次:

\documentclass{book}

\usepackage{exsheets}
\usepackage{tasks}

% we could use `enumitem' for this (or...)
% but exsheets v0.4 provides a command to generate list-like
% environments
% this defines an environment {choices} that uses a square box as
% item symbol, \choice as command to start an item and uses 2 columns:
\NewTasks[style=multiplechoice]{choices}[\choice](2)

\newcommand*\correct{\PrintSolutionsTF{\checkedchoicebox}{\choicebox}}

\begin{document}

\chapter{Some chapter}
\section{Exercises}
\begin{question}
 \begin{choices}
  \choice First choice
  \choice Second choice
  \choice[\correct] Third choice
  \choice Fourth choice
 \end{choices}
\end{question}
\begin{solution}
 \begin{choices}
  \choice First choice
  \choice Second choice
  \choice[\correct] Third choice\\
    This is the right choice because...
  \choice Fourth choice
 \end{choices}
\end{solution}

\begin{question}
 \begin{choices}
  \choice First choice
  \choice[\correct] Second choice
  \choice Third choice
  \choice Fourth choice
 \end{choices}
\end{question}
\begin{solution}
 \begin{choices}
  \choice First choice
  \choice[\correct] Second choice\\
    This is the right choice because...
  \choice Third choice
  \choice Fourth choice
 \end{choices}
\end{solution}

\section{Solutions}
\printsolutions[chapter]

% similar for other chapters

\end{document}

在此处输入图片描述

相关内容