Using texPreview with tikz

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

texPreview can be used for any TeX input beyond tabular environments, such as tikz. In the following examples we input a tikzpicture and an entire TeX file that contains a tikz environment.

library(texPreview)
# add tikz support to tex_opts
use_lib <- "\\usetikzlibrary{arrows,shapes,snakes,automata,backgrounds,arrows.meta,positioning}"

objpath <- file.path(getwd(),"tikz_files/figure-html")

if(!dir.exists(objpath))
  dir.create(objpath,recursive = TRUE)

tex_opts$set(
  fileDir    = objpath, # path to save output
  returnType  = 'html',
  usrPackages = build_usepackage(pkg = 'tikz',uselibrary = use_lib)
)
tikz_examples <- list.files(system.file('examples/tikz',package = 'texPreview'),
                          pattern = 'tex$',full.names = TRUE)
tikz_code <- lapply(tikz_examples,
                    function(x) paste0(readLines(x),collapse = '\n'))
names(tikz_code) <- basename(tikz_examples)

tikzpicture

tex_preview(obj = tikz_code$pk_model.tex,stem = 'tikz-1')
details::details(tikz_code$pd_model.tex,summary = 'Click to view TeX',lang = 'tex')

File

For a full TeX document use the tex_lines argument instead of the obj. This input bypasses the internal document template that is used for texPreview and renders the contents of the file directly.

tex_preview(tex_lines = tikz_code$credit_rationing.tex,stem = 'tikz-2')
details::details(tikz_code$credit_rationing.tex,summary = 'Click to view TeX',lang = 'tex')


Try the texPreview package in your browser

Any scripts or data that you put into this service are public.

texPreview documentation built on March 31, 2022, 9:05 a.m.