Referencing a 'hand-made' table using bookdown package
Asked Answered
R

3

22

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

Runoff answered 19/4, 2016 at 15:54 Comment(1)
please add the code you are usingGomez
Z
20

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|
Zip answered 21/4, 2016 at 4:30 Comment(4)
I found that I had to remove the initial "Table:" and enclose my caption in an html tag for this to work: <caption> (\#tab:lable) My caption </caption>Sadi
For me neither options worked. It prints the (\#tab:label) as if there were text and not code.Eggshell
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-xieSpectatress
One note on a variation: When my reference label contained an underscore the reference didn't render.Ethnology
F
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...

Ferrari answered 13/9, 2019 at 13:19 Comment(3)
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?Spectatress
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.Ferrari
Perhaps because I am on linux?Spectatress
S
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.

Spectatress answered 7/10, 2019 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.