Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

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

I'd like to name my tables from R scripts without the automatic Table 1:... prefix when using xtable() or knitr::kable() in an .Rmd file. Output is a pdf document.

Here's a reproducible example from an .Rmd file:

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

I've seen similar questions with some suggestions here, but I can't seem to get it to work in an .Rmd file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It turns out that I needed to add the following in the YAML section:

header-includes:
    - usepackage{caption}

AND the following somewhere before the code chunk:

captionsetup[table]{labelformat=empty}

Now it works:

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

This has also been described here:

Get rid of captions using texreg in markdown

And yes, I'm a bit embarrased that I didn't find that answer straight away.

Anyway, thanks to daroczig for pointing me in the tex direction instead of trying to solve the problem using chunk options or something like that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...