如何将以下 Latex 代码包含到 Rmarkdown 中?
\usetikzlibrary{shapes.geometric,arrows}
\tikzstyle{decision} = [ diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep-0pt]
\tikzstyle{block} = [ rectangle, draw, fill=blue!20, text width=5em, text badly centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [ draw, -latex']
\tikzstyle{terminator} = [ draw, ellipse, fill=red!20, node distance=3cm, minimum height=2em]
\begin{tikzpicture}[node distance=2cm, auto]
\node [terminator] (puc) {Power-Up Reset};
\node [block, below of=puc] (wdt) {Stop Watchdog};
\node [block, below of=wdt] (port) {Setup Port Pins};
\node [block, below of=port] (loop) {Loop Forever};
\path [line] (puc) -- (wdt);
\path [line] (wdt) -- (port);
\path [line] (port) -- (loop);
\path [line] (loop) -- (loop);
\end{tikzpicture}
答案1
在文件中tikz.Rmd
---
title: "Title"
author: "Me"
header-includes:
- \usepackage{tikz}
- \usetikzlibrary{shapes.geometric,arrows}
output:
pdf_document
---
\begin{tikzpicture}[node distance=2cm, auto]
\tikzstyle{decision}=[diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep-0pt]
\tikzstyle{block}=[rectangle, draw, fill=blue!20, text width=5em, text badly centered, rounded corners, minimum height=4em]
\tikzstyle{line}=[draw, -latex']
\tikzstyle{terminator} = [ draw, ellipse, fill=red!20, node distance=3cm, minimum height=2em]
\node [terminator] (puc) {Power-Up Reset};
\node [block, below of=puc] (wdt) {Stop Watchdog};
\node [block, below of=wdt] (port) {Setup Port Pins};
\node [block, below of=port] (loop) {Loop Forever};
\path [line] (puc) -- (wdt);
\path [line] (wdt) -- (port);
\path [line] (port) -- (loop);
\path [line] (loop) -- (loop);
\end{tikzpicture}
答案2
该示例的一个微小变化,但在 knitr 块中使用 TikZ 代码:
---
title: "Latex and TikZ in knitr chunks"
author: "Me"
header-includes:
- \usepackage{xspace}
- \usepackage{tikz}
- \usetikzlibrary{shapes.geometric,arrows}
- \def\TikZ{Ti\emph{k}Z\ }
output:
pdf_document
---
## TikZ directly on the notebook
\begin{tikzpicture}[node distance=2cm, auto]
\tikzstyle{decision}=[diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep-0pt]
\tikzstyle{block}=[rectangle, draw, fill=blue!20, text width=5em, text badly centered, rounded corners, minimum height=4em]
\tikzstyle{line}=[draw, -latex']
\tikzstyle{terminator} = [ draw, ellipse, fill=red!20, node distance=3cm, minimum height=2em]
\node [terminator] (puc) {Power-Up Reset};
\node [block, below of=puc] (wdt) {Stop Watchdog};
\node [block, below of=wdt] (port) {Setup Port Pins};
\node [block, below of=port] (loop) {Loop Forever};
\path [line] (puc) -- (wdt);
\path [line] (wdt) -- (port);
\path [line] (port) -- (loop);
\path [line] (loop) -- (loop);
\end{tikzpicture}
## \TikZ code inside a knitr chunk
The \LaTeX code can also be included in a knitr chunk with small changes:
* The \TikZ engine has to be specified
* The \TikZ libraries have to be called from within the chunk
```{tikz, echo=FALSE, fig.align="center"}
\usetikzlibrary{shapes.geometric,arrows}
\begin{tikzpicture}[node distance=2cm, auto]
\tikzstyle{decision} = [diamond,
draw,
fill=blue!20,
text width=4.5em,
text badly centered,
node distance=3cm,
inner sep-0pt]
\tikzstyle{block} = [rectangle,
draw,
fill=blue!20,
text width=5em,
text badly centered,
rounded corners,
minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{terminator} = [draw,
ellipse,
fill=red!20,
node distance=3cm,
minimum height=2em]
\node [terminator] (puc) {Power-Up Reset};
\node [block, below of=puc] (wdt) {Stop Watchdog};
\node [block, below of=wdt] (port) {Setup Port Pins};
\node [block, below of=port] (loop) {Loop Forever};
\path [line] (puc) -- (wdt);
\path [line] (wdt) -- (port);
\path [line] (port) -- (loop);
\path [line] (loop) -- (loop);
\end{tikzpicture}
```