Learn R Programming

exams (version 2.4-1)

include_tikz: Including Figures from TikZ Code in Exercises

Description

Include figures from TikZ code in an exercise after compiling it with tex2image.

Usage

include_tikz(tikz, name = "tikzpicture", format = NULL,
  library = NULL, width = NULL, markup = "tex", ...)

Value

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.

Arguments

tikz

character vector with the TikZ code.

name

character. Name prefix of the graphics file to be produced.

format

character. The graphics format requested from tex2image, e.g., "png" (default), "svg", "pdf". If set to "tex" then tex2image is not called but the tikz code is included directly.

library

character. Names of TikZ libraries required for compiling the tikz code (if any).

width

character. The width with which the resulting graphic should be included in LaTeX.

markup

character. Which type of markup should be written? Can be "tex" (default), "markdown", or "none".

...

arguments passed to tex2image.

Details

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

Examples

Run this code
## 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()

# \donttest{
## 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