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

问题描述 投票:9回答:2

我想在.Rmd文件中使用Table 1:...xtable()时,从R脚本命名我的表而没有自动knitr::kable()前缀。输出为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文件中使用它。

r latex rstudio knitr xtable
2个回答
11
投票

事实证明,我需要在YAML部分中添加以下内容:

header-includes:
    - \usepackage{caption}

以及以下代码块之前的位置:

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

这里也对此进行了描述:

Get rid of captions using texreg in markdown

是的,我没有立即找到答案就有些尴尬。

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


4
投票

如果您也希望以相同的方式显示数字,请按背心将示例修改为

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

并跳过\captionsetup{}

© www.soinside.com 2019 - 2024. All rights reserved.