练习包中的多项选择题

练习包中的多项选择题

我正在为学生创建一个作业,其中包含几个章节,包括理论、练习和解决方案。我想使用 latex 的练习包创建多项选择题。我相信这个包不直接支持 MCQ。我遇到了一些我组合的代码片段。

我的目标是创建具有一个或多个正确答案的 MCQ,以便所有答案连同解释和分析都出现在章节末尾。

如果有人建议其他包/方法,那也很好。

\documentclass[a4paper]{amsbook}
\usepackage[margin=1in]{geometry}
\usepackage{mathptmx}
\usepackage[cmintegrals, cmbraces]{newtxmath}
\usepackage{hyperref}% you must load it before the exercise package
\usepackage[answerdelayed]{exercise}
\usepackage{environ}

\newlength{\choiceslen}
\newif\ifshowcorrect
\newcounter{choices}
\newcommand{\choicefinal}[1]{%
\ifnum\value{choices}>0 \hfill\fi\egroup
\hspace{0pt}%
\hbox to\choiceslen
\bgroup
\stepcounter{choices}%
\ifcase#1\relax
(\Alph{choices})%
\else
\ifshowcorrect
\expandafter\bfseries
\fi
{(\Alph{choices})}%
\fi\space
}
\newcommand{\choicetemp}[1]{%
\stepcounter{choices}%
\space(\Alph{choices})\cr
}

\NewEnviron{choices}{%
\setcounter{choices}{0}%
\let\choice\choicetemp
\shorthandchoice% <---------- Add \choice shorthand
\settowidth{\choiceslen}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}
\ifdim\choiceslen>.5\textwidth
\setlength{\choiceslen}{\textwidth}%
\else
\ifdim\choiceslen>.25\textwidth
\setlength{\choiceslen}{.5\textwidth}%
\else
\setlength{\choiceslen}{.25\textwidth}%
\fi
\fi
\let\choice\choicefinal
\shorthandchoice% <---------- Add \choice shorthand
\setcounter{choices}{0}%

\begin{flushleft}
\bgroup\BODY\hfill\egroup
\end{flushleft}
}

\newcommand{\shorthandchoice}{%
\let\oldchoice\choice
\renewcommand{\choice}{\oldchoice{0}}
\def\correctchoice{\oldchoice{1}}
}
\parindent 0pt
\linespread{1.1}

\begin{document}
\begin{Exercise}[label={mylabel}]
What is the value of 2+5? Which fruit do you like?(To see the answer click here: \refAnswer{\ExerciseLabel})
\showcorrecttrue
\begin{choices}
\correctchoice apple
\choice banana
\correctchoice orange
\choice pear
\end{choices}
\end{Exercise}
\begin{Answer}[ref=\ExerciseLabel]
7 
\end{Answer}

\begin{ExerciseList}
\Exercise What is the value of $\pi$  \showcorrecttrue
\begin{choices}
\correctchoice apple
\choice banana
\correctchoice orange
\choice pear
\end{choices}
\Answer Approximate value is 22/7.
\Exercise What is the value of $e$
\Answer Approximate value is 2.71...
\end{ExerciseList}
\clearpage
\begin{center}
\textbf{\textit{    Answers.}}
\end{center}
\newpage % I've added a new page only to clearly show the hyperlink. Of course, you don't need it in your document.
\shipoutAnswer
\end{document}

答案1

我不确定这是您想要实现的,但您可以使用tasks包。

答案的更新版本

自 v1.0 版本tasks(2019/10/04)起,该选项counter-format已被弃用,现在可以label像在 中那样使用enumitem

\documentclass[a4paper]{amsbook}
\usepackage[margin=1in]{geometry}
\usepackage{mathptmx}
\usepackage[cmintegrals, cmbraces]{newtxmath}
\usepackage{hyperref}% you must load it before the exercise package
\usepackage[answerdelayed, lastexercise]{exercise}
\usepackage{tasks}
\settasks{
    label = {(\Alph*)},
    label-width=1.4em
    }

\begin{document}
\chapter{Exercises}
\begin{Exercise}[label={mylabel}]
What is the value of 2+5? (To see the answer click here: \refAnswer{\ExerciseLabel})
\begin{tasks}(4)
\task 2
\task 5
\task\label{correctchoice}7
\task 3
\end{tasks}
\end{Exercise}
\begin{Answer}[ref=\ExerciseLabel]
\ref{correctchoice} 7
\end{Answer}
\begin{Exercise}[label={mysecondlabel}]
Which fruit do you like? (To see the answer click here: \refAnswer{\ExerciseLabel})
\begin{tasks}(4)
\task\label{apple}apple
\task banana
\task\label{orange}orange
\task pear
\end{tasks}
\end{Exercise}
\begin{Answer}[ref=\ExerciseLabel]
\ref{apple} apple or \ref{orange} orange 
\end{Answer}
\begin{Exercise}[label={mythirdlabel}]
What is the value of $\pi$? (To see the answer click here: \refAnswer{\ExerciseLabel})
\begin{tasks}(4)
\task 2.15
\task 1.31
\task 4.3
\task\label{correct}3.14
\end{tasks}
\end{Exercise}
\begin{Answer}[ref=\ExerciseLabel]
\ref{correct} 3.14
\end{Answer}
\chapter{Answers}
\shipoutAnswer
\end{document}

旧版答案

\documentclass[a4paper]{amsbook}
\usepackage[margin=1in]{geometry}
\usepackage{mathptmx}
\usepackage[cmintegrals, cmbraces]{newtxmath}
\usepackage{hyperref}% you must load it before the exercise package
\usepackage[answerdelayed, lastexercise]{exercise}
\usepackage{tasks}
\settasks{
    counter-format = {(tsk[A])},
    label-width=1.4em
    }

\begin{document}
\chapter{Exercises}
\begin{Exercise}[label={mylabel}]
What is the value of 2+5? (To see the answer click here: \refAnswer{\ExerciseLabel})
\begin{tasks}(4)
\task 2
\task 5
\task\label{correctchoice}7
\task 3
\end{tasks}
\end{Exercise}
\begin{Answer}[ref=\ExerciseLabel]
\ref{correctchoice} 7
\end{Answer}
\begin{Exercise}[label={mysecondlabel}]
Which fruit do you like? (To see the answer click here: \refAnswer{\ExerciseLabel})
\begin{tasks}(4)
\task\label{apple}apple
\task banana
\task\label{orange}orange
\task pear
\end{tasks}
\end{Exercise}
\begin{Answer}[ref=\ExerciseLabel]
\ref{apple} apple or \ref{orange} orange 
\end{Answer}
\begin{Exercise}[label={mythirdlabel}]
What is the value of $\pi$? (To see the answer click here: \refAnswer{\ExerciseLabel})
\begin{tasks}(4)
\task 2.15
\task 1.31
\task 4.3
\task\label{correct}3.14
\end{tasks}
\end{Exercise}
\begin{Answer}[ref=\ExerciseLabel]
\ref{correct} 3.14
\end{Answer}
\chapter{Answers}
\shipoutAnswer
\end{document}

在这两种情况下,输出都是:

在此处输入图片描述

在此处输入图片描述

相关内容