One of the promises of the bookdown package is functions for automatic numbering of figure/table captions, and cross-referencing figures/tables/sections.
This is claimed to be enabled for single-Rmd bookdown, if we use bookdown::html_document2
in the YAML: https://bookdown.org/yihui/bookdown/a-single-document.html
Looking at the raw rmarkdown for that chapter of the bookdown manual, I've tried to follow the instructions to make a single Rmd file that can use those cross-referencing functions. But I can't get it to render as expected, with cross-references.
Here's a simple example Rmd:
---
output: bookdown::html_document2
---
Here's the cross-ref: \@ref(fig:cars-fig)
Here's the figure:
```{r cars-fig, fig.cap = 'Here is a plot about cars'}
plot(cars)
```
Here's the output I get when I click on the knit button in RStudio, the cross-references are not quite working as expected:
How can I get the cross-referencing to show figure numbers and be a live link to the figure? And how can I get the caption to be formatted as expected?
Here's my session info:
> devtools::session_info()
Session info -------------------------------------------------------
setting value
version R version 3.3.0 (2016-05-03)
system x86_64, mingw32
ui RStudio (0.99.846)
language (EN)
collate English_Australia.1252
tz Australia/Sydney
date 2016-07-02
Packages -----------------------------------------------------------
package * version date source
bookdown 0.0.74 2016-07-01 Github (rstudio/bookdown@8333bbb)
devtools 1.12.0 2016-06-24 CRAN (R 3.3.1)
digest 0.6.9 2016-01-08 CRAN (R 3.3.0)
htmltools 0.3.5 2016-03-21 CRAN (R 3.2.4)
httpuv 1.3.3 2015-08-04 CRAN (R 3.2.3)
memoise 1.0.0 2016-01-29 CRAN (R 3.2.5)
mime 0.4 2015-09-03 CRAN (R 3.2.2)
miniUI 0.1.1 2016-01-15 CRAN (R 3.2.4)
R6 2.1.2 2016-01-26 CRAN (R 3.2.5)
Rcpp 0.12.5 2016-05-14 CRAN (R 3.3.0)
rmarkdown 0.9.6 2016-05-01 CRAN (R 3.3.0)
rsconnect 0.4.3 2016-05-02 CRAN (R 3.3.0)
shiny 0.13.2 2016-03-28 CRAN (R 3.2.4)
withr 1.0.2 2016-06-20 CRAN (R 3.3.0)
xtable 1.8-2 2016-02-05 CRAN (R 3.2.5)
yaml 2.1.13 2014-06-12 CRAN (R 3.2.2)
Cross References In raw markdown, you would for example write a cross-reference to a figure like this: \@ref(fig:label) . Cross-references are largely the same in visual mode, but you don't need the leading \ (which in raw markdown is used to escape the @ character).
Authoring Books and Technical Documents with R Markdown In fact, you can also reference sections using the same syntax \@ref(label) , where label is the section ID. By default, Pandoc will generate an ID for all section headers, e.g., a section # Hello World will have an ID hello-world .
Open the R Markdown file index. Rmd , and click the button Build Book on the Build tab of RStudio. This will compile the book and display the HTML version within the RStudio Viewer, which looks like Figure 12.1.
This was a bug in the bookdown package and I have fixed it. The figure number should be generated correctly if you use bookdown >= 0.0.75.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With