Reputation: 11762
I am trying to reference a table in a word document using bookdown
package.
I wanted to add the reference id in the fig.cap
parameter of the code chunk, but this is somehow not seen by the interpreter and I don't get the link to the reference.
As a workaround, I added my reference ID to the caption of the table, but here the full id ({#mysecondtable2}
) is written in the figure caption and this looks ugly.
Any idea on how to solve this? Maybe LUA filter to remove the ugly anchor from the table caption? I don't understand how to do this.
---
title: "Untitled"
author: "Mario"
date: '2022-11-10'
output:
bookdown::word_document2:
toc: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
See Table \@ref(tab:myfirsttable). Or click [Table \@ref(tab:mysecondtable)](#mysecondtable2).
```{r myfirsttable, echo = FALSE}
knitr::kable(cars, caption = "First three rows of cars dataset")
```
See Table \@ref(tab:mysecondtable).
```{r mysecondtable, echo = FALSE, fig.cap='{#mysecondtable2}'}
knitr::kable(head(iris, 3),
caption='{#mysecondtable2} test')
```
EDIT:
Some strange behaviour shows, that [Table \@ref(tab:mysecondtable)](#mysecondtable)
seems to work. But actually it only refers to the caption of the second table but ignores the 2 at the end...
Upvotes: 1
Views: 331
Reputation: 22544
One straight-forward solution is to wrap the table into an extra div, as one can then link to that div:
Or click [Table \@ref(tab:mysecondtable)](#mysecondtable-wrapper).
::: {#mysecondtable-wrapper}
```{r mysecondtable, echo = FALSE}
knitr::kable(head(iris, 3),
caption='test')
```
:::
A slightly less crude way is to put a span into the caption:
knitr::kable(head(iris, 3),
caption='[test]{#mysecondtable}')
The difference between the two is that the link will point at the whole table when using a div, and to the caption when using a span.
Upvotes: 4