r语言 - 将图形直接放入Knitr文档中(无需在文件夹中保存文件)



我正在创建一个名为test的文档。Rnw在RStudio中,MWE如下:

documentclass[12pt,english,nohyper]{tufte-handout}
usepackage{tabularx}
usepackage{longtable}
begin{document}
<<setup, echo = FALSE>>=
library(knitr)
library(xtable)
library(ggplot2)
@
<<diamondData, echo=FALSE, fig.env = "marginfigure", out.width = "0.95\linewidth", fig.cap = "The diamond dataset has varibles depth and price.", fig.lp = "mar:">>=
print(qplot(depth,price,data=diamonds))
@
<<echo=FALSE,results='asis'>>=
myDF <- data.frame(a = rnorm(1:10), b = letters[1:10])
print(xtable(myDF, caption= 'This data frame shows ten random variables from the normal distribution and a corresponding letter', label='tab:dataFrame'), floating = FALSE, tabular.environment = "longtable", include.rownames=FALSE)
@
Figure ref{mar:diamondData} shows the diamonds data set, with the variables price and depth. Table ref{tab:dataFrame} shows letters a through j corresponding to a random variable from a normal distribution.
end{document}

在我运行knit("test.Rnw")之后,我得到一个test.tex文件以及一个名为"figure"的文件夹,其中包含钻石图像("diamondData-1.pdf")。

之后,我运行pdflatex测试。文本获取test.pdf文件。

我有一个两部分的问题:

1)我的图标题的文本("图1:钻石数据集有变量depth和price.")显示为灰色而不是黑色(就像文档其余部分的文本一样)。有办法纠正吗?

2)是否有方法生成菱形图形,使其自动嵌入到文档中?当我运行pdflatex获取test.pdf文件时,随后必须删除文件夹/diamondData-1.pdf文件。如果不需要文件夹/diamondData-1.pdf文件就好了。如果没有,是否有一种方法/选项,当我运行pdflatex时,在创建test.pdf文件之后,文件夹/diamondData-1.pdf文件将被自动删除?

要更改标题文本的颜色,可以在LaTeX中通过将setcaptionfont环境中的color设置为黑色来完成(代码的第8行)

用你的例子:

documentclass[nohyper]{tufte-handout}
usepackage{tabularx} 
usepackage{longtable}
setcaptionfont{% changes caption font characteristics
  normalfontfootnotesize
  color{black}% <-- set color here
}
begin{document}
<<setup, echo=FALSE>>=
library(knitr)
library(xtable)
library(ggplot2)
# Specify directory for figure output in a temporary directory
temppath <- tempdir()
opts_chunk$set(fig.path = temppath)
@
<<diamondData, echo=FALSE, fig.env = "marginfigure", out.width="0.95\linewidth",
fig.cap = "The diamond dataset has varibles depth and price.",fig.lp="mar:">>=
print(qplot(depth,price,data=diamonds))
@

<<echo=FALSE,results='asis'>>=
myDF <- data.frame(a = rnorm(1:10), b = letters[1:10])
print(xtable(myDF, caption= 'This data frame shows ten random variables from the
distribution and a corresponding letter', label='tab:dataFrame'),
 floating = FALSE, tabular.environment = "longtable", include.rownames=FALSE)
@
Figure ref{mar:diamondData} shows the diamonds data set, with the
variables price and depth.Table ref{tab:dataFrame} shows letters a through j
corresponding to a random variable from a normal distribution.
end{document}

EDIT:已经更改了图形目录的全局选项,并删除了基于注释将图形目录具体移动到垃圾箱的行。

最新更新