When I was LaTeX user in the past, I like TikZ and tikzDevice a lot. After switching to R Markdown, however, I gradually forgot about them. Today I spent some time figuring out how to make TikZ and tikzDevice work for R Markdown. Here is some simple code:

---

title: "Using tikzDevice"

output:

pdf_document:

latex_engine: xelatex

keep_tex: yes

includes:

in_header: setup.tex

---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = FALSE)

```

```{r, fig.cap="Without tikzDevice"}

x<- rnorm(10)

y<-x +rnorm(5,sd=0.25)

model <- lm(y~x)

rsq<- summary(model)$r.squared

rsq<- signif(rsq,4)

plot(x, y, main='Hello \\LaTeX!')

abline(model,col='red')

mtext(paste("Linear model:$R^{2}=",

rsq, "$"), line=0.5)

legend('bottomright',legend=paste("$y = ",round(coef(model)[2],3),'x +',round(coef(model)[1],3),'$',sep =''), bty ='n')

```

```{r, echo=FALSE}

library(tikzDevice)

```

```{r, echo=FALSE, message=FALSE, results='hide'}

tikz('latexEx.tex',width=7,height=4.5)

x<- rnorm(10)

y<-x +rnorm(5,sd=0.25)

model <- lm(y~x)

rsq<- summary(model)$r.squared

rsq<- signif(rsq,4)

plot(x, y, main='Hello \\LaTeX!')

abline(model,col='red')

mtext(paste("Linear model:$R^{2}=",

rsq, "$"), line=0.5)

legend('bottomright',legend=paste("$y = ",round(coef(model)[2],3),'x +',round(coef(model)[1],3),'$',sep =''), bty ='n')

dev.off()

```

```{r, fig.cap="With tikzDevice", message=FALSE, engine='tikz'}

\input{latexEx.tex}