3

I want to generate electric circuit picture using circuitikz packages and r-exams in R markdown. I already include circuitikz package in header-includes, it's successful when using knit

But when executing exams2pdf , here is the error i get,,

 LaTeX Error: Environment tikzpicture undefined.

Executing exam2moodle or exam2html get no error, but it did not include the circuit image

Any alternative solution?

hamkanen
  • 31
  • 2
  • Did the answer below solve your problem? If so, please accept it but clicking the check mark below the note counter. If not, please refine the question so that we ca resolve it. – Achim Zeileis Sep 03 '20 at 18:57

1 Answers1

0

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:

  1. 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.

  2. 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.

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49