I am having a problem making ggplot2, tikzDevice, and knitr working together. I am working with RStudio, and trying to include some R graphics in a Latex document. I used a very simple example:
我在制作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}
But no pdf is output, and I get the following error message:
但是没有输出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
This problem has been treated here, but the solution proposed does not work for me. Any idea?
此问题已在此处理,但建议的解决方案对我不起作用。任何想法?
2 个解决方案
#1
1
Perhaps a * with another package you haven't included in your simple example? For me, I was getting this error message because of some * between package xcolor
and tikz
. I was calling xcolor
before Tikz
, no problem, but as soon as I set dev="tikz"
in the chunk options I got the same error as you. As soon as I removed \usepackage{xcolor}
everything worked.
也许你的简单例子中没有包含另一个包的冲突?对我来说,由于包xcolor和tikz之间的一些冲突,我收到此错误消息。我在Tikz之前调用xcolor,没问题,但是一旦我在chunk选项中设置dev =“tikz”,我就会得到与你相同的错误。一旦我删除\ usepackage {xcolor}一切正常。
#2
1
I added in the R code
我在R代码中添加了
options(tikzMetricPackages = c("\\usepackage[utf8{inputenc}",
"\\usepackage[T1]{fontenc}", "\\usetikzlibrary{calc}", "\\usepackage{amssymb}"))
#1
1
Perhaps a * with another package you haven't included in your simple example? For me, I was getting this error message because of some * between package xcolor
and tikz
. I was calling xcolor
before Tikz
, no problem, but as soon as I set dev="tikz"
in the chunk options I got the same error as you. As soon as I removed \usepackage{xcolor}
everything worked.
也许你的简单例子中没有包含另一个包的冲突?对我来说,由于包xcolor和tikz之间的一些冲突,我收到此错误消息。我在Tikz之前调用xcolor,没问题,但是一旦我在chunk选项中设置dev =“tikz”,我就会得到与你相同的错误。一旦我删除\ usepackage {xcolor}一切正常。
#2
1
I added in the R code
我在R代码中添加了
options(tikzMetricPackages = c("\\usepackage[utf8{inputenc}",
"\\usepackage[T1]{fontenc}", "\\usetikzlibrary{calc}", "\\usepackage{amssymb}"))