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]

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