Search code examples
rlatexquarto

How can I change the line spacing in Quarto table in R, when rendering to PDF?


I am creating a Quarto book, which I plan to render to a PDF. The document class is scrbook. In this book, I want to keep double spacing in the text, but single spacing in the tables. How can I do this? The YAML specification in the project's _quarto.yml file are as follows:

project:
  type: book

book:
  title: "Book Title"
  author: "Eva"
  date: today
  chapters:
    - index.qmd
format:
  pdf:
    number-sections: true
    number-depth: 3
    documentclass: scrbook
    pdf-engine: lualatex
    geometry:
      - top=1in
      - bottom=1in
      - left=1in
      - right=1in
      - heightrounded
    linestretch: 2
    keep-md: true
    keep-tex: true

The linestretch: 2 ensures that there is double spacing in the output (which is needed by reviewer to write comments inside printout of the book). But it also impacts the tables, which are generated from code chunks. So, I tried to apply linestretch:1 to the code chunk, but it has no effect.

{r}
#| echo: false
#| label: tbl-cars
#| tbl-cap: "Details of Cars"
#| linestretch: 1
head(mtcars) |> knitr::kable()

The rendered PDF still has line spacing of 2 in the table generated, even though I want to keep it to single spacing. Is this possible to do? And if it is possible, how can I do it?


Solution

  • I would try using kableExtra::kbl() and add \renewcommand{\arraystretch}{1} somewhere in your document.

    \renewcommand{\arraystretch}{1}
    
    ```{r}
    #| echo: false
    #| label: tbl-cars
    #| tbl-cap: "Details of Cars"
    head(mtcars) |> kableExtra::kbl(booktabs = TRUE)
    ```