如何在 R/exams 练习中包含 TikZ 图像?

2023-11-29

R/exams 包支持使用其功能包含 TikZ 图形include_tikz(),例如,如图所示自动机 and logic练习随包装一起提供。

得到后include_tikz()进行这些练习(参见:为什么 TikZ 图形是用 exams2pdf 渲染的,而不是用 exams2moodle 渲染的?)我尝试创建自己的练习。然而,到目前为止这还不起作用。我的代码是:

```{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")
```

在其他练习中,我包含了在中创建的该图像的源代码TikZ,但在 R 块之外(来自\begin{tikzpicture} to \end{tikzpicture})以便它可以与exams2pdf() (截屏)。但我无法让它工作exams2moodle().


本质上你的代码是有效的,我不需要做很多修改。我做了什么:

  • 省略在初始 R 代码块开头设置不必要的选项。
  • 省略常规 LaTeX 命令末尾不必要的分号graf01之前{tikzpicture}.
  • 添加呼叫至match_exams_device()这样 TikZ 创建的图形类型可以与 R/Markdown 代码块创建的图形格式相同(PNG、PDF、SVG)。
  • 改善include_tikz()使用前一项的图形类型并包含固定的参数width.

随后我做的最大的改变就是修改了Question部分,以便它是关于单个图形并添加相应的Meta-information部分。这样做的练习是独立的,并且文件的完整源代码cardealer.Rmd包含在本文的末尾。有了这个,以下三个命令都可以工作:

  • exams2pdf("cardealer.Rmd")它将 TikZ 图形渲染为 PDF 格式。
  • exams2html("cardealer.Rmd")它将 TikZ 图形渲染为 PNG 格式。
  • exams2html("cardealer.Rmd", svg = TRUE)它将 TikZ 图形渲染为 SVG 格式。

鉴于此exams2html()作品包含在exams2moodle()也会起作用,因为这在内部也会生成相同类型的 HTML 代码。在所有版本中,输出看起来都是这样的:

HTML output of exams2html("cardealer.Rmd") rendered in a browser.

的源代码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
  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 R/exams 练习中包含 TikZ 图像? 的相关文章

随机推荐