Why your attempts did not work
Additional LaTeX packages can not be included within the exercises themselves (e.g., YAML headers are ignored) for two reasons:
The exercises are processed with LaTeX only for certain interfaces (notably exams2pdf
and exams2nops
) but not for most others. In the latter case it is not straightforward to add further LaTeX packages.
Even when using exams2pdf
or exams2nops
exercises are only fragments of the LaTeX document that is eventually processed. Hence, there is relatively little that the individual exercise may control about the LaTeX document as a whole. (Hence, the YAML headers are not used for the indivual exercises.)
See also related discussions in:
How to include TikZ graphics
When including TikZ graphics in R/exams exercises it is important to know what interfaces this exercise should work with. Some extra effort is needed to make an exercise feasible for all exams2xyz
interfaces. Worked examples are included in:
Both of these rely on the include_tikz()
function from the package. This preprocesses the TikZ code via LaTeX and produces a suitable graphics format (PDF, PNG, or SVG) depending on the graphics format that is used for R graphics chunks as well via match_exams_device()
.
Alternatively, you could also pass through the LaTeX code and assure that the necessary TikZ packages are loaded in the LaTeX template. In exams2pdf
there is a template =
argument and it is probably best to start out with copying and modifying one of the templates shipped with the package. See Section 3 in vignette("exams", package = "exams")
and exams_skeleton(write = "exams2pdf", ...)
should get you started with this. In exams2nops
you can pass the necessary LaTeX code through the header =
argument which is then inserted in the template that exams2nops
uses.