如何使用 xtable 或 knitr::kable 抑制 .Rmd 文件中的自动 table 名称和编号?

How to suppress automatic table name and number in an .Rmd file using xtable or knitr::kable?

在 .Rmd 文件中使用 xtable()knitr::kable() 时,我想从 R 脚本中命名我的表而不使用自动 Table 1:... 前缀。输出为pdf文档。

这是一个来自 .Rmd 文件的可重现示例:

---
title: "Suppress automatic table name and number"
output: pdf_document
---

```{r myirischunk, results = 'asis', tab.cap = NULL, echo = TRUE}
library(xtable)

print(knitr::kable(head(iris), caption = "I sure wish it would say Table    1.a"))
print(knitr::kable(head(iris), caption = "Please stop"))
print(xtable(head(iris), caption = "Same thing with xtable"))
```

我看过类似的问题和一些建议 here,但我似乎无法在 .Rmd 文件中使用它。

原来我需要在 YAML 部分添加以下内容:

header-includes:
    - \usepackage{caption}

AND 以下代码块之前的某处:

\captionsetup[table]{labelformat=empty}

现在可以使用了:

---
title: "Suppress automatic table name and number"
output: pdf_document
header-includes:
    - \usepackage{caption}
---

\captionsetup[table]{labelformat=empty}

```{r myirischunk, results = 'asis', tab.cap = NULL, echo = TRUE}
print(knitr::kable(head(iris), caption = "Table 21.a - My very own table name"))
```

这里也有描述:

是的,我有点尴尬,因为我没有立即找到答案。

无论如何,感谢 daroczig 将我指向 tex 方向,而不是尝试使用块选项或类似的东西来解决问题。

如果你也想要同样的数字,将 vestland 的示例修改为

---
title: "Suppress automatic table name and number"
output: pdf_document
header-includes:
    - \usepackage[labelformat=empty]{caption}
---

并跳过 \captionsetup{}.