1

Somehow my RMarkdown document is not crossreferencing tables or figures. Here is a stripped down version of my document.

---
title: "Test"
author: "Me"
date: "01/04/2022"
output: bookdown::pdf_document2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
var1<-sample(LETTERS)
tab1<-table(var1)

My table is in Table \@ref{tab:tab1}

library(knitr)
kable(tab1, caption="my table")

AS we see in Figure \@ref{fig:plot1}

plot(seq(1,10,1))
spindoctor
  • 1,719
  • 1
  • 18
  • 42
  • Does this answer your question? [how to cross reference tables and plots in rmarkdown?](https://stackoverflow.com/questions/52462761/how-to-cross-reference-tables-and-plots-in-rmarkdown) – Peter Apr 01 '22 at 16:33

1 Answers1

2

You should call your tab1 in the code chunk like this {r tab1}. And use () instead of {} for your @ref. In that case it reference to your figures and tables. You can use the following code:

---
title: "Test"
author: "Me"
date: "01/04/2022"
output: bookdown::pdf_document2
---

My table is in Table \@ref(tab:tab1)

```{r tab1, echo =FALSE}
var1<-sample(LETTERS)
tab1<-table(var1)

library(knitr)
kable(tab1, caption="my table")
```

\newpage

AS we see in Figure \@ref(fig:plot1)

```{r plot1, fig.cap ="plot", echo=FALSE}
par(mar = c(4, 4, .2, .1))
plot(seq(1,10,1))
```

Output:

enter image description here

As you can see on the image, when you click on 1 in will go to your table.

Quinten
  • 35,235
  • 5
  • 20
  • 53