Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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")
```
 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")
```
 

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading