根据单个评论自动涂黑答案

根据单个评论自动涂黑答案

我正在设计由多项选择题组成的试卷。答案将在答题纸上用黑色标记适当的答案(可能不止一个正确)。在我的 latex 文件中,我用一个标签来指示正确的选项,\correct该标签会在该选项中打印一个项目符号。此方案由注释行控制

%\renewcommand{\correct}{} % comment out for printing the solution

如果注释了上述内容,则只有在输出 PDF 文件中才会显示答案。如果取消注释,则会打印有效的试卷。

我希望使用相同的方案自动将答题纸上的相应选项标记为半透明黑色实心圆圈。但是我不知道如何将正确的选项标记为深色圆圈。任何帮助都将不胜感激。

请在此处查看 MWE——

\documentclass[11pt]{article}
\usepackage{tasks,pgffor,tikz,xsim,multicol}

\settasks{
    counter-format = (tsk[a]),
    label-format = \itshape,
    item-indent = 0em, 
    label-offset=.6em, 
    label-align=right, 
    before-skip = 0pt, 
    after-item-skip=0pt
}

\newcommand\encircle[1]{%
    \tikz[baseline=(X.base)] 
    \node (X) [draw, shape=circle, inner sep=-1pt] {\strut #1};
}

% mcqoptions environment is seemingly dummy only in this MWE. In actual document it does a lot more. 
\newenvironment{mcqoptions}{
    \tasks(4)
}{
    \endtasks
}

\setlength{\columnseprule}{0.4pt}
\newcommand{\correct}{$\bullet$}

\newcommand{\answerspace}[1]{%
    \foreach \x in {1,...,#1} {%
        \noindent\hspace*{2em}\llap{[\textbf{\x}]} \encircle{$a$}\,\encircle{$b$}\,\encircle{$c$}\,\encircle{$d$}\hfill\newline%
    }%
}

% If the following line is not commented out, mark all correct answers by blackening the corresponding circle.
%\renewcommand{\correct}{} % comment out for printing the solution

\begin{document}
    One or more options of the following questions may be correct.
    \begin{multicols}{2}    
        \begin{exercise}
            Why o why
            \begin{mcqoptions}
                \task \correct $ \frac{e+2}{e} $ \task $ \frac{e-2}{e} $ \task $ \frac{2}{e} $ \task $ \frac{2-e}{e} $
            \end{mcqoptions}
        \end{exercise}

        \begin{exercise}
            For a reaction in an aqueous medium at a given pH of volume 
            \begin{mcqoptions}
                \task q \task \correct w \task e    \task \correct r
            \end{mcqoptions}
        \end{exercise}

        \begin{exercise}
            $\frac{\tan x-\log(1+x)+x^2}{x^2} =$
            \begin{mcqoptions}
                \task \correct 3/2 \task \correct 1/2 \task 5/2 \task 1 
            \end{mcqoptions}
        \end{exercise}

        \begin{exercise}
            How much heat is gained by the environment in the process? 
            \begin{mcqoptions}
                \task 1170 \task 5080 \task 2350 \task \correct 3690
            \end{mcqoptions}
        \end{exercise}
    \end{multicols} 
    \vspace{.5cm}
    \hrule
    \vspace{.1cm}
    \textbf{Mark your answers below}
    \vspace{.1cm}
    \hrule
    \begin{multicols}{2}
        \answerspace{4}
    \end{multicols}
\end{document}

一个图像 - MWE 图像

答案1

任务包装用途LaTeX3/expl3因此获取任务编号的唯一方法是使用解释3。[好的,您可以expl3仅使用 来提取任务编号,该任务编号存储为\g__tasks_int,然后使用“普通乳胶”,但如果您开始使用,expl3您不妨继续使用expl3。]

在下面的 MWE 中,我向\correct宏添加了一些代码,以便每当它用于将答案标记为正确时,任务编号就会添加到expl3 顺序对于当前问题(序列名称的形式为g_correct_#1_seqwhere #1is \theexercise)。稍后,在\answerspace宏中,我们通过检查此序列的成员资格来检查当前选项是否“标记”为正确,然后fill=blue!10如果此选项正确则将其添加到节点。因此,使用以下代码,MWE 的答案部分如下所示:

在此处输入图片描述

以下是代码:

\documentclass[11pt]{article}
\usepackage{tasks,pgffor,tikz,xsim,multicol}
\usepackage{etoolbox}
\settasks{
    counter-format = (tsk[a]),
    label-format = \itshape,
    item-indent = 0em,
    label-offset=.6em,
    label-align=right,
    before-skip = 0pt,
    after-item-skip=0pt
}

\newcommand\encircle[2][]{% optional argument allows for fill
    \tikz[baseline=(X.base)]
    \node (X) [draw, shape=circle, inner sep=-1pt, #1] {\strut$#2$};
}

% mcqoptions environment is seemingly dummy only in this MWE. 
% In actual document it does a lot more.
\newenvironment{mcqoptions}{
    \tasks(4)
}{
    \endtasks
}

\setlength{\columnseprule}{0.4pt}
  \newif\ifcorrecting
  \correctingtrue% comment out to not print solutions
  \ExplSyntaxOn
  \newcommand{\correct}{%
    $\bullet$%
    \ifcorrecting%
      \l_add_to_sequence {\theexercise} {\int_use:N \g__tasks_int}%
    \fi%
  }

\newcommand{\answerspace}[1]{%
    \foreach \x in {1,...,#1} {%
        \noindent\hspace*{2em}\llap{[\textbf{\x}]}\space
        \foreach \y in {1,2,3,4} {% answers are stored as integers
          \l_encircle {\x} {\y}\,% circle the node, possibly with fill
        }\hfill\newline%
    }%
}

% add #2 to the sequence g_correct_#1_seq, creating it if it does not exist
\cs_new_protected:Npn \l_add_to_sequence #1#2 {
  \seq_if_exist:cF {g_correct_#1_seq} {\seq_new:c {g_correct_#1_seq} }
  \seq_gput_right:cx {g_correct_#1_seq} {#2}
}
% test if correct and then draw the node using \encircle
\cs_new_protected:Npn \l_encircle #1#2 {
   \seq_if_in:coTF {g_correct_#1_seq} {#2}
       { \encircle[fill=blue!10]{\int_to_alph:n{#2}} }
       { \encircle{ \int_to_alph:n{#2}} }
}
\ExplSyntaxOff

% If the following line is not commented out, mark all correct answers by blackening the corresponding circle.

\begin{document}
    One or more options of the following questions may be correct.
    \begin{multicols}{2}
        \begin{exercise}
            Why o why
            \begin{mcqoptions}
                \task \correct $ \frac{e+2}{e} $ \task $ \frac{e-2}{e} $ \task $ \frac{2}{e} $ \task $ \frac{2-e}{e} $
            \end{mcqoptions}
        \end{exercise}

        \begin{exercise}
            For a reaction in an aqueous medium at a given pH of volume
            \begin{mcqoptions}
                \task q \task \correct w \task e    \task \correct r
            \end{mcqoptions}
        \end{exercise}

        \begin{exercise}
            $\frac{\tan x-\log(1+x)+x^2}{x^2} =$
            \begin{mcqoptions}
                \task \correct 3/2 \task \correct 1/2 \task 5/2 \task 1
            \end{mcqoptions}
        \end{exercise}

        \begin{exercise}
            How much heat is gained by the environment in the process?
            \begin{mcqoptions}
                \task 1170 \task 5080 \task 2350 \task \correct 3690
            \end{mcqoptions}
        \end{exercise}
    \end{multicols}
    \vspace{.5cm}
    \hrule
    \vspace{.1cm}
    \textbf{Mark your answers below}
    \vspace{.1cm}
    \hrule
    \begin{multicols}{2}
        \answerspace{4}
    \end{multicols}
\end{document}

请注意,我曾经将fill=blue~10正确答案标记为fill=black太暗。这是使用标准蒂克兹,因此可以轻松更改以满足您的需要。

上面的代码几乎肯定可以变得更高效。据我所知,创建具有动态名称的序列(例如上面的序列\g_correct_1_seq\g_correct_2_seq、...)的唯一方法是分两步进行。这也是我将函数\cs_new_protected:Npn和分开的原因之一\cs_new_protected:Npn

请注意,已经添加了,以便您可以通过分别添加和\newif\ifcorrecting来添加/删除答案的阴影。\correctingtrue\correctingfalse

最后,我要说的是,双下划线意味着\g__tasks_int是代码中的“受保护”变量任务包,这意味着它可能会在将来的版本中发生变化,因此我们实际上不应该使用它。话虽如此,我看不出还有其他选择,因为我们需要访问此计数器的值,而且据我所知,该包不提供钩子。

相关内容