5

I am currently working on a Markdown file (with latex) where I use kable() and kableExtra for my tables. The problem is that some of my tables are to big and doesn't fit on a pdf page (even in landscape).

I have tried to use latex_options = "scale_down" from kableExtra but for some reasons it doesn't work, it doesn't change anything. Here is an example of the code I'm running :

kable(dt, "latex", longtable = T, caption = "SampleCaption") %>% 
  add_header_above(c("","Mens" = 3, "Womens" = 3))  %>%  
  kable_styling(latex_options = c("striped", "scale_down", "repeat_header"),repeat_header_text = "",
                full_width = F) %>%
  column_spec(1, width = "10cm")    

I already looked on Google and stackoverflow. Anyone have an idea of what I'm doing wrong? Thanks

Edit, here is the working code as requested in comments :

  kable(dt, "latex", longtable = T, caption = "SampleCaption") %>% 
 add_header_above(c("","Mens" = 3, "Womens" = 3, "Total" = 2))  %>%  
 kable_styling(font_size = 7, latex_options = c("striped", "repeat_header"),repeat_header_text = "",
               full_width = F) %>%
 column_spec(1, width = "5cm") 
Gainz
  • 1,721
  • 9
  • 24
  • It might help if you can provide a full working markdown file with data large enough to do it. Granted, I could do so as well using `iris` or `ggplot2::diamonds`, but you providing a complete and reproducible example really helps. – r2evans Jan 08 '19 at 08:06
  • 1
    @r2evans Thanks for the answer. I actually pretty much resolve the problem by reducing the font size inside the table. I still don't know why "scale_down" wasn't working properly tho. – Gainz Jan 08 '19 at 15:33
  • That might be a good issue for the maintainers. Do you know, does it allow use of LaTeX's "longtable" package? I can see some work in my future involving multiple pages for a table. (Could you please post your working code and, if you have time, a before/after screenshot of the pages?) – r2evans Jan 08 '19 at 15:56
  • Well most of my tables require the "longtable" feature which is in the kableExtra package. You could probably also use the LaTeX's "longtable" package too which share similar features for columns on multiple pages. I can post the working code for one of my big table but I can't post a true before/after screenshot since the data I'm working on contains private informations. Here is an exemple of what my new code changed : https://i.imgur.com/Zod850l.png – Gainz Jan 08 '19 at 16:13
  • 1
    So while this is the same table, decreasing the font size and the column spec helped a lot to fit the table on the whole page. The same can be done for longtable. I'll post my working code for you in my initial question. – Gainz Jan 08 '19 at 16:15

1 Answers1

4

This is not an answer but more like a clarification that since scale_down is using the resizebox in package graphicx while longtable is longtable and these two latex packages won't talk with each other, scale_down only works for normal tables.

In fact, you should be seeing a note in your console that "scale_down" doesn't work with longtable

Source in kableExtra

if (table_info$tabular == "longtable") {
    warning("Longtable cannot be resized.")
    return(x)
}
Hao
  • 7,476
  • 1
  • 38
  • 59
  • 1
    First, thanks for the answer! Yeah I've notice after my question that using longtable with scale_down didn't work for some reason. I didn't get any error in R console tho. I will upvote your answer anyway so maybe it can help someone in the future. Also to anyone seeking info on kable and LaTeX in R Markdown go read this : https://www.google.com/url?sa=t&source=web&rct=j&url=https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf&ved=2ahUKEwiM3qil7PTfAhUnTt8KHfgKCToQFjABegQIBBAB&usg=AOvVaw1KUfrGaz2bDPo-9bmnbFlw – Gainz Jan 17 '19 at 12:46
  • 2
    Haha, I will add a note somewhere in the PDF document. I wrote this piece of code about 2 years ago. Thanks for bringing up this issue! :D – Hao Jan 17 '19 at 12:57