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

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

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.

  1. ---
  2. title: "Table cross references"
  3. output:
  4. bookdown::pdf_document2
  5. header-includes:
  6. - \usepackage{booktabs}
  7. - \usepackage{float}
  8. ---
  9. ```{r dw, include=FALSE}
  10. library(kableExtra)
  11. ls_eg <- vector("list")
  12. ls_eg$df1 <- mtcars[1:5, c("mpg", "cyl")]
  13. ls_eg$kbl <-
  14. ls_eg$df1 |>
  15. kbl(booktabs = TRUE,
  16. caption = "Kable as a list element") |>
  17. kable_styling(latex_options = "HOLD_position")
  18. ```
  19. ## Include Table in body of report
  20. See table \@ref(tab:kbl-as-list-element) for cross reference fails when kable is an element of a list.
  21. ```{r kbl-as-list-element, echo=FALSE}
  22. ls_eg$kbl
  23. ```
  24. Table \@ref(tab:kbl-in-chunk) cross reference fine.
  25. ```{r kbl-in-chunk, echo=FALSE}
  26. ls_eg$df1 |>
  27. kbl(booktabs = TRUE,
  28. caption = "Kable in chunk") |>
  29. kable_styling(latex_options = "HOLD_position")
  30. ```

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

答案1

得分: 1

以下是翻译好的部分:

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

  1. library(kableExtra)
  2. library(purrr)
  3. ls_eg <- vector("list")
  4. ls_eg$df <- list(
  5. mtcars[1:5, c("mpg", "cyl")],
  6. mtcars[6:10, c("mpg", "cyl")]
  7. )
  8. ls_eg$labels <- list("table1", "table2")
  9. ls_eg$kbl <- purrr::map2(.x = ls_eg$df, .y = ls_eg$labels,
  10. .f = \(x, y) {
  11. x |>
  12. kbl(booktabs = TRUE,
  13. caption = "Kable as a list element",
  14. label = y) |>
  15. kable_styling(latex_options = "HOLD_position")
  16. })

将表格包含在报告正文中

查看表格 @ref(tab:table1) 和 @ref(tab:table2)

  1. cat(ls_eg$kbl[[1]])
  2. 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.

  1. ---
  2. title: &quot;Table cross references&quot;
  3. output: bookdown::pdf_document2
  4. header-includes:
  5. - \usepackage{booktabs}
  6. - \usepackage{float}
  7. ---
  8. ```{r dw, include=FALSE}
  9. library(kableExtra)
  10. library(purrr)
  11. ls_eg &lt;- vector(&quot;list&quot;)
  12. ls_eg$df &lt;- list(
  13. mtcars[1:5, c(&quot;mpg&quot;, &quot;cyl&quot;)],
  14. mtcars[6:10, c(&quot;mpg&quot;, &quot;cyl&quot;)]
  15. )
  16. ls_eg$labels &lt;- list(&quot;table1&quot;, &quot;table2&quot;)
  17. ls_eg$kbl &lt;- purrr::map2(.x = ls_eg$df, .y = ls_eg$labels,
  18. .f = \(x, y) {
  19. x |&gt;
  20. kbl(booktabs = TRUE,
  21. caption = &quot;Kable as a list element&quot;,
  22. label = y) |&gt;
  23. kable_styling(latex_options = &quot;HOLD_position&quot;)
  24. })
  25. ```
  26. ## Include Table in body of report
  27. See table \@ref(tab:table1) and \@ref(tab:table2)
  28. ```{r kbl-as-list-element, echo=FALSE, results=&#39;asis&#39;}
  29. cat(ls_eg$kbl[[1]])
  30. cat(ls_eg$kbl[[2]])
  31. ```

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:

确定