[Tex/LaTex] Rmarkdown – fig.cap will not add caption in figure

floatsknitrr

I want to add a caption to a figure in Rmarkdown, and it won't let me. The header of the file looks like this

---
title: "..."
author: "..."
date: '...'
output: 
  pdf_document: 
    fig_caption: true
---

and I display the image using

```{r, echo = FALSE, fig.width = 4, fig.cap = "my caption. \\label{my-label}"}
knitr::include_graphics('figure1')
knitr::include_graphics('figure2')
```

because I want to display two figures side-by-side. The figures are showed perfectly, but there's no way the caption will appear. Maybe I have to use two captions since I'm using two figures? I don't know how to do that either.

Best Answer

You can include the figures directly using LaTeX:


title: 'A title'
author: 'An author'
date: 'A date'
output: 
  pdf_document: 
    fig_caption: true
---

\begin{figure}[htb]
  \centering
  \includegraphics[width = 2cm]{example-image-a}\quad
  \includegraphics[width = 2cm]{example-image-b}
  \caption{Figure caption}
\end{figure}