7

How can I get captions on my table floats in pdf_document generated by rmarkdown?

Using

output:
  pdf_document:
    fig_caption: true

and

```{r, fig.cap='a caption'} 
myplot 
```

Generates a floating figure with myplot and the caption specified.

How do I achieve the same thing with tables generated by xtable?

```{r, results='asis', fig.cap='table caption'}
    print(xtable(table), comment = FALSE)
```

I have tried using floating.environment = 'figure' in print.xtable, but to no avail.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Rickard
  • 3,600
  • 2
  • 19
  • 22

3 Answers3

14

Or similarly,

```{r results='asis'}
knitr::kable(head(mtcars), format = 'pandoc', caption = 'Title of the table')
```
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
3

The 'caption' is a parameter to xtable, not to print.xtable

```{r, results='asis'}
print(xtable(table, caption='Captions goes within xtable'), comment = FALSE)
```
Rickard
  • 3,600
  • 2
  • 19
  • 22
2

If you are using markdown anyway, why not stick to the same (simple and nice) format. Quick example:

> library(pander)
> pander(table(mtcars$am), caption = 'foo')

-------
 0   1 
--- ---
19  13 
-------

Table: foo
daroczig
  • 28,004
  • 7
  • 90
  • 124