Using tikz in html_document with RMarkdown

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

And (sort of in the same direction):

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

I read a lot of responses to messages (like TikZ in R Markdown or How can I use TikZ for self-creation (SVG)? ) That ask similar questions, but I'm still not quite sure, therefore, 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 would like to have html_document + magic that automatically converts tikz graphics into something that html can understand. Any suggestions?

+5
source share
1 answer

I just found that you can add tikz code to a piece of Rmarkdown code when changing 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 picture is pixelated and the scale is not respected. One way to solve this problem is to change the extension of the output of the picture (in pdf format), as well as the width and height with the parameters of the fragment (for example, equal 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 this is an easy way to use tikz code to output html.

+4
source

Source: https://habr.com/ru/post/1261263/


All Articles