Skip to content

Instantly share code, notes, and snippets.

@moritzpschwarz
Last active October 12, 2023 14:16
Show Gist options
  • Save moritzpschwarz/f8ebc635070a83aafe6946a04ee26b29 to your computer and use it in GitHub Desktop.
Save moritzpschwarz/f8ebc635070a83aafe6946a04ee26b29 to your computer and use it in GitHub Desktop.
kable commands for tex tables
# Source: oxforddown explanation in the chapter on tables
library(kableExtra)
head(mtcars) %>%
kable(booktabs = TRUE) %>%
kable_styling(latex_options = "scale_down")
a_long_table <- rbind(mtcars, mtcars)
a_long_table %>%
select(1:8) %>%
kable(booktabs = TRUE, longtable = TRUE)
a_long_table %>%
kable(booktabs = TRUE, longtable = TRUE) %>%
kable_styling(latex_options = "repeat_header")
#Unfortunately, we cannot use the scale_down option with a longtable . So if a
#longtable is too wide, you can either manually adjust the font size, or show the table
#in landscape layout. To adjust the font size, use kableExtra’s font_size option:
a_long_table %>%
kable(booktabs = TRUE, longtable = TRUE) %>%
kable_styling(font_size = 9, latex_options = "repeat_header")
#To put the table in landscape mode, use kableExtra’s landscape function:
a_long_table %>%
kable(booktabs = TRUE, longtable = TRUE) %>%
kable_styling(latex_options = "repeat_header") %>%
landscape()
our_adjusted_table <- a_long_table %>%
kable(booktabs = TRUE, longtable = TRUE) %>%
kable_styling(latex_options = "repeat_header") %>%
# wrap the longtable in a tiny environment
str_replace('\\\\begin\\{longtable\\}',
'\\\\begin\\{scriptsize\\}\n\\\\begin\\{longtable\\}') %>%
str_replace('\\\\end\\{longtable\\}',
'\\\\end\\{longtable\\}\n\\\\end\\{scriptsize\\}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment