Mathica
Mathica

Reputation: 1303

longtable= T messes up with scaled down table in r markdown pdf

I have a table in my r markdown pdf.

kable(df, "latex", longtable = F, booktabs = T) %>%
      kable_styling(latex_options = c("hold_position",
                                    "scale_down"),
                  fixed_thead = T)

this give a nice table centered in the page, but it is too long for the height of the page. so I added

kable(df, "latex", longtable = T, booktabs = T) %>%
  kable_styling(latex_options = c("repeat_header")) 

as suggested by many posts. My table now is split across several pages but now wider so that it does not fit within the width of the paper.

How can I keep the original width while still using longtable.

I run the same code suggested here enter link description here but my table exceeds the width of the paper.

Upvotes: 2

Views: 2102

Answers (3)

Ceres
Ceres

Reputation: 167

As Peter said, kable(..., longtable = TRUE) and kable_styling(..., full_witdh = TRUE) no longer work with R version >= 4.1.3 (2022-03-10) and RStudio 2022.02.3+492.

After (many) hours of trial and error with kable and kableExtra, the only solution I found uses pander. It doesn't explicitly control the width of the table, but the result looks good with Peter's example.

---
output:
  pdf_document
---

```{r}

library(pander)

cbind(iris, iris) |>
pander("This is a Table")

table out

Upvotes: 1

Peter
Peter

Reputation: 12699

You could try using kable_styling( full_width = TRUE) which fits a wide (10 column version of iris data across one page). You may need to tweek the column headings so they are legible. Without seeing your actual data it's difficult to suggest anything else.

Update: 2022-06-05 since R version 4.1.3 (2022-03-10) and RStudio 2022.02.3+492 this solution produces an error:

! Dimension too large.
\LT@max@sel #1#2->{\ifdim #2=\wd \tw@ 
                                      #1\else \number \c@LT@chunks \fi }{\th...
l.328 \end{longtabu}

---
output:
  pdf_document
---

```{r}

library(kableExtra)

cbind(iris, iris) |>
kbl("latex", longtable = T, booktabs = T) %>%
  kable_styling(latex_options = c("repeat_header"), full_width = TRUE) 

```

enter image description here

Upvotes: 0

Quinten
Quinten

Reputation: 41285

It seems like it is not possible to resize the table when using Longtable = T. When you run this code:

```{r}
library(kableExtra)
kable(iris, "latex", longtable = T, booktabs = T) %>%
  kable_styling(latex_options = c("repeat_header", "scale_down")) 
```

You get the following warning when trying: Warning in styling_latex_scale_down(out, table_info): Longtable cannot be resized.

Output:

enter image description here

When you add the command full_width = T to the kable_styling, it looks more scaled than before. Check this output:

enter image description here

Upvotes: 1

Related Questions