# Incredibly slow compilation with knitr and tikzDevice

I am having some compilation difficulties when using knitr and tikzDevice. Compilataion works, but I am seeing times upwards of 10 minutes, and this is only increasing as my document grows (I am writing my thesis, and at the moment it takes 10 minutes to compile one chapter, and there are many more chapters to come!).

If I set the device to something other than tikz, there's no problem. Unfortunately I need tikz so that I can use LaTeX in my R plots.

I have a main file `main.Rnw` which incorporates each chapter as a child document using e.g. `\Sexpr{knit_child('chapter3.Rnw')}`.

In the chapter files, I include plots etc from R by using e.g. `\Sexpr{knit_child('ch3-fig-nitrogenPH.Rnw')}` to insert in a figure environment.

My main file specifies the following options for knitr setup:

``````<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
library(tikzDevice)
library(tidyverse)
library(ggpubr)
library(multcomp)
library(multcompView)
library(DescTools)

plotTextSize <- 8
cbbPalette<-c("#E69F00", "#56B4E9", "#009E73", "#0072B2", "#D55E00", "#CC79A7")

opts_knit\$set(child.command = 'include')

opts_chunk\$set(dev = 'tikz', external=TRUE)
opts_chunk\$set(fig.align='center', fig.pos='!htbp', fig.width=5.5, fig.height=4, out.width="5.5in", out.height="4in")

options(tikzDefaultEngine='xetex')

options(tikzXelatexPackages = c(
getOption('tikzXelatexPackages'),
"\\usepackage{amsmath,amssymb,amsfonts}",
"\\usepackage{tikz}",
"\\usetikzlibrary{calc}",
"\\usepackage[british]{babel}",
"\\selectlanguage{british}"
))

options(tikzMetricPackages = c(
"\\usetikzlibrary{amssymb}",
"\\usetikzlibrary{calc}",
"\\usepackage{hyperref}"
))

knit_hooks\$set(document = function(x) {sub('\\usepackage[]{color}', '\\usepackage{xcolor}', x, fixed = TRUE)})
@
``````

Finally, one other thing that suggests that I've got something wrong somewhere, is that tikz is ignoring the pointsize arurmet when I try to use it e.g. `dev.args=list(pointsize=10)` has no effect. See my previous question Setting font size in tikzDevice.

If anyone can point me in the right direction to fix this, I'd be very grateful.

I will try to put together a MWE that replicates the problem.

I am using these packages:

``````% -*- program: xelatex -*-