Search code examples
rr-markdownknitrkable

Control spacing between columns in tables created with kable(, format = 'latex')


How can one control the spacing between columns in tables created with kable(, format = 'latex') in R? Specifically, I wish to reduce the spacing beyond what booktabs=T can achieve for the latex format.

Although there is a similar question for format='markdown', the solution of the padding argument is incompatible with the latex format, and there does not seem to be a kableExtra option for this.

I've included a practically-identical reproducible example from the aforementioned question:

---
title: "Untitled"
author: "The author"
date: "`r Sys.Date()`"
output: pdf_document
---
```{r results='tble', echo=F, warning=F}
library(knitr)
table1 <- data.frame(Factor1 = c('level 1', 'level 1', 'level 2', 'level 2'),
                     Factor2 = c('level 1', 'level 2', 'level 1', 'level 2'),
                     Parameter1 = sample(1000000:9999999, 2),
                     Parameter2 = sample(1000000:9999999, 2),
                     Parameter3 = sample(1000000:9999999, 2),
                     Parameter4 = sample(1000000:9999999, 2),
                     Parameter5 = sample(1000000:9999999, 2),
                     Parameter6 = sample(1000000:9999999, 2),
                     Parameter7 = sample(1000000:9999999, 2))
names(table1) <- c('Factor1', 'Factor2', 'Parameter1', 'Parameter2', 'Parameter3', 'Parameter4', 'Parameter5', 'Parameter6', 'Parameter7')
kable(table1, format='latex', booktabs=T)

Solution

  • Following Julian's comment, column spacing can be editted using LateX. The length of tabcolsept can be editted manually, such as for a small size:

    \setlength{\tabcolsep}{3pt} # default size is usually 6