如何在 R/exams 练习中包含 TikZ 图像?
How to include TikZ images in R/exams exercises?
R/exams 软件包支持使用其函数 include_tikz()
包含 TikZ 图形,例如,如软件包附带的 automaton and logic 练习所示。
在让 include_tikz()
完成这些练习后(参见:),我尝试创建自己的练习。但是,到目前为止,这还行不通。我的代码是:
```{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}
到 \end{tikzpicture}
),因此它可以与 exams2pdf()
(screenshot)。但我无法让它与 exams2moodle()
.
一起工作
已解决!
感谢 Achim Zeileis 孜孜不倦的陪伴。我分享更正后的代码:
graf01<-c("\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}")
那么要调用 TicZ
的代码块是:
```{r, echo = FALSE, results = "asis"}
include_tikz(graf01, name = "graf", markup = "markdown",format = typ,library =
c("arrows"),packages = "booktabs",width = "5cm")
```
format = typ
段在代码开头声明:
typ <- match_exams_device()
再次感谢阿奇姆。他的工作值得称赞。希望有一天我能在这个社区做出和你一样伟大的贡献。祝福!
基本上你的代码可以工作,我不需要做太多修改。我做了什么:
- 省略在初始 R 代码块的开头设置不必要的选项。
- 在
{tikzpicture}
. 之前的 graf01
中省略常规 LaTeX 命令末尾不必要的分号
- 添加对
match_exams_device()
的调用,以便 TikZ 创建的图形类型可以与 R/Markdown 代码块创建的图形具有相同的格式(PNG vs. PDF vs. SVG ).
- 改进
include_tikz()
参数以使用上一项中的图形类型并包含固定的 width
。
后来,我做的最大的改动是修改了Question
部分,使其成为关于单个图形的部分,并添加了相应的Meta-information
部分。这样做的练习是独立的,文件 cardealer.Rmd
的完整源代码包含在本 post 的末尾。有了这个,以下三个命令都可以工作:
exams2pdf("cardealer.Rmd")
将 TikZ 图形呈现为 PDF 格式。
exams2html("cardealer.Rmd")
将 TikZ 图形呈现为 PNG 格式。
exams2html("cardealer.Rmd", svg = TRUE)
将 TikZ 图形渲染成 SVG 格式。
鉴于 exams2html()
有效,包含在 exams2moodle()
中也将有效,因为这在内部也会生成相同类型的 HTML 代码。在所有版本中,输出看起来像这样:
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
R/exams 软件包支持使用其函数 include_tikz()
包含 TikZ 图形,例如,如软件包附带的 automaton and logic 练习所示。
在让 include_tikz()
完成这些练习后(参见:
```{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}
到 \end{tikzpicture}
),因此它可以与 exams2pdf()
(screenshot)。但我无法让它与 exams2moodle()
.
已解决!
感谢 Achim Zeileis 孜孜不倦的陪伴。我分享更正后的代码:
graf01<-c("\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}")
那么要调用 TicZ
的代码块是:
```{r, echo = FALSE, results = "asis"}
include_tikz(graf01, name = "graf", markup = "markdown",format = typ,library =
c("arrows"),packages = "booktabs",width = "5cm")
```
format = typ
段在代码开头声明:
typ <- match_exams_device()
再次感谢阿奇姆。他的工作值得称赞。希望有一天我能在这个社区做出和你一样伟大的贡献。祝福!
基本上你的代码可以工作,我不需要做太多修改。我做了什么:
- 省略在初始 R 代码块的开头设置不必要的选项。
- 在
{tikzpicture}
. 之前的 - 添加对
match_exams_device()
的调用,以便 TikZ 创建的图形类型可以与 R/Markdown 代码块创建的图形具有相同的格式(PNG vs. PDF vs. SVG ). - 改进
include_tikz()
参数以使用上一项中的图形类型并包含固定的width
。
graf01
中省略常规 LaTeX 命令末尾不必要的分号
后来,我做的最大的改动是修改了Question
部分,使其成为关于单个图形的部分,并添加了相应的Meta-information
部分。这样做的练习是独立的,文件 cardealer.Rmd
的完整源代码包含在本 post 的末尾。有了这个,以下三个命令都可以工作:
exams2pdf("cardealer.Rmd")
将 TikZ 图形呈现为 PDF 格式。exams2html("cardealer.Rmd")
将 TikZ 图形呈现为 PNG 格式。exams2html("cardealer.Rmd", svg = TRUE)
将 TikZ 图形渲染成 SVG 格式。
鉴于 exams2html()
有效,包含在 exams2moodle()
中也将有效,因为这在内部也会生成相同类型的 HTML 代码。在所有版本中,输出看起来像这样:
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