Search code examples
rlatexr-markdownquarto

Quarto PDF output: code block line spacing


This Github repo, hosts a .qmd file of my dissertation template. In config/preamble.tex I've set \onehalfspacing and \linespread{1.5} which I thought would affect only plain text and not also code blocks.

code block

Is it possible to change monofont spacing individually (or inversely, set space for mainfont only)?


Solution

  • More quarto way to do this actually modifying the knitr chunk hook.

    ---
    title: ""
    format:
        pdf:
          include-in-header:
            text: |
              \usepackage{lipsum}
              \usepackage{setspace}
              \onehalfspacing
              \linespread{2}
          df-print: kable
          highlight-style: zenburn
    fontsize: 12pt
    geometry: margin=1in
    ---
    
    ```{r}
    #| label: setup
    #| include: false
    
    
    chunk_hook  <- knitr::knit_hooks$get("chunk")
    knitr::knit_hooks$set(chunk = function(x, options) {
      x <- chunk_hook(x, options)
      paste0("\\linespread{0.5}\n", x, "\n\n\\linespread{2}")
    })
    
    ```
    
    ## Different linespacing for text and code
    
    When you click the **Render** button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
    
    ```{r}
    library(dplyr, quietly = TRUE)
    
    mtcars %>% 
      group_by(am) %>% 
      summarise(
        disp = mean(disp),
        mpg = mean(mpg)
      )
    ```
    
    \lipsum[1]
    
    

    different linespace for code and text


    Here I have used linespace 0.5 for code and linespace 2 for text. Change these as you need.