如何在使用rmarkdown生成PDF输出时交叉引用列表中的kable元素?

huangapple go评论78阅读模式
英文:

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")

```

如何在使用rmarkdown生成PDF输出时交叉引用列表中的kable元素?

答案1

得分: 1

以下是翻译好的部分:

你可以将表格的标签存储在不同的列表中,然后在循环中将它们传递给 kableExtra::kbllabel 参数。因此,列表中的表格将使用这些标签创建,您稍后可以用它们来引用这些表格。

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]])

渲染输出

如何在使用rmarkdown生成PDF输出时交叉引用列表中的kable元素?

英文:

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: &quot;Table cross references&quot;
output: bookdown::pdf_document2
header-includes:
 - \usepackage{booktabs}
 - \usepackage{float}
---


```{r dw, include=FALSE}
library(kableExtra)
library(purrr)

ls_eg &lt;- vector(&quot;list&quot;)
ls_eg$df &lt;- list(
              mtcars[1:5, c(&quot;mpg&quot;, &quot;cyl&quot;)], 
              mtcars[6:10, c(&quot;mpg&quot;, &quot;cyl&quot;)]
            )

ls_eg$labels &lt;- list(&quot;table1&quot;, &quot;table2&quot;)

ls_eg$kbl &lt;- purrr::map2(.x = ls_eg$df, .y = ls_eg$labels, 
            .f = \(x, y) {
              x |&gt; 
                kbl(booktabs = TRUE,
                    caption = &quot;Kable as a list element&quot;,
                    label = y) |&gt; 
                kable_styling(latex_options = &quot;HOLD_position&quot;)
            })

```

## Include Table in body of report

See table \@ref(tab:table1) and \@ref(tab:table2)

```{r kbl-as-list-element, echo=FALSE, results=&#39;asis&#39;}
cat(ls_eg$kbl[[1]])
cat(ls_eg$kbl[[2]])
```

rendered output

如何在使用rmarkdown生成PDF输出时交叉引用列表中的kable元素?

huangapple
  • 本文由 发表于 2023年6月8日 03:33:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定