14

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.

vestland
  • 55,229
  • 37
  • 187
  • 305
  • 1
    IMO it's not an R, but rather a LaTeX question, which could be answered something like `\captionsetup{labelformat=empty}` in the preamble. – daroczig Jul 02 '15 at 13:05
  • Ok, but how do I include this in the .Rmd file? In the YAML header? – vestland Jul 03 '15 at 06:54
  • yup, see e.g. http://rmarkdown.rstudio.com/pdf_document_format.html#includes – daroczig Jul 03 '15 at 10:54
  • @daroczig Thanks for the hints. I've been messing around with chunk settings like fig.lp = '' and other things from the source you mentioned. As well as trying different things in the YAML section, but so far without success. Most examples seem to be "pure" LaTeX examples, and not knitted markdown applying LaTeX functions (I don't even know if that makes sense). Any other suggestion on how to squeeze that \captionsetup{labelformat=empty} into either the YAML section or chunk options using markdown would be sweet. Anyway, thanks for your help so far. – vestland Jul 03 '15 at 19:52
  • possible duplicate of [get rid of captions when using texreg in Rmarkdown](http://stackoverflow.com/questions/30679537/get-rid-of-captions-when-using-texreg-in-rmarkdown) – Nick Kennedy Jul 03 '15 at 22:10

2 Answers2

23

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.

Community
  • 1
  • 1
vestland
  • 55,229
  • 37
  • 187
  • 305
5

In case you also want figures the same way, modify the example by vestland to

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

and skip the \captionsetup{}.

vestland
  • 55,229
  • 37
  • 187
  • 305
micce
  • 131
  • 1
  • 4