2013-03-04 100 views
1

我在RStudio中使用带有XeLaTeX的knitr。我使用块缓存,这样我不必在每次编译文档时都重新运行某些代码。这个最小的例子表明,如果fontspec软件包被加载,缓存似乎被破坏。通过RStudio使用XeLaTeX fontspec包时,knitr缓存失败

\documentclass{article} 
\usepackage{fontspec} % Appears to somehow conflict with caching. 

\begin{document} 

<<pre_load, cache=TRUE>>= 
library(tikzDevice) 
options(tikzDefaultEngine="xetex") 
@ 

\section{Test} 
<<test_block, dev='tikz', dependson='pre_load'>>= 
plot(1:10,main='Test') 
@ 

\end{document} 

本文档首次编译为PDF时,它将起作用,因为不使用缓存。但是,如果对test_block块进行更改,并且代码再次运行,则它将失败。例如,编译成PDF一次后,改变test_block大块:

<<test_block, dev='tikz', dependson='pre_load'>>= 
plot(1:10,main='Test Modified') 
@ 

现在,编译为PDF失败,出现以下错误:

! 
******************************************** 
* XeTeX is required to compile this document. 
* Sorry! 
********************************************. 
\RequireXeTeX ...********************************} 
                \endgroup \fi 
l.18 \RequireXeTeX 

此错误表示options(tikzDefaultEngine="xetex")一直没有组。有趣的是,如果fontspec软件包未加载,则不会发生此错误。

我的问题是:这是一个错误,还是有我的代码有问题?我正在使用tikzDevice(0.6.3)在R(R开发中(不稳定)(2012-11-10 r61101))通过RStudio(0.97.246)使用knitr(1.1)(通过RStudio Server通过浏览器访问) )本身运行在Ubuntu(12.04.2 LTS)上。我的LaTeX2e的日期是< 2009/09/24>。

回答

2

请勿将options(tikzDefaultEngine="xetex")放入缓存块中,因为它具有无法缓存的副作用,所以在第二次编译文档时,此选项将被跳过。请阅读knitr网站上的Important Notes in the cache page部分。

请注意,您不需要library(tikzDevice);当您设置dev='tikz'时,此包将自动加载。

在大多数情况下,您应该缓存plot chunk,因为它创建TikZ图形的速度很慢。

\documentclass{article} 
\usepackage{fontspec} % Appears to somehow conflict with caching. 

\begin{document} 

<<pre_load>>= 
options(tikzDefaultEngine="xetex") 
@ 

\section{Test} 
<<test_block, dev='tikz'>>= 
plot(1:10,main='Test') 
@ 

\end{document}