0

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)
MBorg
  • 1,345
  • 2
  • 19
  • 38
  • 1
    Have you tried a Solution proposed [here](https://tex.stackexchange.com/questions/16519/adding-space-between-columns-in-a-table)? – Julian Dec 09 '22 at 13:23

2 Answers2

1

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

MBorg
  • 1,345
  • 2
  • 19
  • 38
0

One option might be to use kableExtra::column_spec(), this requires a bit of trial and error to establish the column width which can be further influenced by font size.

library(kableExtra)

kbl(table1, 
    booktabs = TRUE) |> 
  column_spec(3:10, width = "13mm")

Peter
  • 11,500
  • 5
  • 21
  • 31
  • 1
    column_spec() can help by lowering the column width, forcing cell text into the next row. Similary, kable_styling(font_size) helps by lowering the text size. But neither actually change the column spacing, which results in white space between columns that cannot be filled. – MBorg Dec 09 '22 at 12:29
  • When I try with `width = "8mm"` there is no spacing between the numbers in adjacent cells. If I try with width less than 8mm, say 6mm the 7 digit numbers between adjacent cells overlap. I'll have another look at how this fits in with cell or column padding if at all. – Peter Dec 09 '22 at 13:17