Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 313
Rickard Avatar asked Jul 04 '14 10:07

Rickard


People also ask

How do I add a caption to a table in R markdown?

You can add a caption to the table via the caption argument, e.g. (see Table 10.1 for the output), knitr::kable(iris2, caption = "An example table caption.") TABLE 10.1: An example table caption.

How do I use R markdown in PDF?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

What is TOC in R markdown?

1 Table of contents. You can add a table of contents (TOC) using the toc option and specify the depth of headers that it applies to using the toc_depth option. For example: --- title: "Habits" output: html_document: toc: true toc_depth: 2 ---


1 Answers

Or similarly,

```{r results='asis'}
knitr::kable(head(mtcars), format = 'pandoc', caption = 'Title of the table')
```
like image 109
Yihui Xie Avatar answered Sep 20 '22 00:09

Yihui Xie