Put figure directly into Knitr document (without s

2019-02-18 07:14发布

问题:

I am creating a document called test.Rnw in RStudio, with a MWE as follows:

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

After I run knit("test.Rnw"), I get a test.tex file as well as a folder called "figure" with the diamonds image in it ("diamondData-1.pdf").

After that, I run pdflatex test.text to get a test.pdf file.

I have a two-part question:

1) The text of my figure caption ("Figure 1: The diamond dataset has variables depth and price.") appears gray instead of black (like the text in the rest of the document). Is there a way to correct this?

2) Is there a method to generate the diamond figure so that it is automatically embedded in the document? When I run pdflatex to get the test.pdf file, I afterward must delete the folder/diamondData-1.pdf file. It would be nice to not have to have the folder/diamondData-1.pdf file. If not, is there a method/option so that when I run pdflatex, after the test.pdf file is created, then the folder/diamondData-1.pdf file is automatically deleted?

回答1:

To change the color of the caption text can be done within LaTeX by setting \color within the \setcaptionfont environment to black (Line 8 of code.)

With your example:

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

EDIT: Have changed the global options for the figure directory and have removed the line where the figure directory was specifically moved to the trash based on comments.