我在 RStudio 中将 knitr 与 XeLaTeX 一起使用。我使用 block 缓存,这样我就不必在每次编译文档时重新运行某些代码。这个最小的示例表明,如果加载 fontspec
包,缓存似乎会被破坏。
\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
block 进行更改,并且代码第二次运行,它将失败。例如,编译为 PDF 一次后,将 test_block
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
包,则不会发生此错误。
我的问题是:这是一个错误,还是我的代码有问题?
我通过 RStudio (0.97.246) 在 R(R 正在开发(不稳定)(2012-11-10 r61101))上使用 knit(1.1)和 tikzDevice(0.6.3)(通过 RStudio Server 通过浏览器访问)它本身运行在 Ubuntu (12.04.2 LTS) 上。我的 LaTeX2e 日期为 <2009/09/24>。
最佳答案
不要将 options(tikzDefaultEngine="xetex")
放入缓存 block 中,因为它有无法缓存的副作用,因此第二次编译文档时,此选项将被跳过了。阅读 Important Notes in the cache page 部分在 knitr
网站上。
请注意,您也不需要library(tikzDevice)
;当您设置 dev='tikz'
时,将自动加载此包。
在大多数情况下,您应该缓存绘图 block ,因为创建 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}
关于r - 通过 RStudio 使用 XeLaTeX fontspec 包时,knitr 缓存失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15205263/