Using texPreview with tikz

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.

# add tikz support to tex_opts
use_lib <- "\\usetikzlibrary{arrows,shapes,decorations,automata,backgrounds,arrows.meta,positioning}"

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

  dir.create(objpath,recursive = TRUE)

  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)


tex_preview(obj = tikz_code$pk_model.tex,stem = 'tikz-1')
plot of chunk unnamed-chunk-4

plot of chunk unnamed-chunk-4

Click to view TeX


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')
plot of chunk unnamed-chunk-6

plot of chunk unnamed-chunk-6

unlink(dirname(objpath), force = TRUE, recursive = TRUE)

Click to view TeX

% Author: Rasmus Pank Roulund
\documentclass[varwidth, border={ 10 5 10 5 }]{standalone}

:Title: Credit rationing

An illustration inspired by a figure in Stiglitz, J.E. and Greenwald, B. (2003). `Towards a New Paradigm in Monetary Economics`__.

.. __:

%Define standard arrow tip
%Define style for different line styles
help lines/.style={dashed, thick},
important line/.style={thick},
connection/.style={thick, dotted},
    % Axis
    \coordinate (y) at (0,5);
    \coordinate (x) at (5,0);
    \draw[<->] (y) node[above] {$r$} -- (0,0) --  (x) node[right]
    % A grid can be useful when defining coordinates
    % \draw[step=1mm, gray, thin] (0,0) grid (5,5); 
    % \draw[step=5mm, black] (0,0) grid (5,5); 

    % Let us define some coordinates
    coordinate (start) at (0,4)
    coordinate (c1) at +(5,3)
    coordinate (c2) at +(5,1.75)
    coordinate (slut) at (2.7,.5)
    coordinate (top) at (4.2,2);

    \draw[important line] (start) .. controls (c1) and (c2) .. (slut);
    % Help coordinates for drawing the curve
    % \filldraw [black] 
    % (start) circle (2pt)
    % (c1) circle (2pt)
    % (c2) circle (2pt)
    % (slut) circle (2pt)
    \filldraw [black] 
     (top) circle (2pt) node[above right, black] {$Q$};

     % We start the second graph
       % Axis
      \coordinate (y2) at (0,5);
      \coordinate (x2) at (5,0);
      \draw[axis] (y2) node[above] {$r$} -- (0,0) --  (x2) node[right] {$L$};
      % Define some coodinates
     coordinate (sstart) at (1,.5) 
     coordinate (sslut) at (4, 4.5)
     coordinate (dstart) at (4,.5)
     coordinate (dslut) at (1,4.5)
% Intersection 1
     coordinate (int) at  (intersection cs:
       first line={(sstart)--(sslut)},
       second line={(dstart)--(dslut)})
% Intersection 2
    coordinate (int2) at  (intersection cs:
       first line={(top)--($(10,\y1)$)},
       second line={(dstart)--(dslut)})
% Intersection 3
    coordinate (int3) at  (intersection cs:
       first line={(top)--($(10,\y1)$)},
       second line={(sstart)--(sslut)});
% Draw the lines
     \draw[important line] (sstart) -- (sslut) node[above right] {$S$}
       (dstart) -- (dslut)  node[above left] {$D$};
     \draw[connection] let \p1=(int2), \p2=(int3) in 
     (int2)--(\x1,0) node[below] {$\mathit{L_D}$}
     (int3)--(\x2,0) node[below] {$\mathit{L_S}$};
%Finally, connect the two graphs
     \draw[connection] let \p1=(top), \p2=(x2) in (0,\y1) node[left]
     {$r^*$} -- (\x2, \y1);