2
votes

I'm using rmarkdown to output a PDF document, however adding captions to plots seems not to be working. According to the docs, we're supposed to be using fig.cap to specify figure captions to pass to Latex. This is the header of my code chunk:

```{r Plot bond index returns, include = TRUE, fig.cap = "Bond index cumulative returns"}

I made sure to include the following lines in the header of the rmarkdown document

output: pdf_document: fig_caption: true

This setup eliminates the entire chunk's output from the resulting PDF completely

1

1 Answers

4
votes

Can you provide a little more detail? I can't reproduce this error. With a barebones .Rmd that looks like:

---
output: pdf_document
---

```{r echo = FALSE, fig.cap = "Test figure caption."}
plot(pressure)
```

I get an output like:

enter image description here

Edit:

After looking at the answers to this question, the following code generates figures with text interspersed with the figures:

---
output: pdf_document
header-includes:
 \usepackage{float}
 \floatplacement{figure}{H}
---

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.pos = 'h')
```

Here is text number preceding figure 1

```{r echo = FALSE, fig.cap = "Test figure caption."}
plot(pressure)
```

Here is text following figure 1

```{r echo = FALSE, fig.cap = "Second test figure caption."}
plot(cars)
```

Here is some final text following the second figure