2

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?

  • Why don't you create a R markdown file that you knit directly to pdf ? – Gwenaël Gouérou Sep 26 '15 at 19:57
  • Which platform are you using: mac, pc, linux, etc? –  Sep 26 '15 at 21:10
  • @GwenaëlGouérou: Thank you for the comment. I am pretty unfamiliar with the differences, and did not realize that the image could be directly knitted to the pdf file if I converted the script from .Rnw to .Rmd. I have read that .Rmd is less flexible than .Rnw, and would be worried that some of the other functions might be lost? I am taking over from someone else's project, and that person created the script in .Rnw, so I wonder if it was for a reason (like it could not be done with .Rmd). I also wonder how difficult it would be to convert from .Rnw to .Rmd to test it out? –  Sep 26 '15 at 22:20
  • @JimM. Thank you. I am using Mac. However, I am working on wrapping this up into an R package (that generates output .pdf files with summary statistics). So, it could be used by people with different platforms. –  Sep 26 '15 at 22:21
  • No matter how you delete the files, this must happen after the PDF file has been compiled. That's why Jim M.'s answer doesn't work. Have you considered compiling in a temporary directory and copying the PDF afterwards? You could also try [HelpersMG::clean.knitr](http://finzi.psych.upenn.edu/library/HelpersMG/html/clean.knitr.html). – CL. Sep 27 '15 at 09:03

1 Answers1

2

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.

  • Thank you for the input, but I worry this might be an unsafe solution :(. I accidentally set "fig_directory" to my Desktop pathway, and it deleted everything on my Desktop (they are not in "Trash" either, they appear to be completely gone?). I am trying to put this in an R package, so I don't think I would be able to have a rm - r function because it would almost be like malware to remove files from user's computers. It seemed like a good solution to me as well, but I unfortunately will probably have to find another solution... –  Sep 27 '15 at 01:28
  • I am wondering why this answer received thumbs-up - especially if that means I am doing something wrong? Because even if I try to run this after removing everything from my Desktop (so that it does not delete files I want to keep, and only deletes files in the figure folder), I still run across an error "/Desktop/fig ure/diamondData-1.png' not found." I think it deletes figures too soon. Without those figure files, when I run pdflatex on the .tex file, the .pdf file does not have the figures in it, since they were deleted immediately after knitting. Or am I doing something wrong? Thanks again. –  Sep 27 '15 at 02:24
  • 1
    This won't work because the figure directory is purged before the PDF has been compiled. You should call `system()` outside of the `Rnw` file, which comes with the problem of passing `fig_directory` to `knitr`. Moreover I think a global chunk option for `fig.path` would be appropriate. – CL. Sep 27 '15 at 09:07
  • @JosephHudson: Good points. I have made changes to the code following your suggestions and those of user2706569. –  Sep 27 '15 at 18:24
  • Hello both of you, thanks for your help. It seems this is a better solution than earlier. So, it basically creates a temporary folder and stores the figures in there? How often does this temporary folder get deleted? –  Sep 27 '15 at 20:30
  • I am selecting this answer as correct, because it answers what I wrote in my original post. However, I did not say this next part in my original post: I am planning on developing this into an R package (where people generate summary reports from calling this .Rnw file. I still wonder if this approach would pass CRAN if it is creating folders on the user's computer? I vaguely remember reading that CRAN would not allow such features, and am currently searching again for that, but wondered if anyone here would know? –  Sep 27 '15 at 20:31
  • @JosephHudson: The temporary folder will be deleted at the start of a new R session. –  Sep 27 '15 at 20:31
  • If so, would it be in my best interest to try to convert this from .Rnw to .Rmd, as was suggested in an earlier comment? I worry that could be meticulous and difficult - especially because the .Rnw file I am working on uses lots of margins and is meticulous with the positioning and font of the output file. –  Sep 27 '15 at 20:35
  • If your output is a pdf, and you would like to have greater control over the output, I would stick to .Rnw with LaTeX output as an intermediary. –  Sep 27 '15 at 20:44