改进我的考试类答案框代码

改进我的考试类答案框代码

我正在使用考试类进行工作,我发现构建学生填写答案的空间可以显著加快评分过程。为了实现这一点,我创建了一个名为 的新命令\answerbox,它会为学生的回答生成指定的空间。以下是我为此目的实现的代码:

\documentclass[answers]{exam}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}
% Command for small answer Box
% \answerbox{length}{height}{solution}
% solution is only printed if answers is a class option
% solution argument must not be an environment
% tikz can still be added with \tikz{...}
\newcommand{\answerbox}[3]{%
    \pgfmathsetlengthmacro{\halfheight}{0.5*#2cm}
    \pgfmathsetlengthmacro{\halfwidth}{0.5*#1cm}
    \pgfmathsetlengthmacro{\adjustheight}{-0.45*#2cm}
    \ifprintanswers
        \raisebox{\adjustheight}{%
            \begin{tikzpicture}
                \draw (0, 0) rectangle (#1cm, #2cm);
                \node at (\halfwidth, \halfheight) {\color{red!75!black}\large\textbf{#3}};
            \end{tikzpicture}%
        }%
    \else
        \raisebox{\adjustheight}{%
            \begin{tikzpicture}
                \draw (0, 0) rectangle (#1cm, #2cm);
            \end{tikzpicture}%
        }%
    \fi
}
\begin{document}
\begin{questions}
    \question
    \answerbox{3.25}{4}{$\begin{bmatrix}
            0.25\\0.25\\0.25\\0.25
        \end{bmatrix}$}
    
    \answerbox{4}{2}{Richard Feynman}
\end{questions}
\end{document}

我想扩展此代码片段并使其更加健壮。我已经做了一些初步评估,但我希望彻底避免任何未来问题,这可能会给我/其他人带来大量工作。我希望你能帮助我进一步改进它。如果您发现任何潜在问题或有任何深刻的建议,我将不胜感激。

答案1

这显示了一个\framebox解决方案。默认对齐方式是内容的基线。 \raisebox顶部对齐。

\documentclass[answers]{exam}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}
% Command for small answer Box
% \answerbox{length}{height}{solution}
% solution is only printed if answers is a class option
% solution argument must not be an environment
% tikz can still be added with \tikz{...}
\newcommand{\answerbox}[3]{% #1 = width, #2 = height, #3 = contents
\bgroup% make changes local
    \ifprintanswers
      \sbox0{\color{red!75!black}\large\textbf{#3}}% measure contents
      \setlength{\fboxsep}{\dimexpr #2cm /2 - 0.5\ht0 - 0.5\dp0}%
      \raisebox{\dimexpr \ht\strutbox-\height}{\framebox[#1cm]{\usebox0}}%
    \else
      \setlength{\fboxsep}{\dimexpr #2cm /2 - 0.5\baselineskip}%
      \raisebox{\dimexpr \ht\strutbox-\height}{\framebox[#1cm]{\strut}}%
    \fi
\egroup}

\begin{document}
\begin{questions}
    \question
    \answerbox{3.25}{4}{$\begin{bmatrix}
            0.25\\0.25\\0.25\\0.25
        \end{bmatrix}$}
    
    \answerbox{4}{2}{Richard Feynman}
\end{questions}
\end{document}

这将答案置于解决方案框的中心。

\documentclass[answers]{exam}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}
\newcommand{\answerbox}[2]{% #1 = height, #2 = contents
  \begin{solutionbox}{#1cm}
    \sbox0{\color{red!75!black}\large\textbf{#2}}% measure contents
    \strut\par\vskip-\parskip\vskip-\baselineskip\centering
    \raisebox{\dimexpr 0.5\dp0-0.5\ht0}[\dimexpr #1cm /2 - 3\fboxsep]%
      [\dimexpr #1cm /2 - 3\fboxsep]{\usebox0}
\end{solutionbox}}

\begin{document}
\begin{questions}
    \question show a column vector.
    
    \answerbox{4}{$\begin{bmatrix}
            0.25\\0.25\\0.25\\0.25
        \end{bmatrix}$}
    
    \question Who is buried in Richard Feynman's tomb?
    
    \answerbox{2}{Richard Feynman}
\end{questions}
\end{document}

相关内容