Nikolas Hoeft Nikolas Hoeft - 12 days ago 7
R Question

Referencing a 'hand-made' table using bookdown package

I am trying to reference a table using the

bookdown
package. In the documentation the author only shows how to create tables using
knitr::kable
.

```{r table1}
knitr::kable(
head(iris, 20), caption = 'Here is a nice table!',
booktabs = TRUE
)
```


Table
\@ref(tab:table1)
is here.

Using
knitr::kable
works just fine. The caption of the table is displayed and I can reference the table. I would like to do the same with a classic hand-made markdown table but obviously the code below fails. What can I do to get a similar result as with the code above?

```{r table2, echo=FALSE, results='asis'}
cat('| Sepal.Length| Sepal.Width| Petal.Length|
|------------:|-----------:|------------:|
| 5.1| 3.5| 1.4|
| 4.9| 3.0| 1.4|
| 4.7| 3.2| 1.3|
| 4.6| 3.1| 1.5|')
```


Table
\@ref(tab:table2)
is here.

This picture shows the output of this code when it is knitted.

This

Answer

I did mention it in the documentation, but perhaps it is not clear enough. I said you need the label of the form (\#tab:...). For example, you may refer to this table using \@ref(tab:foo).

Table: (\#tab:foo) Your table caption.

| Sepal.Length| Sepal.Width| Petal.Length|
|------------:|-----------:|------------:|
|          5.1|         3.5|          1.4|
|          4.9|         3.0|          1.4|
|          4.7|         3.2|          1.3|
|          4.6|         3.1|          1.5|
Comments