Reducing space between command and output in RMarkdown / Quarto

Viewed 189

I am just starting to use Quarto in the latest version of Rstudio. However, I'm annoyed by the amount of space added between the command and the output in the R code chunk (rendered as pdf)

```{r}
3 + 1
```

You can add options to executable code like this

```{r}
#| echo: false
2 * 2
```

The `echo: false` option disables the printing of code (only output is displayed).

enter image description here

I am unable to find any references or global code chunk options to reduce this spacing. What is the usual R Markdown code chunk option for this?

2 Answers

You can try this solution.

As you know, Quarto by default uses the KOMA-Script classes.

If you change one of them (by default: scrartcl) to the standard document-class in LaTeX (f.e.: article) - space will be reduced.

format: 
  pdf:
    documentclass: article

enter image description here

Good luck ;-)

You could imagine it as a latex document, so you can use the code from answer in your include-in-header and create a minipage document where you specify the \begin in include-before-body and \end in include-after-body like this:

---
format:
  pdf: 
    include-in-header: 
      text: |
       \usepackage{etoolbox} 
       \makeatletter 
       \preto{\@verbatim}{\topsep=-10pt \partopsep=-10pt } 
       \makeatother
    include-before-body: 
      text: |
       \begin{minipage}{5cm}
    include-after-body: 
      text: |
       \end{minipage}
---

{r}
3 + 1

You can add options to executable code like this

{r}
#| echo: false
2*2

The `echo: false` option disables the printing of code (only output is displayed).

Output:

enter image description here

Original output with your code:

enter image description here

It is a bit hard to see maybe, but the whitespace is less.

Related