请修改以下对应于多项选择表的代码以获得如下图所示的结果。
\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\answergrid}{ m m m }{
\begin{tikzpicture}[y=.6cm]
\seq_set_split:Nnn \splitted_seq{;}{#3}
\newcounter{answer}
\int_step_inline:nnnn {1} {1} {#2} {
\stepcounter{answer}
\node[fill=green!10,draw=black,minimum~width=6mm,minimum~height=4mm] at (##1+1,#1) {\Alph{answer}};}
\int_step_inline:nnnn {1} {1} {#1} {
\node[fill=black!50,text=white, minimum~width=6mm,minimum~height=4mm] at (1, #1-##1) {\bf ##1};
\int_step_inline:nnnn {1} {1} {#2} {
\int_compare:nNnTF {####1} = {\seq_item:Nn \splitted_seq {#1-##1+1}}
{
\node[fill=red!10,draw,minimum~width=7mm,minimum~height=4mm] at (####1+1, ##1-1) {};
}
{\node[draw=blue,minimum~width=7mm,minimum~height=4mm] at (####1+1, ##1-1) {};}
}}
\end{tikzpicture}
}
\ExplSyntaxOff
\begin{document}
\par
\answergrid {14} {4} {2;0;0;0;1;0;2;0;0;0;2;0;2;4}
\end{document}
答案1
一种不同的方法。
我大量编辑了代码如何生成多项选择题考试的动态答题纸,但循环例程特别有用。
修改后的答案
已编辑以提供\answergrid{<no.-choices>}{<answer-key>}
界面。因此,调用将\answergrid{5}{B;0;0;A;C;0;0;0;0;0;D;0;B;E}
指示选项 AE,其中有 14 个问题,其答案由列表给出(0
表示未指定答案)。
{B;E}
重新编辑以允许对给定的问题有多个正确答案,使用给定问题答案的语法。
\documentclass{article}
\usepackage{xcolor,graphicx,amssymb}
\usepackage[usestackEOL]{stackengine}
\newcommand{\repeatntimes}[2]{%
\newcount\i%
\i=0%
\newtoks\ans%
\loop%
\ans=\expandafter{\the\ans #2}%
\advance\i by 1%
\ifnum\i<#1\repeat%
\the\ans%
}
\newcommand{\countRepeatntimes}[2]{%
\newcount\i%
\i=0%
\newtoks\ans%
\loop%
\advance\i by 1%
\ans=\expandafter{\the\ans #2}%
\ifnum\i<#1\repeat%
\the\ans%
}
\newcommand\numberbox[1]{\colorbox{black!50}{\makebox[1em]{\textcolor{white}{#1}}}}
\newcommand\letterbox[2][green!10]{\fcolorbox{blue}{#1}{\makebox[1.2em]{#2}}}
\newcounter{problem}
\newcounter{choice}
\makeatletter
\newcommand{\generateQs}[2][0]{%
\getnextanswer#1;0\relax%
\countRepeatntimes{#2}{\stepcounter{problem}\savestack{\Prob}{%
\numberbox{\theproblem}}%
\setcounter{choice}{0}%
\repeatntimes{\numexpr\NumberOfChoices-1}{\stepcounter{choice}%
\if\Alph{choice}\nextanswer\relax%
\savestack{\Prob}{\stackunder{\Prob}{\letterbox[red!10]{\Alph{choice}}}}%
\expandafter\getnextanswer\moreanswers;0\relax%
\else%
\savestack{\Prob}{\stackunder{\Prob}{\letterbox{\Alph{choice}}}}%
\fi%
}\Prob\kern7pt%
}%
}
\def\getnextanswer#1;#2\relax{%
\edef\nextanswer{#1}%
\edef\moreanswers{#2}%
}
\makeatother
\def\NumberOfChoices{4}
\newcommand\answergrid[2]{\renewcommand\NumberOfChoices{#1}\makegrid #2;\relax\relax}
\def\makegrid#1#2#3\relax{%
\generateQs[#1]{1}\ifx\relax#3\else\makegrid#3\relax\fi%
}
\begin{document}
\answergrid{5}{{B;E};0;0;{A;C;D};C;0;0;0;0;0;D;0;B;E}
\end{document}
原始答案
操作宏是\generateQs[<answer>]{<count>}
。当<answer>
指定 时,<count>
通常是1
,并且该[那些]问题的答案将以粉红色阴影显示(其余为绿色)。如果<answer>
指定 否,则所有可能的答案都以绿色阴影显示,<count>
并可用于生成多个这样的答案块。
选择数 (AD) 被硬连线为 的\generateQs
参数。如果问题计数需要重置,则计数器需要重置为零。3
\repeatntimes
problem
\documentclass{article}
\usepackage{xcolor,graphicx,amssymb}
\usepackage[usestackEOL]{stackengine}
\newcommand{\repeatntimes}[2]{%
\newcount\i%
\i=0%
\newtoks\ans%
\loop%
\ans=\expandafter{\the\ans #2}%
\advance\i by 1%
\ifnum\i<#1\repeat%
\the\ans%
}
\newcommand{\countRepeatntimes}[2]{%
\newcount\i%
\i=0%
\newtoks\ans%
\loop%
\advance\i by 1%
\ans=\expandafter{\the\ans #2}%
\ifnum\i<#1\repeat%
\the\ans%
}
\newcommand\numberbox[1]{\colorbox{black!50}{\makebox[1em]{\textcolor{white}{#1}}}}
\newcommand\letterbox[2][green!10]{\fcolorbox{blue}{#1}{\makebox[1.2em]{#2}}}
\newcounter{problem}
\newcounter{choice}
\newcommand{\generateQs}[2][0]{%
\countRepeatntimes{#2}{\stepcounter{problem}\savestack{\Prob}{%
\numberbox{\theproblem}}%
\setcounter{choice}{0}%
\repeatntimes{3}{\stepcounter{choice}% <--CHOICE OF {3} PRINTS A,B,C,D
\if\Alph{choice}#1\relax%
\savestack{\Prob}{\stackunder{\Prob}{\letterbox[red!10]{\Alph{choice}}}}%
\else%
\savestack{\Prob}{\stackunder{\Prob}{\letterbox{\Alph{choice}}}}%
\fi%
}\Prob\kern7pt%
}%
}
\begin{document}
\generateQs[B]{1}%
\generateQs{5}
\generateQs[C]{1}%
\generateQs{4}
\generateQs[A]{1}%
\generateQs{2}
\end{document}
\scriptsize
这是字体大小减小(至)、答案框宽度减小、 减小\fboxsep
(至 1.5pt)以及问题间距\kern
减小(从 7pt 到 5pt)的版本。
\documentclass{article}
\usepackage{xcolor,graphicx,amssymb}
\usepackage[usestackEOL]{stackengine}
\newcommand{\repeatntimes}[2]{%
\newcount\i%
\i=0%
\newtoks\ans%
\loop%
\ans=\expandafter{\the\ans #2}%
\advance\i by 1%
\ifnum\i<#1\repeat%
\the\ans%
}
\newcommand{\countRepeatntimes}[2]{%
\newcount\i%
\i=0%
\newtoks\ans%
\loop%
\advance\i by 1%
\ans=\expandafter{\the\ans #2}%
\ifnum\i<#1\repeat%
\the\ans%
}
\def\FS{\scriptsize}
\newcommand\numberbox[1]{\colorbox{black!50}{\makebox[.8em]{\textcolor{white}{\FS#1}}}}
\newcommand\letterbox[2][green!10]{\fcolorbox{blue}{#1}{\makebox[1em]{\FS#2}}}
\newcounter{problem}
\newcounter{choice}
\newcommand{\generateQs}[2][0]{\fboxsep=1.5pt\relax%
\countRepeatntimes{#2}{\stepcounter{problem}\savestack{\Prob}{%
\numberbox{\theproblem}}%
\setcounter{choice}{0}%
\repeatntimes{3}{\stepcounter{choice}% <--CHOICE OF {3} PRINTS A,B,C,D
\if\Alph{choice}#1\relax%
\savestack{\Prob}{\stackunder{\Prob}{\letterbox[red!10]{\Alph{choice}}}}%
\else%
\savestack{\Prob}{\stackunder{\Prob}{\letterbox{\Alph{choice}}}}%
\fi%
}\Prob\kern5pt%
}%
}
\begin{document}
\generateQs[B]{1}%
\generateQs{5}
\generateQs[C]{1}%
\generateQs{4}
\generateQs[A]{1}%
\generateQs{2}
\end{document}
答案2
答案3
这是另一种方法,但我无法重复以下代码 14 次:
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-n) {\Alph{answer}}; from n=1 to n=14
\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\answergrid}{ m m m }{
\begin{tikzpicture}[x=.6cm, rotate=90]
\seq_set_split:Nnn \splitted_seq{;}{#3}
\newcounter{answer}
\int_step_inline:nnnn {1} {1} {#2} {
\stepcounter{answer}
%----> the problem!!!
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-1) {\Alph{answer}};
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm,] at (6-##1,#1-2) {\Alph{answer}};
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-3) {\Alph{answer}};
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-4) {\Alph{answer}};
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-5) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-6) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-7) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-8) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-9) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-10) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-11) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-12) {\Alph{answer}};\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-13) {\Alph{answer}};
\node[fill=green!10,minimum~width=6mm,minimum~height=4mm] at (6-##1,#1-14) {\Alph{answer}};}
\int_step_inline:nnnn {1} {1} {#1} {
\node[fill=black!50,text=white, minimum~width=6mm,minimum~height=4mm] at (6, #1-##1) {\bf ##1};
\int_step_inline:nnnn {1} {1} {#2} {
\int_compare:nNnTF {####1} = {\seq_item:Nn \splitted_seq {#1-##1+1}} {
\node[fill=red!10,draw,minimum~width=7mm,minimum~height=4mm] at (6-####1, ##1-1) {B};
}{
\node[draw=blue,minimum~width=7mm,minimum~height=4mm] at (6-####1, ##1-1) {};
}
}
}
\end{tikzpicture}
}
\ExplSyntaxOff
\begin{document}
\par
\answergrid {15} {5} {2;0;0;0;0;0;2;0;0;0;0;2;0;0;0;0;0}
\end{document}