如何在R Markdown中使用kbl减少表格列间距
问题:减少kableExtra生成表格的列间距(不修改现有列宽)
我用R Markdown结合kableExtra包创建了如下表格,但生成的表格列间距过大。已经通过column_spec设置了列宽,希望在不改动列宽的前提下缩小列之间的间距,请问该如何实现?
```{r table_r_3, message=FALSE, warning=FALSE, echo=FALSE} library(kableExtra) tab <- matrix(c("Description that takes 43 characters +123","2.89*** (1.12-3.90)","2.89*** (1.12-3.90)","2.89*** (1.12-3.90)","2.89 (1.12-3.90)","2.89*** (1.12-3.90)","2.89*** (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)", "Description that takes 43 characters +123","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)","2.89 (1.12-3.90)" ), ncol=13, byrow=TRUE) colnames(tab) <- c(' ','A1','A2','A1','A2','A1','A2','A1','A2','A1','A2','A1','A2') rownames(tab) <- NULL tab <- as.table(tab) table_r_3_output <- kbl(tab, booktabs = T, linesep = "", row.names = 0, caption = "Results.") %>% kable_styling(font_size = 5, latex_options = "hold_position") %>% add_header_above(c(" " = 1, "Grp1 & Grp2" = 2, "Grp1" = 2, "Grp1 & Grp2" = 2, "Grp1" = 2, "Grp1 & Grp2" = 2, "Grp1" = 2)) %>% add_header_above(c(" " = 1, "Jan 2015 - Dec 2017" = 4, "Jan 2016 - Dec 2016" = 4, "Jan 2017 - Dec 2017" = 4), escape = FALSE) %>% column_spec(1, width = "19em") %>% column_spec(c(2:13), width = "5em")
table_r_3_output
--- # 解决方案 要在保留现有列宽的前提下缩小列间距,核心是调整LaTeX表格的`\tabcolsep`参数(该参数控制列与列之间的空白宽度,默认值通常为6pt),以下两种方法均可实现: ## 方法1:在`kbl()`中直接添加LaTeX命令 在`kbl()`函数的`extra`参数中插入`\setlength{\tabcolsep}{<数值>pt}`,自定义列间距大小(示例设为2pt,可根据可读性调整): ```r table_r_3_output <- kbl(tab, booktabs = T, linesep = "", row.names = 0, caption = "Results.", # 新增这一行,调整列间距 extra = "\\setlength{\\tabcolsep}{2pt}") %>% kable_styling(font_size = 5, latex_options = "hold_position") %>% add_header_above(c(" " = 1, "Grp1 & Grp2" = 2, "Grp1" = 2, "Grp1 & Grp2" = 2, "Grp1" = 2, "Grp1 & Grp2" = 2, "Grp1" = 2)) %>% add_header_above(c(" " = 1, "Jan 2015 - Dec 2017" = 4, "Jan 2016 - Dec 2016" = 4, "Jan 2017 - Dec 2017" = 4), escape = FALSE) %>% column_spec(1, width = "19em") %>% column_spec(c(2:13), width = "5em")
方法2:通过add_footnote()实现局部生效
如果不想影响文档中的其他表格,可利用LaTeX分组机制,通过add_footnote()将列间距设置限定在当前表格范围内:
table_r_3_output <- kbl(tab, booktabs = T, linesep = "", row.names = 0, caption = "Results.") %>% kable_styling(font_size = 5, latex_options = "hold_position") %>% add_header_above(c(" " = 1, "Grp1 & Grp2" = 2, "Grp1" = 2, "Grp1 & Grp2" = 2, "Grp1" = 2, "Grp1 & Grp2" = 2, "Grp1" = 2)) %>% add_header_above(c(" " = 1, "Jan 2015 - Dec 2017" = 4, "Jan 2016 - Dec 2016" = 4, "Jan 2017 - Dec 2017" = 4), escape = FALSE) %>% column_spec(1, width = "19em") %>% column_spec(c(2:13), width = "5em") %>% # 新增这一行,局部调整列间距 add_footnote("\\setlength{\\tabcolsep}{2pt}", escape = FALSE, footnote_as_chunk = TRUE)
原理说明
\tabcolsep是LaTeX表格的核心参数,控制列边缘之间的水平空白距离。调整该参数的数值时,column_spec设置的列宽不会受到影响,只会改变列与列之间的空隙大小。建议在1pt-4pt之间测试,找到兼顾紧凑性和可读性的数值。
内容的提问来源于stack exchange,提问作者Dan
相关产品推荐
相关产品推荐

