24

I am writing a report on Rmarkdown and knitr using RStudio, and cannot find a way to get figure captions on my document. My code for the chunk where I try to plot is like:

```{r fig1,fig.width=4,fig.height=4,echo=FALSE,message=FALSE,include=TRUE,fig.cap="Caption"}

plot(rnorm(100,2),rnorm(100,8))
```

No caption is added to the pdf output, and a look at the intermediary latex file reveals that the figure environment is not created:

\includegraphics{./Untitled_files/figure-latex/fig.pdf}

Otherwise, the pdf document is created without any problems, it just misses the captions. I have read in the knitr manual and in many other questions about the topic that the chunk option fig.cap will add the figure environment. I tried to add the fig.env=TRUE option, as well, but the results are the same. Is this an issue with rmarkdown in rstudio only or is it that in general it will not work?

6
  • How are you converting from .Rmd to .pdf?
    – Adam Liter
    Commented Sep 19, 2014 at 15:50
  • Using knitr and pdfLaTex. Knitr produces a .tex file and pdfLaTex takes over from there, I suppose.
    – Kenji
    Commented Sep 19, 2014 at 16:45
  • Calling knit() on an .Rmd file will produce .md output, though. How exactly are you going from .Rmd to a .tex file?
    – Adam Liter
    Commented Sep 19, 2014 at 16:56
  • And knit2pdf() only works on .Rnw files and .Rrst files.
    – Adam Liter
    Commented Sep 19, 2014 at 17:04
  • I use the built in functionality in RStudio. It works alright and outputs the PDF, it just fails to add a caption.
    – Kenji
    Commented Sep 21, 2014 at 11:26

1 Answer 1

27

Try adding fig_caption: yes to the YAML header of your .Rmd file. It will look something like this:

---
title: "Untitled"
author: "Nicholas"
date: "20/09/2014"
output:
  pdf_document:
    fig_caption: yes
---

There's more information here: http://rmarkdown.rstudio.com/pdf_document_format.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .