-3

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)?

Alberson Miranda
  • 1,248
  • 7
  • 25
  • Your repo has no data folder which giving error. Please consider updating your question by giving a reproducible example (may b a short qmd file with a bit of text and r-code which would be enough for showcasing the linespacing problem) and also make this question useful others too. – shafee Aug 22 '22 at 02:04
  • @shafee my bad. Check updated link with dedicated branch for this issue. – Alberson Miranda Aug 22 '22 at 03:08
  • 2
    you are missing the point of a minimal example. I think you can just simply update your question with a simple `qmd` file with both text and code. That would be really helpful for all of us here. – shafee Aug 22 '22 at 10:35
  • 2
    Please include the relevant code in your question. Posts on SO should be self-sustained so they remain helpful for future users with the same problem even if the link stops working – samcarter_is_at_topanswers.xyz Aug 22 '22 at 11:09

2 Answers2

3

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.

shafee
  • 15,566
  • 3
  • 19
  • 47
1

Code blocks are defined in Shaded environment. So, the fix was simply redefining it in preamble.tex using singlespace environment and \linespread{1}:

\renewenvironment{Shaded}
    {\begin{snugshade}
    \begin{singlespace}
    \linespread{1}
    }
    {\end{singlespace}
    \end{snugshade}
}
shafee
  • 15,566
  • 3
  • 19
  • 47
Alberson Miranda
  • 1,248
  • 7
  • 25