将图形直接放入 Knitr 文档中(不将其文件保存在文件夹中)

Put figure directly into Knitr document (without saving file of it in folder)

我正在 RStudio 中创建一个名为 test.Rnw 的文档,其 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}

在我 运行 编织 ("test.Rnw") 之后,我得到一个 test.tex 文件以及一个名为 "figure" 的文件夹,里面有钻石图像 ("diamondData-1.pdf").

之后,我 运行 pdflatex test.text 得到一个 test.pdf 文件。

我有一个two-part问题:

1) 我的图形标题 ("Figure 1: The diamond dataset has variables depth and price.") 的文本显示为灰色而不是黑色(与文档其余部分中的文本一样)。有办法纠正这个问题吗?

2) 有没有生成菱形图自动嵌入到文档中的方法?当我 运行 pdflatex 得到 test.pdf 文件时,我必须删除 folder/diamondData-1.pdf 文件。如果不需要 folder/diamondData-1.pdf 文件就好了。如果没有,有没有method/option这样我运行pdflatex的时候,创建test.pdf文件后,然后自动删除folder/diamondData-1.pdf文件?

要更改字幕文本的颜色,可以在 LaTeX 中完成,方法是将 \setcaptionfont 环境中的 \color 设置为黑色(代码第 8 行。)

以你为例:

\documentclass[nohyper]{tufte-handout}
\usepackage{tabularx} 
\usepackage{longtable}

\setcaptionfont{% changes caption font characteristics
  \normalfont\footnotesize
  \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}

编辑:更改了图形目录的全局选项,并删除了根据评论专门将图形目录移至垃圾箱的行。