R图使用tikzDevice在LaTeX文件中使用knitr [英] R plot using tikzDevice in LaTeX document with knitr

查看:298
本文介绍了R图使用tikzDevice在LaTeX文件中使用knitr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,使ggplot2,tikzDevice和knitr一起工作。我正在使用RStudio,并试图在Latex文档中包含一些R图形。
我使用了一个非常简单的例子:

$ p $ \documentclass {article}

\\ \\ begin {document}

\ begin {figure}
<<< fig1,eval = TRUE,echo = FALSE,dev ='tikz'>>> =
库(ggplot2)
库(tikzDevice)
qplot(displ,hwy,data = mpg,color = factor(cyl))
@
\ end {figure}

\ end {document}

但是没有输出pdf, getMetricsFromLatex(TeXMetrics):
中的错误TeX无法计算以下的指标:

$ p $ 字符串
或字符:

hwy

失败的常见原因包括:
*该字符串包含一个LaTeX特殊的字符,除非
正确转义,例如%或$。
*字符串使用包提供的LaTeX命令,
没有告诉tikzDevice加载包。

中止运行的LaTeX日志的内容已在上面打印,
可能包含关于度量计算失败原因的额外详细信息。
通话:编织... widthDetails.text - > grid.Call - > <匿名> - > getMetricsFromLatex

执行暂停

这个问题已被处理此处,但提出的解决方案对我无效。任何想法?

解决方案

我在R代码中添加了

<$ p $ (tikzMetricPackages = c(\\ usepackage [utf8 {inputenc},
\\ usepackage [T1] {fontenc},\\ usetikzlibrary {calc},\\usepackage {amssymb}))

来源


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:

\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:

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?

解决方案

I added in the R code

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

source

这篇关于R图使用tikzDevice在LaTeX文件中使用knitr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆