Search code examples
rknitrr-markdownxtable

Captions on tables in pdf documents generated by rmarkdown


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.


Solution

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

    ```{r, results='asis'}
    print(xtable(table, caption='Captions goes within xtable'), comment = FALSE)
    ```