When cross referencing tables and figures in RMarkdown via \@ref(tab:...)
and/or \@ref(fig:...)
the rendered output will create a link on the number of the respective output. i.e. "Table 1" will only show a link on "1" and likewise "Figure 1" will only show a link on "1".
Can I extend the linking text so that the link spans the full e.g. "Table 1"/"Figure 1"? If yes, how? I'm looking for a solution that will generically work for both docx and html outputs.
---
output:
officedown::rdocx_document:
base_format: "rmarkdown::word_document"
bookdown::html_document2:
toc: true
toc_float: true
fig_caption: true
number_sections: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
library(flextable)
if (!knitr::is_html_output()) {
library(officedown)
}
```
## My nice iris plot
```{r my-nice-figure, fig.cap = "A really cool figure."}
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
```
## My nice iris table
```{r my-nice-table, tab.cap = "A really cool table.", tab.id = "my-nice-table"}
flextable(head(iris))
```
Table \@ref(tab:my-nice-table) shows interesting things.
The same is true for Figure \@ref(fig:my-nice-figure).
With just bookdown, this is not possible: https://forum.posit.co/t/changing-labels-of-cross-references-on-bookdown/126621 The reason is how bookdown works- it replaces the reference with a number only. Wrapping the reference in some way breaks this.
The question was asked here without an answer. This Question has a workaround, but only for PDF.
This question has no answers, but the sole comment suggests using Quarto.
Here is the Quarto guide for references and the guide for using RStudio.
This would mean using Quarto-R
and creating a .qmd
file. Here is a minimal example that does what you want to achieve:
---
title: "Quarto r reference example"
format: html
editor: visual
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
library(flextable)
library(quarto)
if (!knitr::is_html_output()) {
library(officedown)
}
```
## My nice iris plot
```{r fig-my-nice-figure}
#| label: fig-mynicefig
#| fig-cap: "My nice figure"
#| warning: false
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
```
## My nice iris table
```{r tab-my-nice-table}
#| label: tbl-mynicetab
#| tbl-cap: "My nice table"
#| warning: false
flextable(head(iris))
```
## My heading
@fig-mynicefig shows something.
@tbl-mynicetab also shows something.
Output of the text references:
However, Quarto does have some limitations when generating .docx
documents: Cross-references and captions not working when using gt in Quarto with Word output
Whether the Quarto workaround is worth the price would depend on how much you rely on bookdown
or the word-output. If you are using bookdown
for academic purposes, then the default behaviour of linking only the number, not the whole text or tag, is actually the default in many papers. In this case, you are probably better off leaving Qarto
be.
If accessibility for a website or something is your concern, then I would suggest the switch, since hitting single-digit links can be difficult for vision-impaired people.