1

I estimate a model with estimatr::lm_robust and display the model output with modelsummary::modelsummary. Consider the following example:

df <- tibble(x=runif(100, 0, 100)) %>% mutate(y=x*(2+runif(100, 0, 10)), z=rep(c(1:10), 10))

tab <- modelsummary::modelsummary(
  list("A very, very long title that will span multiple lines"=estimatr::lm_robust(data=df, clusters=z, y~x),
       "A short title"=estimatr::lm_robust(data=df, y~x),
       "A very, very long title that will span multiple lines"=estimatr::lm_robust(data=df, clusters=z, y~x),
       "A short title"=estimatr::lm_robust(data=df, y~x)),
  title = "This table should be on one landscape page in LaTeX with automatically adjusted column width and line breaks",
  notes = c("A note that is very long and says many things and will span multiple lines where I would like automatic line breaks in LaTeX"),
  output="kableExtra")

I now want to perform some kableExtra manipulations:

tab %>% 
  add_header_above(c(" " = 1, "M1" = 1, "M2" = 1, "M3" = 2)) %>%
  row_spec(3, color = 'red') %>%
  row_spec(5, background = 'lightblue') %>% 
  column_spec(1, width="10.5cm")

I want to ensure that the table overall has the full page width (be it landscape or portrait) and line breaks automatically included. How do I best do this?

Further, I want save to .tex. However, tab %>% ... %>% save_kable(format = "latex", file = "latex_table.tex") gives an empty file. What am I doing wrong?

Can someone point me into the right direction? Thanks a lot.

Ivo
  • 3,890
  • 5
  • 22
  • 53
  • Try to add `kable_styling(latex_options="scale_down")` for full page width! And maybe check [this](https://cran.r-project.org/web/packages/kableExtra/vignettes/best_practice_for_newline_in_latex_table.pdf) out for line breaks. – Julian Jan 30 '23 at 11:18
  • Thank you @Julian! I am still struggling to export it to LaTeX though - am I making an obvious mistake? – Ivo Jan 30 '23 at 15:00
  • I would try it without the pipe, ie `save_kable(x = tab, format...` – Julian Jan 30 '23 at 15:30

0 Answers0