[Tex/LaTex] Knitr and rmarkdown will not add figure captions to the output

captionsknitrmarkdownrrstudio

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?

Best Answer

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