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