Referencing figures with bookdown
Asked Answered
P

2

6

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?

Phyllis answered 20/1, 2017 at 15:10 Comment(5)
When in doubt, update.packages(ask = FALSE) (it is 2017 now and you still have packages from 2014? :). Your RStudio version is also a little low but I don't think that matters in this case.Guidepost
I know. This is due to the fact that, at my office, I do not have internet access from within R. Thus, it is kind of tedious to receive package updates. I hoped to get a specific hint but I guess I have to compile a list of outdated packages to be update for the responsible colleage. Still, thank you!Phyllis
My best guess is that the knitr version is too low.Guidepost
I managed to update all relevant packages. Now the pandoc version is too low for creating numbered figures. Simple documents work. Do I recall correctly that pandoc ships with RStudio?Phyllis
Yes, if you have the latest version of RStudio, it should have a built-in version of Pandoc, which is 1.17.2. You can verify by rmarkdown::pandoc_version().Guidepost
U
6

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):
Unman answered 12/3, 2019 at 16:17 Comment(1)
wow apparently underscores count as special characters. Thanks!Unclothe
P
0

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.

Pudgy answered 10/4, 2018 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.