Include figures from TikZ code in an exercise after
compiling it with tex2image
.
include_tikz(tikz, name = "tikzpicture", format = NULL,
library = NULL, width = NULL, markup = "tex", …)
character vector with the TikZ code.
character. Name prefix of the graphics file to be produced.
character. Names of TikZ libraries required
for compiling the tikz
code (if any).
character. The width with which the resulting graphic should be included in LaTeX.
character. Which type of markup should be written?
Can be "tex"
(default), "markdown"
, or "none"
.
arguments passed to tex2image
.
A character vector is returned. This contains just the name of the graphics
file produced (i.e., name.format
) except for format = "tex"
where the TikZ code is returned. For markup = "tex"
or "markdown"
the value is returned invisibly.
The function include_tikz
takes a character vector with tikz
code, if necessary adds a {tikzpicture}
environment, renders it
into a graphics file via tex2image
, and returns
LaTeX or Markdown code that embeds the graphics into an exercise.
If format = "tex"
and markup = "tex"
the TikZ code
is included directly (possibly adding library
and {tikzpicture}
,
if necessary).
# NOT RUN {
## TikZ code for a logic gate
tz <- "
\\node[left,draw, logic gate inputs=nn, xor gate US,fill=white,,scale=2.5] (G1) at (0,0) {};
\\draw (G1.output) --++ (0.5,0) node[right] (y) {$y$};
\\draw (G1.input 1) --++ (-0.5,0) node[left] {$a$};
\\draw (G1.input 2) --++ (-0.5,0) node[left] {$b$};
"
## switch to temporary directory
wd <- getwd()
td <- tempfile()
dir.create(td)
setwd(td)
dir()
## produce PDF figure and produce includegraphics statement
include_tikz(tz, name = "logicgate", format = "pdf",
library = c("arrows", "shapes.gates.logic.US", "calc"),
width = "2.5cm")
dir()
## alternatively produce just the complete TikZ code
include_tikz(tz, name = "logicgate", format = "tex",
library = c("arrows", "shapes.gates.logic.US", "calc"))
## switch back to original working directory
setwd(wd)
# }
Run the code above in your browser using DataLab