Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using tikz in html_document using RMarkdown

Question: Is there a way to use tikz notation within an Rmarkdown document without having to compile the document to a pdf?

And (sort of in the same direction):

Question: What is the best way to add mutiple tikz graphics in an RMarkdown document that I want to compile to html (while keeping the flexibilty to compile the file to pdf or word in a later stage as well)?

I've read many answers to posts (like TikZ in R Markdown or How can I use TikZ to make standalone (SVG) graphics?) that ask similar questions but I am still not completely sure, hence the questions.

I basically want to run this piece of code

---
output: pdf_document
header-includes: 
  - \usepackage{tikz}
---

## TikZ picture

Some picture

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

Instead of pdf_document I'd like to have html_document + some magic that automatically converts the tikz graphic to something that can be understood by html. Any suggestions?

like image 536
Manuel R Avatar asked Dec 14 '16 12:12

Manuel R


1 Answers

I've just discovered that you can add tikz code in the code chunk of Rmarkdown when you modify the engine option :

---
output: html_document
---

Some picture

```{r,engine='tikz'}
\begin{tikzpicture}
 \draw (0,0) circle (2cm);
\end{tikzpicture}
```

The problem is that the figure is pixelized and the scale is not respected. One way to solve this may be to change the extension of the figure output (to pdf) and the width and the height with the chunk options (e.g. equals to 3 inches) :

```{r,engine='tikz',fig.ext='pdf',fig.width=3}
\begin{tikzpicture}
 \draw (0,0) circle (2cm);
\end{tikzpicture}
```

I'm sure there are better solutions but it's an easy way to use tikz code for an html output.

like image 93
AQLT Avatar answered Nov 20 '22 21:11

AQLT