Home > Enterprise >  How to reduce line spacing with flextable() in R Markdown to pdf?
How to reduce line spacing with flextable() in R Markdown to pdf?

Time:08-24

When knitting to html, flextable::line_spacing(space = 0) does what is expected, reducing the line spacing. When knitting to pdf, it does not. See code below and screenprint. I am using flextable version 0.7.3. Setting the height has a similar effect, besides some of my real data runs on more lines, so setting an exact row height is not oke. Since I have conditional formatting in my real data, flextable is easier than for instance kableExtra::column_spec(). Anyone an idea how to get the pdf produced with flextable with actual reduced line spacing? Thanks!

Screenprint_code_and_outputs

---
output:
  pdf_document:
    latex_engine: xelatex
  html_document:
    df_print: paged
---

```{r, echo = FALSE, message = FALSE}
library(tidyverse)
cars %>%
  head(10) %>%
  flextable::flextable() %>%
  flextable::line_spacing(space = 0,
                          part = "body",
                          unit = "mm")
```
 

CodePudding user response:

Yes, PDF output does not support line spacing. There is a specific parameter for that, ft.arraystretch (height of each row relative to its default height - only for latex tables):

---
output:
  pdf_document:
    latex_engine: xelatex
  html_document:
    df_print: paged
---

```{r, echo = FALSE, message = FALSE, ft.arraystretch = 1}
library(tidyverse)
cars %>%
  head(10) %>%
  flextable::flextable() %>%
  flextable::line_spacing(space = 0,
                          part = "body",
                          unit = "mm")
```
 

  • Related