2

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.

Peter
  • 11,500
  • 5
  • 21
  • 31
Mathica
  • 1,241
  • 1
  • 5
  • 17

3 Answers3

1

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

Quinten
  • 35,235
  • 5
  • 20
  • 53
1

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

Ceres
  • 163
  • 7
0

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

Peter
  • 11,500
  • 5
  • 21
  • 31
  • This solution fails for me with current CRAN package versions. Any chance you can share your session info? ``` ! Dimension too large. \LT@max@sel #1#2->{\ifdim #2=\wd \tw@ #1\else \number \c@LT@chunks \fi }{\th... l.376 \end{longtabu} ```` – Ceres Jun 03 '22 at 17:39
  • 1
    I'm afraid it now fails for me too with: R version 4.1.3 (2022-03-10) and RStudio 2022.02.3+492. I think I was using R 4.0... something in March 2022. I get a similar error as you do. I've had a little look at what could be done but I just do not have time to explore this at the moment. If you find a solution please add it as an updated answer to this question. – Peter Jun 04 '22 at 08:34
  • Done, see [above](https://stackoverflow.com/a/72511243/11969696) – Ceres Jun 13 '22 at 16:24