5

My question is how to reference tables in markdown format like the following because the answer here doesn't work Referencing a 'hand-made' table using bookdown package I tried

---
output: html_document
---

you may refer to this table using \@ref(tab:foo)

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

+-----------------------+-----------------------+-----------------------+
| Auteur                | Protocole             | Résultats             |
+=======================+=======================+=======================+
| (Jiayi 2011)          | Analyse formantique   | Diphtongaison de [e  |
+-----------------------+-----------------------+-----------------------+

and it didn't work.

it gives "Table : (#tab:foo) Your table caption." as the caption and "you may refer to this table using @ref(tab:foo)" If I cross reference using @ref(tab:foo).

Is it possible also to have automatic numbering ?

xiaoou wang
  • 891
  • 10
  • 13
  • Does this answer your question? [knitr/rmarkdown/Latex: How to cross-reference figures and tables?](https://stackoverflow.com/questions/38861041/knitr-rmarkdown-latex-how-to-cross-reference-figures-and-tables) – joshpk Dec 10 '19 at 18:08
  • @joshpk nope and this answer doesn't work either [link](https://stackoverflow.com/questions/36723514/referencing-a-hand-made-table-using-bookdown-package#comment102892470_36759697) – xiaoou wang Dec 10 '19 at 18:12
  • In that case you will need to provide a [minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) so that one can test solutions. – joshpk Dec 10 '19 at 18:16
  • 1
    @joshpk added, tks ! – xiaoou wang Dec 10 '19 at 18:22

2 Answers2

1

Looks like you just need to use bookdown's output formats.

---
output:
  bookdown::html_document2:
    df_print: paged
---

you may refer to this table using \@ref(tab:foo)

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

+-----------------------+-----------------------+-----------------------+
| Auteur                | Protocole             | Résultats             |
+=======================+=======================+=======================+
| (Jiayi 2011)          | Analyse formantique   | Diphtongaison de [e  |
+-----------------------+-----------------------+-----------------------+
joshpk
  • 729
  • 4
  • 11
  • it works ! thank you so much. By the way do you know how to make the caption appear under the table instead of above ? – xiaoou wang Dec 10 '19 at 18:38
  • Great! I think the general consensus is that table captions should be above tables (e.g. [this discussion](https://github.com/yihui/knitr/issues/1189)). Maybe ask as a new question if needed. – joshpk Dec 10 '19 at 18:50
0

The answer here works for me:

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

The table can now be reference via \@ref(tab:mwe) in bookdown. This is working for me in pdf and html exports.

Note that if you would like to add greek letters or more complicated sub-or superscripts, you will need to include the following text reference outside of the code chunk


(ref:flower) Flower~dimensions\ example~


```{r , echo=FALSE, results='asis'}
  cat(' Table: (\\#tab:mwe) Example with (ref:flower)

  | 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|')```
Martin
  • 401
  • 6
  • 15