【问题标题】:Adding a letter to all table numbers in RMarkdown在 RMarkdown 中为所有表格编号添加一个字母
【发布时间】:2020-11-17 02:45:11
【问题描述】:

我正在使用 RMarkdown 为论文创建补充文档。这些补充包含许多表格。让我们将这些文档称为 Supplement A 和 Supplement B。我希望表格编号能够反映补充字母,即 Supplement A 中的第一个表格的 Table A1 或 Table 1A 等等。

如何修改表编号以将字母添加到表编号架构中?

下面是一个生成正常编号表格的示例:

---
title: "Supplement A"
output: pdf_document
---

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

```{r cars}
kable(mtcars[1:5, ], booktabs=TRUE, caption="MTCars") %>%
  kable_styling(latex_options="hold_position", position="left")
```

【问题讨论】:

    标签: r-markdown pdf-generation caption


    【解决方案1】:

    使用captioner 包(此处提供详细信息:https://datascienceplus.com/r-markdown-how-to-number-and-reference-tables/)的不雅解决方案可以创建字幕并在代码块之前手动插入它们。如果标题是在代码块 (How to suppress automatic table name and number in an .Rmd file using xtable or knitr::kable?) 中生成的,则将其与 Latex 包caption 结合使用可以删除自动表命名和编号。我在 YAML 中使用 \captionsetup[table]{labelformat=empty} 完成了此操作,但也可以在文档正文中完成。无论哪种方式,标题都可以在代码块中生成,这对我来说很重要。

    此解决方案使 bookdown 引用停止工作(因为 labelformat 不能是 empty),但在使用 captioner 包的链接中提供了表引用的解决方法(并包含在下面)。

    ---
    title: "Supplement A"
    header-includes:
      - \usepackage{caption}
        \captionsetup[table]{labelformat=empty}
    output: pdf_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    library(knitr)
    library(kableExtra)
    library(captioner)
    library(stringr)
    ```
    
    ```{r captions}
    # Create the caption(s)
    caption <- captioner("Table A", FALSE)
    tab_1_cap <- caption("Tab_1", "MTCars")
    
    # Create a function for referring to the tables in text
    ref <- function(x) str_extract(x, "[^:]*")
    ```
    
    The following table is `r ref(tab_1_cap)`.
    
    ```{r cars}
    kable(mtcars[1:5, ], booktabs=TRUE, caption=tab_1_cap) %>%
      kable_styling(latex_options="hold_position", position="left")
    ```
    

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 2017-07-22
      • 1970-01-01
      • 2015-10-04
      相关资源
      最近更新 更多