0

I am writing a report with Knitr and am trying to use bookdown to automatically number figures and refer to them.

Here is my header :

---
title: "My title"
author: "Me"
email: "My email"
output:
  bookdown::html_document2:
    number_sections: true
    toc : true
    fig_caption : TRUE
---

```{r setup, include=FALSE}
library(bookdown)
knitr::opts_chunk$set(echo = TRUE)
setwd(dir=dirname(rstudioapi::getActiveDocumentContext()$path))
```

And my first figure :

```{r fig1,echo=F, eval=T, fig.align = 'center', fig.cap="My first figure"}
knitr::include_graphics(here::here("images", "Formule 3.png"))
```

When I knit it, I can see my figure but under it, I can see:

(#fig:fig1)My first figure

I expected:

Figure 1 : My first figure

When I remove fig1 in the code chunk, I get an automatic number, but I do not know how to refer to it, because I do not have any reference.

Could anybody explain me what I am doing wrong?

Thank you in advance.

Best regards,

Jean-Luc.

Carlos Luis Rivera
  • 3,108
  • 18
  • 45
  • I cound not reproduce your problem and I got a correct figure numbering like `Figure 1`. Would you mind adding your result of `sessionInfo()` and `rmarkdown::pandoc_version()` to your post? – Carlos Luis Rivera Aug 08 '21 at 03:00

1 Answers1

0

If knitr::include_graphics() does not work, you can use the markdown notation for figure insertion supported by pandoc ![(#fig:tag-for-figure) Caption for the figure](/path/to/the/figure.png). See also r2evans' answer for more detail (e.g. about how to resize the figure). You can cross-reference to the figure, as shown below:

<!-- figure insertion -->

![(#fig:tag-for-figure) Caption for the figure](/path/to/the/figure.png)

<!-- Reference to the figure in a text -->

Figure \@ref(fig:tag-for-figure) shows an awesome results...
Carlos Luis Rivera
  • 3,108
  • 18
  • 45
  • Thank you. In fact, the problem came from the figure reference. I put underscores, whivh seems to be the cause of the problem. With simple dash character, it works fine. It is in accordance with the example given by Carlos Luis Rivera. Thanks to him. I have another question : is there an easy way to : * add a title above the list of content * add a list of figures I would like to add it under the table of content, but I do not know how to do it. Thank you in advance for your help. Best regards. Jean-Luc. – Jean-Luc BELLIER Aug 08 '21 at 14:39
  • 1
    If my answer works fine, please show that [by up-voting it and/or accepting it](https://stackoverflow.com/help/someone-answers). Moreover, you are encouraged to start new threads to ask different questions from the original post. When you start new posts on TOC of figures, you may encounter more useful answers from users who know more on that topic. – Carlos Luis Rivera Aug 08 '21 at 15:43