TikZ in R Markdown with HTML output

If I include TikZ code in an R Markdown document that has a PDF output (like below), everything is fine.
If change the output format (say HTML or ioslides_presentation), the drawing does not appear.

Once of the beauty of R Markdown is "one input many possible outputs". Is there a way to fix that problem?

---
title: "Hello World"
author: "Me"
date: "February 24, 2020"
output:
  pdf_document: default
header-includes: 
  - \usepackage{tikz}
  - \usepackage{pgfplots}
  - \usepackage{pgf,tikz,pgfplots}
  - \pgfplotsset{compat=1.15}
  - \usepackage{mathrsfs}
  - \usetikzlibrary{arrows}
  - \usetikzlibrary{patterns}
---

## TikZ picture
- Here is a TikZ picutre

\definecolor{zzffzz}{rgb}{0.6,1,0.6}
\definecolor{ffcctt}{rgb}{1,0.8,0.2}
\definecolor{yqyqdz}{rgb}{0.5019607843137255,0.5019607843137255,0.8509803921568627}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
\clip(-4.505289256198347,-6.316528925619829) rectangle (21.02198347107439,13.083471074380155);
\draw [line width=2pt,pattern color=yqyqdz,fill=yqyqdz,fill opacity=0.25] (2,0) circle (3cm);
\draw [line width=2pt,pattern color=ffcctt,fill=ffcctt,fill opacity=0.25] (6,0) circle (3cm);
\draw [line width=2pt,pattern color=zzffzz,fill=zzffzz,fill opacity=0.25] (4,3.46415) circle (3cm);
\end{tikzpicture}

I did some googleing and turned up a cran package called exam that offers the function tex2image, I'm thinking the SVG option might suite, seems like it requires a system utility called pdf2svg to work.

tex2image
Description
Transformation of LaTeX code into an image by compiling to PDF and then transforming to PNG
(by default) via ImageMagick’s convert command or to SVG via pdf2svg.
CRAN - Package exams

1 Like

If you include raw latex as you did inside the Rmd document, it will only be useful to create pdf. This raw latex code won't be able to generate HTML code for an HTML output.That is why currently you can't render html file. PDF only.

However, knitr includes a tikz engine that you could use no matter the output format. Here is an example to produce HTML

---
title: "Hello World"
author: "Me"
date: "February 24, 2020"
output: html_document
---

## TikZ picture

Here is a TikZ picture

```{tikz, fig.cap = "Funky tikz", fig.ext = 'png'}
\usetikzlibrary{arrows}
\usetikzlibrary{patterns}
\definecolor{zzffzz}{rgb}{0.6,1,0.6}
\definecolor{ffcctt}{rgb}{1,0.8,0.2}
\definecolor{yqyqdz}{rgb}{0.5019607843137255,0.5019607843137255,0.8509803921568627}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
\clip(-4.505289256198347,-6.316528925619829) rectangle (21.02198347107439,13.083471074380155);
\draw [line width=2pt,pattern color=yqyqdz,fill=yqyqdz,fill opacity=0.25] (2,0) circle (3cm);
\draw [line width=2pt,pattern color=ffcctt,fill=ffcctt,fill opacity=0.25] (6,0) circle (3cm);
\draw [line width=2pt,pattern color=zzffzz,fill=zzffzz,fill opacity=0.25] (4,3.46415) circle (3cm);
\end{tikzpicture}
```

See also knitr example

This knitr engine seems the way to go if you want to reach multiple format using tikz.

hope it helps

3 Likes

Thanks you Christophe, super useful, it works.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.