The R/exams package supports including TikZ graphics using its function include_tikz()
, e.g., as shown in the automaton and logic exercises shipped with the package.
After getting include_tikz()
to work for these exercises (see: Why are TikZ graphics rendered with exams2pdf but not with exams2moodle?) I tried to create my own exercise. However, so far this does not work yet. My code is:
```{r datos1, echo = FALSE, results = "hide"}
options(tinytex.verbose = TRUE)
options(scipen=999)
knitr::opts_chunk$set(echo = TRUE)
##opts_chunk$set(fig.cap="")
library(exams)
library(magick)
questions<-solutions<-explanations<-NULL
questions[1]<-paste("Graph 1.")
solutions[1]<-TRUE
explanations[1]<-"."
questions[2]<-paste("Graph 2.")
solutions[2]<-FALSE
explanations[2]<-"."
questions[3]<-paste("Graph 3.")
solutions[3]<-FALSE
explanations[3]<-"."
questions[4]<-paste("Graph 4.")
solutions[4]<-FALSE
explanations[4]<-"."
orden<-sample(1:4)
questions<-questions[orden]
solutions<-solutions[orden]
explanations<-explanations[orden]
graf01<-'
\\definecolor{uuuuuu}{rgb}{0.26,0.26,0.26};
\\definecolor{zzttqq}{rgb}{0.6,0.2,0};
\\definecolor{ududff}{rgb}{0.30,0.30,1};
\\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
\\clip(-0.1,-0.1) rectangle (5,5);
\\fill[line width=2pt,color=zzttqq,fill=zzttqq,fill opacity=0.10] (0,0) -- (4.68,-0.02) --
(4.7,4.66) -- (0.02,4.68) -- cycle;
\\draw (0.99,3.80) node[anchor=north west] {\\textbf{49}};
\\draw (3.44,3.80) node[anchor=north west] {\\textbf{12}};
\\draw (2.22,1.74) node[anchor=north west] {\\textbf{14}};
\\draw (2.22,3.00) node[anchor=north west] {\\textbf{5}};
\\draw (2.22,3.96) node[anchor=north west] {\\textbf{6}};
\\draw (1.40,2.53) node[anchor=north west] {\\textbf{8}};
\\draw (3.00,2.53) node[anchor=north west] {\\textbf{9}};
\\draw (3.85,1.39) node[anchor=north west] {\\textbf{}};
\\draw (0.35,4.57) node[anchor=north west] {\\textbf{DH}};
\\draw (3.97,4.57) node[anchor=north west] {\\textbf{CA}};
\\draw (2.31,0.72) node[anchor=north west] {\\textbf{R}};
\\draw [line width=2pt] (1.76,3.08) circle (1.3cm);
\\draw [line width=2pt] (3.05,3.13) circle (1.3cm);
\\draw [line width=2pt] (2.45,1.98) circle (1.3cm);
\\draw [line width=2pt,color=zzttqq] (0,0)-- (4.68,-0.02);
\\draw [line width=2pt,color=zzttqq] (4.68,-0.02)-- (4.7,4.66);
\\draw [line width=2pt,color=zzttqq] (4.7,4.66)-- (0.02,4.68);
\\draw [line width=2pt,color=zzttqq] (0.02,4.68)-- (0,0);
\\begin{scriptsize}
\\draw [fill=ududff] (0,0) circle (2.5pt);
\\draw [fill=ududff] (4.68,-0.02) circle (2.5pt);
\\draw [fill=uuuuuu] (4.7,4.66) circle (2.5pt);
\\draw [fill=uuuuuu] (0.02,4.68) circle (2.5pt);
\\end{scriptsize}
\\end{tikzpicture}
'
```
Question
========
An auto dealer sold 53 cars in a month; 33 of them had hydraulic steering (DH); 21 were
automatic gearboxes (CA); and 38 had radius (R); 4 had hydraulic steering, automatic gears
and radio; 3 had power steering and automatic shifts, but they had no radio; 13 had
automatic gears and radio, but did not have hydraulics and, 15 had hydraulics and radio, but
did not have automatic gears.
The graph that represents the information of the sale in the agency is
Graph 1:
```{r, echo = FALSE, results = "asis"}
include_tikz(graf01, name = "graf01", markup = "markdown",library = c("arrows"))
```
```{r questionlist, echo = FALSE, results = "asis"}
library(exams)
answerlist(questions, markup = "markdown")
```
In other exercises I have included the source code of this image created in TikZ
, but outside the R chunks (from \begin{tikzpicture}
to \end{tikzpicture}
) so that it works with exams2pdf()
(screenshot). But I could not get it to work with exams2moodle()
.
Essentially your code works and I did not have to make many modifications. What I did:
graf01
before the {tikzpicture}
.match_exams_device()
so that the type of graphic created by TikZ can be in the same format as the graphics created by the R/Markdown code chunks (PNG vs. PDF vs. SVG).include_tikz()
arguments to use the graphic type from the previous item and to include a fixed width
.Subsequently, the biggest change I made was to modify the Question
section so that it is about a single graphic and add a corresponding Meta-information
section. Doing so the exercise is self-contained and the complete source code for the file cardealer.Rmd
is included at the end of this post. With this the following three commands all work:
exams2pdf("cardealer.Rmd")
which renders the TikZ graphic into PDF format.exams2html("cardealer.Rmd")
which renders the TikZ graphic into PNG format.exams2html("cardealer.Rmd", svg = TRUE)
which renders the TikZ graphic into SVG format.And given that exams2html()
works the inclusion in exams2moodle()
will also work because this internally also generates the same kind of HTML code. In all versions the output looks something like this:
Source code of cardealer.Rmd
:
```{r tikzsource, echo = FALSE, results = "hide"} typ <- match_exams_device() graf01 <- ' \\definecolor{uuuuuu}{rgb}{0.26,0.26,0.26} \\definecolor{zzttqq}{rgb}{0.6,0.2,0} \\definecolor{ududff}{rgb}{0.30,0.30,1} \\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm] \\clip(-0.1,-0.1) rectangle (5,5); \\fill[line width=2pt,color=zzttqq,fill=zzttqq,fill opacity=0.10] (0,0) -- (4.68,-0.02) -- (4.7,4.66) -- (0.02,4.68) -- cycle; \\draw (0.99,3.80) node[anchor=north west] {\\textbf{49}}; \\draw (3.44,3.80) node[anchor=north west] {\\textbf{12}}; \\draw (2.22,1.74) node[anchor=north west] {\\textbf{14}}; \\draw (2.22,3.00) node[anchor=north west] {\\textbf{5}}; \\draw (2.22,3.96) node[anchor=north west] {\\textbf{6}}; \\draw (1.40,2.53) node[anchor=north west] {\\textbf{8}}; \\draw (3.00,2.53) node[anchor=north west] {\\textbf{9}}; \\draw (3.85,1.39) node[anchor=north west] {\\textbf{}}; \\draw (0.35,4.57) node[anchor=north west] {\\textbf{DH}}; \\draw (3.97,4.57) node[anchor=north west] {\\textbf{CA}}; \\draw (2.31,0.72) node[anchor=north west] {\\textbf{R}}; \\draw [line width=2pt] (1.76,3.08) circle (1.3cm); \\draw [line width=2pt] (3.05,3.13) circle (1.3cm); \\draw [line width=2pt] (2.45,1.98) circle (1.3cm); \\draw [line width=2pt,color=zzttqq] (0,0)-- (4.68,-0.02); \\draw [line width=2pt,color=zzttqq] (4.68,-0.02)-- (4.7,4.66); \\draw [line width=2pt,color=zzttqq] (4.7,4.66)-- (0.02,4.68); \\draw [line width=2pt,color=zzttqq] (0.02,4.68)-- (0,0); \\begin{scriptsize} \\draw [fill=ududff] (0,0) circle (2.5pt); \\draw [fill=ududff] (4.68,-0.02) circle (2.5pt); \\draw [fill=uuuuuu] (4.7,4.66) circle (2.5pt); \\draw [fill=uuuuuu] (0.02,4.68) circle (2.5pt); \\end{scriptsize} \\end{tikzpicture} ' ``` Question ======== An auto dealer sold 103 cars in a month that had at least one of the following features: hydraulic steering (DH), automatic gearbox (CA), radio (R). The following graph displays how many cars had which combination of features: ```{r, echo = FALSE, results = "asis"} include_tikz(graf01, name = "graf", format = typ, markup = "markdown", library = "arrows", width = "5cm") ``` How many cars had all three features? Meta-information ================ exname: car dealer extype: num exsolution: 5