Referencing figures with bookdown

2.9k Views Asked by At

For some reason I have problems with cross-referencing figures in a bookdown document. Here is a minimal example:

---
output: 
  bookdown::html_document2: 
    fig_caption: yes
---

Reference example: \@ref(fig:plot-cars):

```{r plot-cars, fig.cap = "A car plot"}
plot(cars)
```

When I knit this, R Markdown throws the following warning ...

Warning message:
The label(s) fig:plot-cars not found

... and this result: enter image description here

This problem seems to be very similar to this one, which was solved by an update of bookdown. As far as I understand my session_info() this should not be a problem any more:

Session info -----------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.1 (2016-06-21)
 system   i386, mingw32               
 ui       RStudio (0.99.489)          
 language (EN)                        
 collate  German_Germany.1252         
 tz       Europe/Berlin               
 date     2017-01-20                  

Packages ---------------------------------------------------------------------------------------------
 package   * version date       source        
 bookdown    0.3     2016-11-28 CRAN (R 3.3.2)
 devtools    1.9.1   2015-09-11 CRAN (R 3.2.2)
 digest      0.6.8   2014-12-31 CRAN (R 3.2.2)
 htmltools   0.3.5   2016-03-21 CRAN (R 3.2.4)
 knitr       1.11    2015-08-14 CRAN (R 3.2.2)
 memoise     0.2.1   2014-04-22 CRAN (R 3.2.2)
 Rcpp        0.12.8  2016-11-17 CRAN (R 3.3.2)
 rmarkdown   0.8.1   2015-10-10 CRAN (R 3.2.2)
 yaml        2.1.13  2014-06-12 CRAN (R 3.2.2)

Any ideas?

2

There are 2 best solutions below

0
Balthasar On

If anyone experiences this problem even upon updating all packages, check if your references are correctly specified. ?? also appear in the pdf output when referring to a chunk with a table using \@ref(fig:missings) rather than \@ref(tab:missings). This solved it for me.

1
user236456 On

The name of the code chunk cannot have special characters in it. I recommend using camelCase just to be sure. I.e. this should work:

---
output: 
  bookdown::html_document2: 
    fig_caption: yes
---

```{r plotCars, fig.cap = "A car plot"}
plot(cars)
```

Reference example: \@ref(fig:plotCars):