jandraor
jandraor

Reputation: 349

How to add a caption to a resized table with scalebox in xtable?

I'm working on a R Markdown document. I've got a dataframe of this kind:

library(tidyverse)
library(xtable)

df <- tibble(a = 1:10, b = 1:10, c = 1:10, d = 1:10, e = 1:10, f =1:10,
g = 1:10, h = 1:10, i = 1:10)

I'm using the xtable package to create display a table. Since the table is too wide, I scale the table with the scalebox parameter

xt <- xtable(df, caption = "Table 1")
print(xt, type = "latex", comment = FALSE,floating = F, 
include.rownames = F, scalebox = 0.50)

However, the caption is not displayed on the document. What can I do?

Upvotes: 0

Views: 662

Answers (1)

Kevin Arseneau
Kevin Arseneau

Reputation: 6264

If you are not limited to using xtable, I would recommend making the switch to knitr::kable and kableExtra.

---
output: pdf_document
---

```{r setup, include=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)

df <- tibble(a = 1:10, b = 1:10, c = 1:10, d = 1:10, e = 1:10, f =1:10,
g = 1:10, h = 1:10, i = 1:10)
```

```{r table, results='asis'}
df %>%
  kable("latex", caption = "Table 1", booktabs = TRUE) %>%
  kable_styling(latex_options = c("striped", "hold_position"))
```

Produces...

enter image description here

In addition, there is an equivalent scale_down option available to latex_options. However, as noted in the vignette it will fit to page width and therefore also scale up if the table is not sufficiently wide.

enter image description here

Upvotes: 1

Related Questions