How to set a reference in Rmarkdown to a gt() table


I am struggling to understand how I can reference to a gt table in my Rmarkdown file while outputting to PDF.
My current code takes a tibble, performs some gt() wrangling, and outputs the gt table in latex format.

table %>%
gt::gt() %>%

Calling this simple code in a code chunk wihtin my Rmarkdown file gives me a super nice table as shown below.

However, I can't figure out how to add a reference to the table such as "Table 1.1: Socioeconomics", which I can then refer to within the text in the Rmarkdown file.
I have been trying to search the web for help, but so far without any luck.
The function kable,


let me at a reference, but it overwrites all my gt() wrangling, leaving me with the original table instead. It does not seem to be able to handle the issue.

Does anyone know if this is doable as of now?

Thank you.

Cross-referencing is available in a branch version of gt and it looks like it's coming soon to the master version. There's an open issue asking for cross-referencing at the gt repository on github, as well as a pull request with code that implements cross-referencing.

Here's an example:*

First, install the version of the package with the cross-referencing updates in the pull request.

devtools::install_github("rstudio/gt", ref="eff3be7384365a44459691e49b9b740420cd0851")

Below is an rmarkdown document demonstrating cross-referencing with gt. The new label argument in tab_header is the cross-referencing label.

toc: FALSE

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)


You can see in Table \@ref(tab:tab1) that this is the famous `mtcars` data frame.

mtcars %>% 
  slice(1:2) %>% 
  gt() %>% 
  tab_header(title="First two rows of mtcars",

The PDF output is below. Note that the title text is in a larger font size than the Table 1:. Maybe this can be changed with some title formatting commands, but it would be nice to have gt output the entire caption in the same font size.

* I initially wasn't able to get cross-referencing to work, but Vincent Aral-Bundock provided the proper label syntax at github in response to my query.


Thank you so much. This just did the trick for me :slight_smile:
I appreciate the time you invested in your answer, and also how you took it one step further to the Github issue site.

I am looking forward to when the github version is available on CRAN as well. But for now it should be sufficient.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.