Tikz inside 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}
`` `   

On the R side with R Markdown, I see some solutions. I can't tell for latex commands.

You can use some advanced trick of knitr to reuse a chunk content in several place.

  1. First version is the easiest: You can reuse a code chunk by its label.
---
title: "Untitled"
output: 
  pdf_document: default
---

# First

```{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}
```   

# Second

```{tikz, ThreeStateDiagram, fig.cap="Layout of the MSM used in the motivating model (Second version)", fig.align="center"}
```   

See

  1. You can also have the content in an external file and use code= chunk option to insert the content of the file as the chunk option. Here is an example, where I use the cat engine to write the file, but you can create the file manually, and the trick will still work. This is useful to use existing script in a chunk for example.
---
title: "Untitled"
output: 
  pdf_document: default
---

```{cat, engine.opts = list(file = "ThreeStateDiagram.txt"), include = FALSE}
\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}
```

# First

```{tikz, ThreeStateDiagram, fig.cap="Layout of the MSM used in the motivating model", fig.align="center", code = xfun::read_utf8("ThreeStateDiagram.txt")}
```   

# Second

```{tikz, ThreeStateDiagram2, fig.cap="Layout of the MSM used in the motivating model (Second version)", fig.align="center", code = xfun::read_utf8("ThreeStateDiagram.txt")}
```   

See

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.