r在乳胶文档中使用knitr的tikzdevice绘图



我遇到问题,使GGPLOT2,Tikzdevice和Knitr一起工作。我正在与Rstudio合作,并尝试在乳胶文档中包括一些R图形。我使用了一个非常简单的示例:

documentclass{article} 
begin{document} 
begin{figure} 
<<fig1,eval=TRUE,echo=FALSE,dev='tikz'>>= 
library(ggplot2)
library(tikzDevice)
qplot(displ, hwy, data = mpg, colour = factor(cyl)) 
@ 
end{figure} 
end{document}

但没有输出PDF,我会收到以下错误消息:

Error in getMetricsFromLatex(TeXMetrics) : 
TeX was unable to calculate metrics for the following string
or character:
    hwy
Common reasons for failure include:
  * The string contains a character which is special to LaTeX unless
    escaped properly, such as % or $.
  * The string makes use of LaTeX commands provided by a package and
    the tikzDevice was not told to load the package.
The contents of the LaTeX log of the aborted run have been printed above,
it may contain additional details as to why the metric calculation failed.
Calls: knit ... widthDetails.text -> grid.Call -> <Anonymous> -> getMetricsFromLatex
Execution halted

此问题在这里得到了处理,但是提出的解决方案对我不起作用。有什么想法吗?

也许与您在简单示例中未包含的另一个软件包发生冲突?对我来说,由于包xcolortikz之间发生了冲突,因此我收到了此错误消息。我在Tikz之前调用xcolor,这没问题,但是一旦我在块选项中设置dev="tikz",我就会遇到与您相同的错误。一旦我删除了usepackage{xcolor},一切都起作用。

我在r代码中添加

options(tikzMetricPackages = c("\usepackage[utf8{inputenc}",
    "\usepackage[T1]{fontenc}", "\usetikzlibrary{calc}", "\usepackage{amssymb}"))

最新更新