4

Reposting this from tex.stackexchange, since the tikzDevice tag seems fairly inactive there. I hope nobody minds...

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 https://tex.stackexchange.com/questions/408390/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 -*-
\documentclass[12pt,twoside,headings=big,open=right]{scrreprt}
\usepackage{scrhack,graphicx,subcaption,nomencl,setspace,xltxtra,fontspec,microtype,csquotes,tocbibind,xspace,floatrow,tikz,textcomp,rotating,xpatch,amssymb,amsfonts,xcolor,xunicode}
\defaultfontfeatures{Ligatures=TeX}
\graphicspath{ {images/} }
\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,bindingoffset=6mm]{geometry}
\usepackage[british]{babel}

EDIT: My sessionInfo():

R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.3

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] DescTools_0.99.23  multcompView_0.1-7 multcomp_1.4-8     TH.data_1.0-8      MASS_7.3-48       
 [6] survival_2.41-3    mvtnorm_1.0-7      ggpubr_0.1.6       magrittr_1.5       forcats_0.2.0     
[11] stringr_1.2.0      dplyr_0.7.4        purrr_0.2.4        readr_1.1.1        tidyr_0.8.0       
[16] tibble_1.4.2       ggplot2_2.2.1.9000 tidyverse_1.2.1    tikzDevice_0.10-5  knitr_1.19        

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15      lubridate_1.7.1   lattice_0.20-35   zoo_1.8-1         assertthat_0.2.0 
 [6] digest_0.6.15     psych_1.7.8       R6_2.2.2          cellranger_1.1.0  plyr_1.8.4       
[11] httr_1.3.1        pillar_1.1.0      rlang_0.1.6.9003  lazyeval_0.2.1    curl_3.1         
[16] readxl_1.0.0      rstudioapi_0.7    Matrix_1.2-12     devtools_1.13.4   splines_3.4.3    
[21] foreign_0.8-69    munsell_0.4.3     tinytex_0.3.1     broom_0.4.3       compiler_3.4.3   
[26] modelr_0.1.1      pkgconfig_2.0.1   mnormt_1.5-5      manipulate_1.0.1  expm_0.999-2     
[31] codetools_0.2-15  crayon_1.3.4      withr_2.1.1.9000  grid_3.4.3        nlme_3.1-131     
[36] jsonlite_1.5      gtable_0.2.0      git2r_0.21.0      scales_0.5.0.9000 cli_1.0.0        
[41] stringi_1.1.6     reshape2_1.4.3    bindrcpp_0.2      xml2_1.2.0        boot_1.3-20      
[46] sandwich_2.4-0    tools_3.4.3       glue_1.2.0        hms_0.4.1         parallel_3.4.3   
[51] yaml_2.1.16       colorspace_1.3-2  filehash_2.4-1    rvest_0.3.2       memoise_1.1.0    
[56] bindr_0.1         haven_1.1.1      
beanstalker
  • 105
  • 1
  • 10
  • 1
    I had this problem also and could never really solve it, but this makes it better: 1. Try to cache your plots, so they are only rebuild, when something changes (but you have to be careful with that) 2. Set `external = FALSE` in you `opts_chunk$set`-option. I can't explain why but this worked for me once.... – kath Feb 04 '18 at 18:56
  • Hi @kath , thanks for those suggestions. I’ll try switching external = FALSE. I’ve had trouble with errors from caching before, but I’ll give it another try. – beanstalker Feb 04 '18 at 19:16

0 Answers0