2

I am using quarto to prepare a Beamer presentation. I would like to reduce the font size of the code inside a given chunk so everything fits better. See the example below:

enter image description here

The code that generates the chunk is:

```{python}
# Import packages
import numpy as np
```

```{python}
#| echo: true
w = np.array([0.7, 0.3])
Sigma = np.array([[0.09, 0.1], [0.1, 0.25]])

var_rp = w.T @ Sigma @ w
print("Variance of our example portfolio {:.4f}".format(var_rp))
print("Standard Deviation of our example portfolio {:.2f}".format(np.sqrt(var_rp)))
```

Is there a way to change the font size of this chunk so all lines of code fit in a single line in the pdf? Thanks in advance!

shafee
  • 15,566
  • 3
  • 19
  • 47
Raul Guarini Riva
  • 651
  • 1
  • 10
  • 20

2 Answers2

3

Specify a monofont and scale down the font using monofontoptions. Check out the Quarto Beamer reference docs to know about these font options.

And I have used the font Source Code Pro from the LaTeX Font Catalogue. You may look into there for other monospaced fonts.

---
title: "Small code font"
format: beamer
jupyter: python3
pdf-engine: lualatex
monofont: 'Source Code Pro'
monofontoptions: 
  - Scale=0.55
---

## Portfolio

Let's do this computation in python.....


```{python}
# Import packages
import numpy as np
```

```{python}
#| echo: true
w = np.array([0.7, 0.3])
Sigma = np.array([[0.09, 0.1], [0.1, 0.25]])

var_rp = w.T @ Sigma @ w
print("Variance of our example portfolio {:.4f}".format(var_rp))
print("Standard Deviation of our example portfolio {:.2f}".format(np.sqrt(var_rp)))
```


code font size reduced


Note: This solution will reduce the font size for all code chunks and chunk output. If you want smaller font size for a specific code chunk, check the answer given by samcarter_is_at_topanswers.xyz.

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

You can use \AddToHookNext{env/Highlighting/begin}{\tiny} to change the font size of the next chunk without affecting other chunks:

---
format: beamer
---

```{python}
# Import packages
import numpy as np
```

\AddToHookNext{env/Highlighting/begin}{\tiny}
```{python}
#| echo: true
w = np.array([0.7, 0.3])
Sigma = np.array([[0.09, 0.1], [0.1, 0.25]])

var_rp = w.T @ Sigma @ w
print("Variance of our example portfolio {:.4f}".format(var_rp))
print("Standard Deviation of our example portfolio {:.2f}".format(np.sqrt(var_rp)))
```

enter image description here


(in case you want to change the font size of all chunks, use \AddToHook{env/Highlighting/begin}{\tiny} in your header includes)