*Some* figure captions from RMarkdown not showing

knitrpandocr-markdownrstudio

Essentially, I have a Rmd document configured like this:

---
title: "Example"
author: "me"
date: "December 2014"
output:
  pdf_document:
    fig_caption: yes
    keep_tex: yes
--

Then, in the document, I use ggplot2 to show some charts, for example:

```{r myLabel, fig.cap='My Caption'}
qplot(1:10, 10:1)
```

Now, for some reason I cannot explain or investigate any deeper than this, the produced TeX does not contain the figure environment, even if I force it with fig.env='figure'. Instead, the TeX only has the includegraphics command:

\includegraphics{journal_files/figure-latex/myLabel-1.pdf}

Other figures in the same document do have the figure environment, with the caption. I.e., the TeX output "Knit PDF" should be producing is:

\begin{figure}[htbp]
\centering
\includegraphics{journal_files/figure-latex/myLabel-1.pdf}
\caption{My Citation}
\end{figure}

The R Markdown log window only shows irrelevant stuff:

label: myLabel (with options) 
List of 2
 $ fig.cap: chr "My Caption"
 $ fig.env: chr "figure"

cropping journal_files/figure-latex/myLabel-1.pdf
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.

How do I either

  • find out what exactly is going on (debug log?) once I press "Knit PDF" and get to the TeX (the "R Markdown" output window shows no errors, and even echos the [missing] captions!),
  • or, even better, has anybody had this very problem and knows what is wrong with RStudio, knitr, and pandoc?

Best Answer

Turns out, the solution is quite simple: the RMarkdown compiler does not show captions for two consecutive figures, like this:

```{r myLabel1, fig.cap='My Caption 1'}
qplot(1:10, 10:1)
```
```{r myLabel2, fig.cap='My Caption 2'}
qplot(1:10, 10:1)
```

or for a figure not separated from the text in a new paragraph like this:

```{r myLabel1, fig.cap='My Caption 1'}
qplot(1:10, 10:1)
```
As shown in Fig. 2, the inter-galactic distances are strongly correlated with the observed redshift ...

In this setting, the captions are missing and no figure environment is created in the TeX file.

Instead, between the two plots, there must be at least two spacing (newline) characters. I.e., this works nicely and both captions show:

```{r myLabel1, fig.cap='My Caption 1'}
qplot(1:10, 10:1)
```

```{r myLabel2, fig.cap='My Caption 2'}
qplot(1:10, 10:1)
```

Even though this is a feature to make it possible to include inline graphics, it would be nice if there was a warning message for figures with a fig.cap argument that does not show up.