'R/exams: Duplicated images in PDF files

When using several R/exams exercises with TikZ plots, I have no problems generating exams in HTML format or for Canvas. However, exactly the same exercises when rendered to PDF show the same plots in different questions. As a simple example, you can use the same exercise twice, e.g., for the automaton exercise shipped with the package:

library("exams")
set.seed(0)
exams2pdf(c("automaton.Rnw", "automaton.Rnw"))

The automaton diagram in the exercise should have a double circle for "A" (the "accepting state" described in the text). However, it has the double circle for "B" (as in the first exercise).



Solution 1:[1]

It turns out that this was caused by generating graphics files with the same name in different exercises. Since exams 2.3-5 exams2pdf() had a workaround for resolving these problems but the workaround failed in some situations. I just fixed the problem in version 2.4-0, the current development version on R-Forge at the time of writing.

Other workarounds include using different names for the TikZ graphics files via include_tikz(..., name = ...) or including the TikZ code as LaTeX in exams2pdf. For the latter the logic.Rnw template contains a worked example that embeds the LaTeX code (rather than generating a graphics file) for exams2nops() output.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Achim Zeileis