# Tikz in RMarkdown

I have a diagram rendered using tikz and would like to include it at multiple places throughout my document. I have tried to define a command to insert this into the tikz environment, however I'm not very familiar with LaTeX and it doesn't seem to be working.

\newcommand\ThreeStateDiagram{}
\def\ThreeStateDiagram{
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\begin{tikzpicture}[
sharp corners=2pt,
inner sep=7pt,
node distance=3cm,
>=latex]
\tikzstyle{my node}=[draw,minimum height=1cm,minimum width=2cm]
\node[my node] (A){A};
\node[my node,right of=A](C){C};
\node[my node] at ($(A)!0.5!(C)-(0pt,1.5cm)$) (B) {B};
\draw[->] (A) -- (B);
\draw[->] (A) -- (C);
\draw[->] (B) -- (C);
\end{tikzpicture}
}


An added bit of difficulty is that I'm writing my document in RMarkdown and using the built-in tikz engine to render it. Here is the code in RMarkdown:

{tikz, ThreeStateDiagram, fig.cap="Layout Diagram", fig.align="center"}
\ThreeStateDiagram;



And I can then use \@ref(tab:ThreeStateDiagram) to cross-reference it (because I'm using {bookdown}).

For clarity, the below works fine (in LaTeX, HTML and Word formats), however I don't want to have to insert this (with different captions) repeatedly:

{tikz, ThreeStateDiagram, fig.cap="Layout of the MSM used in the motivating model", fig.align="center"}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\begin{tikzpicture}[
sharp corners=2pt,
inner sep=7pt,
node distance=3cm,
>=latex]
\tikzstyle{my node}=[draw,minimum height=1cm,minimum width=2cm]
\node[my node] (A){A};
\node[my node,right of=A](C){C};
\node[my node] at ($(A)!0.5!(C)-(0pt,1.5cm)$) (B) {B};
\draw[->] (A) -- (B);
\draw[->] (A) -- (C);
\draw[->] (B) -- (C);
\end{tikzpicture}


• "It doesn't seem to be working"... well, maybe, but what does it complain about? Loading tikZ libraries has to take place ONCE, in the document header. That may be it. Furthermore, it's strange to see you define your command twice, first using LaTeX's \newcommand with no content and then switching back to TeX's mor basic \def. – Christoph Frings Dec 29 '20 at 14:33
• Hi and welcome to TeX.SX. As @ChristophFrings said you seem to have a couple of problems with your document. This is only by looking at a couple of code snippets. It would make it far easier for us to figure out all the problems if you give us a small document that recreates the problem starting with \documentclass{ and ending with \end{document} – Elad Den Dec 29 '20 at 14:54

knitr's tikz engine renders the figure into a pdf file and then includes it via

\begin{figure}
...
\includegraphics{[path-to-pdf-figure]}
...
\end{figure}


which you can check yourself if your specify keep_tex: true in your YAML header output section and examine the resulting *.tex file (_book/_main.tex by default).

If you have named the respective chunk, e.g. as ThreeStateDiagram then the figure is named ThreeStateDiagram-1.pdf

If you are using the bookdown package to manage and render your document, then the figure is stored in _bookdown_files/_main_files/figure_latex/ThreeStateDiagram-1.pdf

If the respective chunk is the nth unnamed chunk, then the figure's filename is unnamed-chunk-[n]-1.pdf (replace n with the actual value) and will be residing in the same directory as mentioned above.

In case your documentclass is beamer or you specified output: bookdown::beamer_presentation2 in your _output.yml, then the path is _bookdown_files/_main_files/figure_beamer/foo-1.pdf

Hence you can reuse the figure in subsequent code in a similar fashion.