繁体   English   中英

使用knitr在LaTeX文档中使用tikzDevice的R图

[英]R plot using tikzDevice in LaTeX document with knitr

我在制作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

此问题已得到处理, 在这里 ,但提出的解决方案并没有为我工作。 任何想法?

也许与你未包含在你的简单例子中的另一个包冲突? 对我来说,由于包xcolortikz之间的一些冲突,我收到此错误消息。 我打电话xcolorTikz ,没有问题,但只要我设置dev="tikz"在块选项我得到了同样的错误,你。 一旦我删除\\usepackage{xcolor}一切正常。

我在R代码中添加了

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

资源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM