TikZ in R Markdown

2019-02-02 12:58发布

R Markdown is a good tool for document authoring. It can use Latex statement in R Markdwon directory. But when I try to insert a TikZ picture, I am lost about how to add preamble \usepackage{tikz} in it, so compile will fail.

example code:

---
title: "R Markdown with tikz picture"
author: "Me"
date: "January 10, 2015"
output: beamer_presentation
---

## TikZ picture
- Here is a TikZ picutre

\begin{tikzpicture}
\draw (0,0) circle (2cm);
\end{tikzpicture}

Since actually it will generate a latex document, so it's fine to just add it in latex then compile it to pdf, but I wish I can just do it in R Markdown then click the "Knit PDF" button to generate pdf document directly.

2条回答
神经病院院长
2楼-- · 2019-02-02 13:10

In this post it is described how to use LaTeX packages in R Markdown. Not sure if the tikz-package works with R Markdown though.

查看更多
何必那么认真
3楼-- · 2019-02-02 13:26

This is a example about how to use tikz graph in R Markdown within R Studio

---
title: "Hello World"
author: "Me"
date: "February 17, 2015"
output:
  pdf_document: default
header-includes: 
  - \usepackage{tikz}
  - \usepackage{pgfplots}
---

## TikZ picture
- Here is a TikZ picutre

\begin{tikzpicture}
\draw (0,0) circle (2cm);
\end{tikzpicture}

- Here is another TikZ picutre

\begin{tikzpicture}
\begin{axis}[xmax=9,ymax=9, samples=50]
  \addplot[blue, ultra thick] (x,x*x);
  \addplot[red,  ultra thick] (x*x,x);
\end{axis}
\end{tikzpicture}

Output:

enter image description here

查看更多
登录 后发表回答