boredgirl
boredgirl

Reputation: 109

Setting code chunk size on Quarto pdf output

Any idea how to set a small size for code chunks on Quarto pdf output ? I don't want my code to have the same size of my text, but I can't find an argument in the settings in the begining of my document nor in the R code chunk to specify a font size.

Upvotes: 2

Views: 2666

Answers (3)

Carsten
Carsten

Reputation: 131

You can define the global size of chunk text (i.e. R input and output) using the chunk options of knitr. Here an example of the content of an R-chunk as I put it into the very beginning of the Quarto (or Rmarkdown) document (with other potentially useful options; for example, I want figures to be approximately square, by default):

#| echo: false
library(knitr)
opts_chunk$set(fig.align='center', fig.width=4, fig.height=4.2, 
cache=TRUE, size="small")

The argument "size" accepts the standard LaTeX sizes (as provided in the other answers). Since fixed-width fonts appear larger than variable-width fonts, "small" seems to strike a good balance between readability and space use.

Upvotes: 0

Shafee
Shafee

Reputation: 20037

You can write an knitr-hooks to create a chunk option (suppose size) to control the font size of code chunk (source-code + chunk-output).

Then, we can set, for example, size: scriptsize chunk option for a code-chunk to get a smaller font size than the usual text (which has, by default, font size equals to \normalsize.

---
title: "Small Font Size for Code chunk"
format: pdf
---

## Quarto

```{r}
#| echo: false

default_chunk_hook  <- knitr::knit_hooks$get("chunk")

latex_font_size <- c("Huge", "huge", "LARGE", "Large", 
                     "large", "normalsize", "small", 
                     "footnotesize", "scriptsize", "tiny")

knitr::knit_hooks$set(chunk = function(x, options) {
  x <- default_chunk_hook(x, options)
  if(options$size %in% latex_font_size) {
    paste0("\n \\", options$size, "\n\n", 
      x, 
      "\n\n \\normalsize"
    )
  } else {
    x
  }
})

```

Lorem ipsum dolor sit amet consectetur adipiscing elit, urna consequat felis 
vehicula class ultricies mollis dictumst

```{r}
#| eval: false
#| size: scriptsize

print("This is r code")
print("This is r code")
print("This is r code")
print("And this has samller font size")
```

Vivamus integer non suscipit taciti mus etiam at primis tempor sagittis sit, eui
smod libero facilisi

smaller font size for code chunk


Upvotes: 4

Ma&#235;l
Ma&#235;l

Reputation: 52319

You can do this locally with LaTeX font options:

---
title: "chunk-size"
format: pdf
editor: visual
---

\tiny
```{r}
summary(mtcars$mpg)
```
\normalsize

You can use Huge > huge > LARGE > Large > large > normalsize > small > footnotesize > scriptsize > tiny.

Upvotes: 2

Related Questions