如何使用 Sweave 通过 examdesign 包随机化内容

如何使用 Sweave 通过 examdesign 包随机化内容

如果这是一个愚蠢的问题,或者过于具体到某个包,请提前原谅我。我一直用它examdesign来随机化问题的顺序,但我也想随机生成内容。我已经开始用它Sweave来为一些问题在 R 中生成随机数。但是,当我要求几个不同的版本时,它会将内容从一个版本保持到下一个版本相同。换句话说,它首先从 R 创建随机内容,然后在多个版本中使用相同的内容。我希望它做的是每次创建新版本时调用 R 函数。这是一个例子:

\documentclass[10pt]{examdesign}
\usepackage{Sweave}
\usepackage{amsmath}
\NumberOfVersions{2}

\begin{document}
\begin{examtop}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{ll}
Name:& \rule{2in}{.4pt}\\
\end{tabular}
\vspace{0.5in}
  \begin{center}
    \textbf{Introduction to Statistics} \\
    \textbf{Practice Exam, Form \Alph{version}} \\
  \end{center}
\end{examtop}

\begin{shortanswer}[title={\Large Short Answer (10 pts each)}]

\begin{question}
<<ifst, echo=FALSE>>==
N = round(runif(1, 5, 20))
mu = 75
xbar = round(runif(1, 76, 84))
sig = round(runif(1, 5, 15))
z = round((xbar-mu)/(sig/sqrt(N)), digits=2)
p = round(1-pnorm(z), digits=4)
@

John wants to know whether his students are smarter than the average student. 
John has \Sexpr{N} students and their average was \Sexpr{xbar}. 
Assuming a population $\mu$ of \Sexpr{mu} and a standard deviation of 
\Sexpr{sig}, can we conclude his students are smarter? 

  \examvspace*{1.5in}
  \begin{answer}
    \begin{align}
    \nonumber Z_{obt} = \frac{\Sexpr{xbar}-\Sexpr{mu}}{\Sexpr{sig}/\sqrt{\Sexpr{N}}} =& \Sexpr{z} \\
    \nonumber p_{obt} =& \Sexpr{p} 
    \end{align}
  \end{answer}
\end{question}

\end{shortanswer}

\end{document}

您会注意到,一旦您对其进行了 sweave,就会有两个版本,但两个版本的平均值/sd/N 将相同。如何让它们生成不同的随机数?

答案1

根据 Ben Bolker 的评论:

只需复制并粘贴整个question环境。编辑块的名称。然后,Sweave 将运行代码两次并输出相同的文本,但输入的数字不同\Sexpr

\begin{shortanswer}[title={\Large Short Answer (10 pts each)}]

\begin{question}
<<ifst1, echo=FALSE>>==
N = round(runif(1, 5, 20))
@

John wants to know whether his students are smarter than the average student. 
John has \Sexpr{N} students \dots

  \examvspace*{1.5in}
  \begin{answer}
  \end{answer}
\end{question}

\begin{question}
<<ifst2, echo=FALSE>>==
N = round(runif(1, 5, 20))
@

John wants to know whether his students are smarter than the average student. 
John has \Sexpr{N} students \dots

  \examvspace*{1.5in}
  \begin{answer}
  \end{answer}
\end{question}

\end{shortanswer}

这可行,但严重违反了干燥原理。针织品,Sweave 的后继者,您可以编写 R 代码来创建整个question环境并将其逐字写入生成的 LaTeX 代码中。(也许 Sweave 也可以做到这一点?)类似这样的内容:

\begin{shortanswer}[...]
    <<generate, echo=FALSE, format=asis>>=
    for (i in 1:2) {
      N = round(runif(1, 5, 20))
      paste('\\begin{question}', 'John has', N, 'students', ..., '\\end{question}')
    }
    @
\end{shortanswer}

相关内容