Question

I am trying to use XeLaTeX, knitr and tikz - and failing with a ghostscript error. I have tried many different approaches and the situation is as follows:

  1. XeLaTeX with knitr: works
  2. PDFLaTeX with knitr: works
  3. PDFLaTeX with knitr and tikz: works
  4. XeLaTeX with knitr and tikz: fails

The simple document I made is here http://paste.lisp.org/+31GJ , the main part is this:

<<xetex-tikz, eval=FALSE, echo=FALSE>>=
options(tikzDefaultEngine='xetex')
@  
A plot: 
<<test, echo=FALSE,dev='tikz'>>=
plot(10,10) 
@

the error message is in an annotation there as well, but I'll paste it here:

GPL Ghostscript 9.14: Unrecoverable error, exit code 1

** WARNING ** Filtering file via command -->rungs -q -dNOPAUSE -dBATCH -dEPSCrop -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dAutoFilterGrayImages=false -dGrayImageFilter=/FlateEncode -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -sOutputFile='/tmp/xdvipdfmx.45a08580e905757a9f5e6fc456cb9f8b' '/dev/null' -c quit<-- failed.
** WARNING ** Image format conversion for "/dev/null" failed...
** WARNING ** Image width=0.0!
** WARNING ** Image height=0.0!
** ERROR ** pdf_ref_obj(): passed invalid object.

To avoid any issues with older packages (RHEL5 has an older version of texlive, for example), I locally installed the following:

TeXlive 2013:

$ type xelatex
xelatex is hashed (/usr/local/texlive/2013/bin/x86_64-linux/xelatex
$ xelatex -version
XeTeX 3.1415926-2.5-0.9999.3-2013060708 (TeX Live 2013)
kpathsea version 6.1.1

GNU R

$ type R
R is /usr/local/bin/R
$ R --version
R version 3.0.3 (2014-03-06) -- "Warm Puppy"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-unknown-linux-gnu (64-bit)

Ghostscript

$ type ghostscript
ghostscript is /usr/local/bin/ghostscript
$ ghostscript --version
9.14

I set R_LATEXCMD to xelatex (before that pdflatex was being used, which meant that knit would fail since I use XeTeX packages) and that's about the only global variable or additional setting I made; paths seem to be correct, e.g.:

$ kpsewhich tikz
/usr/local/texlive/2013/texmf-dist/tex/plain/pgf/frontendlayer/tikz.tex

On R I updated the packages and installed knitr and tikzDevice (latest versions, via install.package).

Any pointers would be appreciated; the XeLaTeX+knitr+tikz combination doesn't strike as particularly exotic: tikz is recommended in the knitr graphics manual and is needed to use the same fonts as the main document, and XeLaTeX is a very common choice of engine - and I need the unicode support it provides in order to use Charis SIL, IPA characters, etc.

Was it helpful?

Solution

I just tested the CRAN versions of knitr and tikzDevice, with R 3.0.3 and TeXLive 2013 (basically the same environment as yours except that I use Ubuntu). It seems to be a very simple problem: you used eval=FALSE on the first chunk, so that chunk was not evaluated, and options(tikzDefaultEngine = 'xetex') was basically ignored. Everything works after I change eval=FALSE to TRUE (actually it is the default value so you can leave it out).

\documentclass{article}
\usepackage{xltxtra} %used this to avoid pdflatex being used
\usepackage{tikz}
%\setmainfont{Charis SIL} %depends on xlxtra
\begin{document}
\title{Testing}
\maketitle

<<xetex-tikz, eval=TRUE, include=FALSE>>=
options(tikzDefaultEngine='xetex')
@

A plot:
<<test, echo=FALSE, dev='tikz'>>=
plot(10,10)
@
\end{document}

tikzDevice with XeTeX and knitr

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top