rr-markdownr-flextable

Reduce padding between captions and tables


I'm using flextable to loop over a list of data, generating captions and tables for each element in the list to generate tables in an rmarkdown document. While I can successfully render the captions and tables, the padding/margins between captions and tables is too large, and I can't find a way to reduce them. Here's what I have so far:

---
title: "Untitled"
output: html_document
date: '2022-05-09'
---

``{r setup, include=FALSE}
library(flextable)
library(magrittr)
``

``{r results='asis', echo=FALSE, ft.align="left"}

my_list<-list()
my_list$ds1<-head(mtcars, 5)
my_list$ds2<-head(mtcars, 10)

for (i in 1:length(my_list)) {
  
  myft <- flextable(my_list[[i]]) %>% 
    set_caption(paste("Caption ", i))
  
  flextable_to_rmd(myft)
  
}
``

I thought that perhaps using flextable::padding() would help, but this seems to only control the padding within the table rows, rather than the spacing between elements on a page.

Note that I've removed some of the required markdown backticks in the code sample above so that it can render correctly on Stackoverflow.

Unwanted padding between caption and top of the table


Solution

  • Add this to your RMD. Don't put it in a code chunk—just place it where you might otherwise free-write.

    <style>
    caption {
      padding-bottom: 0px;
    }
    </style>
    

    Right now the caption is written in p tags, surrounded by caption tags, surrounded by more p tags, all of which have padding, margin, and line-height specs. This gives you the biggest bang for the least effort (IMO).

    enter image description here