我在制作ggplot2,tikzDevice和knitr时遇到问题。我正在使用RStudio,并尝试在Latex文档中包含一些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
此问题已得到处理here,但建议的解决方案对我不起作用。有什么想法吗?
答案 0 :(得分:1)
或许与您未在简单示例中包含的其他套餐发生冲突?对我来说,由于包xcolor
和tikz
之间存在冲突,我收到此错误消息。我在xcolor
之前调用Tikz
,没问题,但是只要我在chunk选项中设置dev="tikz"
,我就会得到与你相同的错误。一旦我删除\usepackage{xcolor}
一切正常。
答案 1 :(得分:1)
我在R代码中添加了
options(tikzMetricPackages = c("\\usepackage[utf8{inputenc}",
"\\usepackage[T1]{fontenc}", "\\usetikzlibrary{calc}", "\\usepackage{amssymb}"))