Créer des versions étudiants/enseignants à partir d’un unique fichier SweaveProducing student/teacher versions from a single Sweave file
This tutorial describes how to integrate computable R code (and outputs) in a LaTeX documents in a such a way that a single file can generate two (or more) PDF files. This trick is particularly useful for creating worksheets for R courses.</p>
R code is easily embedded in LaTeX documents using Sweave. Using Sweave is easy: you create a standard LaTeX file with the extension
I have been using different tricks for creating worksheets with a teacher/student versions (including or excluding the solutions) for a while. These include in particular:
which, respectively, include/exclude the text written between
and
Unfortunately, none of these two solutions work combined with R chunks in Sweave files because of the way the R chunks are embedded in special environments in the output LaTeX file.
To combine both needs, a simple solution consists in using direct TeX script. A variable
In our example, this variable can take two values: 1 (teacher version) and 0 (student version). Teacher specific text is included after the
and similarly for the student specific text. A complete example (in which I included several tricks with LaTeX, Sweave files and chunk options) is provided at the end of this tutorial, which can be compiled to give this document for students and this document for the teacher.
A very small introduction to integrating R code in LaTeX files
.Rnw
and you can include R code chunks using special delimiters:
When the file is compiled (the best way to do it is to rely on the R package knitr under a RStudio environment because the production and compilation of Sweave files are integrated in a very handy manner), the R chunks are evaluated and the results (screen prints and figures) directly included in a .tex
which is generated and can be itself compiled to obtain a PDF files including comments, R code and results. R chunks can include various options such as a chunk’s name, cache
(to cache the results from one compilation), echo
(to show/hide the R code), results
(to specify how the results must be printed or even if they must be printed)…
A very small introduction to producing different PDF files from a single LaTeX file
\version<code> is first defined:
<pre lang="LaTeX">
\def\version{prof}
</pre>
<p> that can take two values (say "prof" and "etud" in this example, for "teacher" and "student"). Then two new environments corresponding respectively, to the text specific to the teacher and to the student versions, are made:</p>
<pre lang="LaTeX">
\newcommand{\profVersion}[1]{\ifthenelse{\equal{\version}{prof}}{#1}{}}
\newcommand{\etudVersion}[1]{\ifthenelse{\equal{\version}{etud}}{#1}{}}
</pre>
<p> The first command <code>\profVersion{truc}
tests if the variable \version
is equal to "prof" and if so it prints truc
. If \version
is not equal to "prof", nothing is printed. The second command does the same with the value "etud". Changing the definition of the variable \version
in the header of the file yields to change the PDF file obtained while compiling the LaTeX document;
\includecomment{prof}
\excludecomment{etud}
\begin{prof}
teacher's version
\end{prof}
\begin{etud}
students' version
\end{etud}
Solution to combine both needs
\version
is first defined, like when the ifthenelse package is used:
\def\version{1}
\if
command testing the value of \version
:
\if \version1
\emph{This is the teacher's version...}
\fi