Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 253
boredgirl Avatar asked Jun 11 '26 16:06

boredgirl


2 Answers

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.

like image 106
Maël Avatar answered Jun 14 '26 08:06

Maël


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


like image 22
Shafee Avatar answered Jun 14 '26 09:06

Shafee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!