2

I am new to knitr/Sweave. I need to prepare different versions of an exam with randomly generated questions. One version per student, each with the student's name on it. Below is a simple example with just two students.

\documentclass{article}

\begin{document}

<<echo = FALSE>>=
x <- sample(0:9, 2)
@
William Sealy Gosset $\Sexpr{x[1]}+\Sexpr{x[2]}=$\clearpage

<<echo = FALSE>>=
x <- sample(0:9, 2)
@
Carl Friedrich Gauss $\Sexpr{x[1]}+\Sexpr{x[2]}=$\clearpage

\end{document}

As I have one hundred students, I would prefer to have kintr loop through a list of students.

  • My approach would be to structure your class lists and symbolic question templates in a database, then use any language of your choice to generate the latex code for you. A simple batch script could then compile all exams at once. – EngBIRD Jul 12 '15 at 16:27
  • Indeed, an option would be to use make and leave knitr aside. – Primo Petri Jul 12 '15 at 16:52
2

Put all the names in name.txt file, each name on a separate line, and then use this Sweave code:

\documentclass[12pt]{article}

\usepackage{Sweave}

\title{Exam}

\author{Teacher}

\begin{document}
\SweaveOpts{concordance=TRUE}

\maketitle

\newpage

<<echo = FALSE, results=tex>>=
name=readLines("name.txt")
n=length(name)
x=sample(0:9, n)
y=sample(0:9, n)

for (i in 1:n) {
  cat(name[i],'\\\\')
  cat('$',x[i],'+',y[i],'=$\\newpage\n',sep="")
}
@

\end{document}
  • The only problem is that with more complex questions, it may get inpractical to typeset. – Primo Petri Jul 12 '15 at 19:23
  • Yes, in fact I don't use Sweave for practical purposes, because it is too complicated. There are appropriate ways that you can do this, like generating LaTeX output and then processing it. – Ho1 Jul 13 '15 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.