21

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.

```{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

Das_Geek
  • 2,775
  • 7
  • 20
  • 26
nhoeft
  • 535
  • 6
  • 17

3 Answers3

19

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|
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • 1
    I found that I had to remove the initial "Table:" and enclose my caption in an html tag for this to work: (\#tab:lable) My caption – vkehayas Mar 29 '19 at 09:54
  • 2
    For me neither options worked. It prints the (\#tab:label) as if there were text and not code. – Santiago I. Hurtado Jul 09 '19 at 15:39
  • 1
    This example doesn't work for me either. I get the table being produced in the .pdf but "Table 4: (#tab:foo) Your table caption." as the caption and "For example, you may refer to this table using @ref(tab:foo)" If I cross reference using \@ref(tab:foo). Not sure how to overcome this issue @yihui-xie – Christopher Kavazos Oct 06 '19 at 23:32
2

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...

  • Not working for me. This just prints out the table structure in a series of lines and doesn't allow me to cross reference either. What packages where you using when you ran this? – Christopher Kavazos Oct 06 '19 at 23:40
  • Hi Christopher, that's odd. There was some additional white space before the ``` in my original post (copy paste error), did you remove that? I updated my post (to remove the white space) and just tried again. Works fine for me using the out of the box default "new bookdown project" settings in R. I cannot comment on your post, but your post is not working for me. The Table prints nicely, but the referencing is not working. – Hans Henrik Sievertsen Oct 07 '19 at 05:48
  • Perhaps because I am on linux? – Christopher Kavazos Oct 08 '19 at 02:48
1

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|')
  
```

If you write text and want to reference the table, you can write in table \ref{tab:table2} the results are shown.

Martin
  • 401
  • 6
  • 15