7

我在使 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

此问题已在此处得到处理,但提出的解决方案对我不起作用。任何想法?

4

2 回答 2

1

也许与您的简单示例中未包含的另一个包发生冲突?对我来说,由于 packagexcolortikz. 我xcolor之前打过电话Tikz,没问题,但是一旦我设置dev="tikz"了块选项,我就得到了和你一样的错误。一旦我删除\usepackage{xcolor}一切工作。

于 2014-02-11T12:07:15.837 回答
1

我在 R 代码中添加

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

资源

于 2016-03-24T00:28:22.863 回答