I\'m trying to reference a table using the bookdown package. In the documentation for tables, the author only shows how to create tables using knitr::kable
.
I solved this with the following:
```{r table2 , echo=FALSE, results='asis'}
cat(' Table: \\label{tab:table2}Example
| 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} shows...
I am joining the discussion a bit late, but I just wanted to share a working MWE (based on the earlier answers):
```{r , echo=FALSE, results='asis'}
cat(' Table: (\\#tab:mwe) Example
| 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) shows...
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|