英文:
How to cross reference a kable which is an element of a list using rmarkdown for a pdf output?
问题
I'm generating a pdf from rmarkdown with cross references to tables. This works fine when a kable is generated within a chunk. Where a kable is an element of a list, and the kable includes a caption, the generated table is given a caption as expected, however, the cross reference for that table fails.
Is there a way to overcome this and enable table cross references where a kable is an element of a list?
My actual use case is a bit more complicated in that the kable is included in a list which has been generated as an output from a local package function.
英文:
I'm generating a pdf from rmarkdown with cross references to tables.This works fine when a kable is generated within a chuck. Where a kable is an element of a list, and the kable includes a caption the generated table is given a caption as expected, however, the cross reference for that table fails.
Is there a way to overcome this, and enable table cross references where a kable is an element of a list?
My actual use case is a bit more complicated in that the kable is included in a list which has been generated as an output from a local package function.
---
title: "Table cross references"
output:
bookdown::pdf_document2
header-includes:
- \usepackage{booktabs}
- \usepackage{float}
---
```{r dw, include=FALSE}
library(kableExtra)
ls_eg <- vector("list")
ls_eg$df1 <- mtcars[1:5, c("mpg", "cyl")]
ls_eg$kbl <-
ls_eg$df1 |>
kbl(booktabs = TRUE,
caption = "Kable as a list element") |>
kable_styling(latex_options = "HOLD_position")
```
## Include Table in body of report
See table \@ref(tab:kbl-as-list-element) for cross reference fails when kable is an element of a list.
```{r kbl-as-list-element, echo=FALSE}
ls_eg$kbl
```
Table \@ref(tab:kbl-in-chunk) cross reference fine.
```{r kbl-in-chunk, echo=FALSE}
ls_eg$df1 |>
kbl(booktabs = TRUE,
caption = "Kable in chunk") |>
kable_styling(latex_options = "HOLD_position")
```
答案1
得分: 1
以下是翻译好的部分:
你可以将表格的标签存储在不同的列表中,然后在循环中将它们传递给 kableExtra::kbl
的 label
参数。因此,列表中的表格将使用这些标签创建,您稍后可以用它们来引用这些表格。
library(kableExtra)
library(purrr)
ls_eg <- vector("list")
ls_eg$df <- list(
mtcars[1:5, c("mpg", "cyl")],
mtcars[6:10, c("mpg", "cyl")]
)
ls_eg$labels <- list("table1", "table2")
ls_eg$kbl <- purrr::map2(.x = ls_eg$df, .y = ls_eg$labels,
.f = \(x, y) {
x |>
kbl(booktabs = TRUE,
caption = "Kable as a list element",
label = y) |>
kable_styling(latex_options = "HOLD_position")
})
将表格包含在报告正文中
查看表格 @ref(tab:table1) 和 @ref(tab:table2)
cat(ls_eg$kbl[[1]])
cat(ls_eg$kbl[[2]])
渲染输出
英文:
You can store the table labels in a different list and then pass them in a loop to kableExtra::kbl
's label
argument. So the tables in the list will be created with those labels which you can use later to refer to them.
---
title: "Table cross references"
output: bookdown::pdf_document2
header-includes:
- \usepackage{booktabs}
- \usepackage{float}
---
```{r dw, include=FALSE}
library(kableExtra)
library(purrr)
ls_eg <- vector("list")
ls_eg$df <- list(
mtcars[1:5, c("mpg", "cyl")],
mtcars[6:10, c("mpg", "cyl")]
)
ls_eg$labels <- list("table1", "table2")
ls_eg$kbl <- purrr::map2(.x = ls_eg$df, .y = ls_eg$labels,
.f = \(x, y) {
x |>
kbl(booktabs = TRUE,
caption = "Kable as a list element",
label = y) |>
kable_styling(latex_options = "HOLD_position")
})
```
## Include Table in body of report
See table \@ref(tab:table1) and \@ref(tab:table2)
```{r kbl-as-list-element, echo=FALSE, results='asis'}
cat(ls_eg$kbl[[1]])
cat(ls_eg$kbl[[2]])
```
rendered output
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论