I am trying to create a reference to DT::datatable
in a bookdown project.
The bookdown manual states that (\#tab:label)
should be placed at the beginning of the table caption. For testing, I created a new bookdown project in R-studio but replaced the contents of _output.yml
with the following html_book
configuration (I am only interested in HTML output).
bookdown::html_book:
toc: yes
theme: null
highlight: pygments
split_by: none
Then I added the following code at the bottom of 01-intro.Rmd
.
```{r irisTab}
DT::datatable(iris, caption = '(\\#tab:irisTab) Iris table')
```
See Table \@ref(tab:irisTab).
My expectation was that (\\#tab:irisTab)
would be replaced by Table 2.2
or at least 2.2
and the reference below would be 2.2
. However, this does not work. The label remains verbatim and the reference is ??
.
The closest I could get was by placing the caption text before the table.
Table (\#tab:irisTab): Iris table
```{r irisTab}
DT::datatable(iris)
```
See Table \@ref(tab:irisTab).
In that case, the reference works but the label (\#tab:irisTab)
remains verbatim in the output, i.e. it is not replaced with 2.2
as expected.
Is there any way to create a DT table that would have a caption and could be referenced?
Update 1: @mikey-harper suggested using fig.cap
. However, fig.cap
updates only the fig
counter and not the tab
counter. Thus, if you have any non-DT tables, there will be multiple tables with the same number. It might be possible to treat all tables as figures. However, that would not be a standard approach. Typically, tables and figures have separate counters.