I can suggest two approaches to do this, use whatever suits you!
Option 01
Code chunk outputs are wrapped inside the verbatim
environment. So to change the font size for a single code chunk, one option could be redefining the verbatim
environment to have a smaller font size just before that code chunk and then again redefining the verbatim
environment to get the default font size for later code chunk outputs.
---
title: "Untitled"
format: pdf
---
\let\oldvrbtm\verbatim
\let\endoldvrbtm\endverbatim
<!-- % redefine the verbatim environment -->
\renewenvironment{verbatim}{\tiny\oldvrbtm}{\endoldvrbtm}
```{python}
#|echo: false
#|result: 'asis'
import pandas as pd
df = pd.DataFrame({'A': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'],
'B': ['one', 'one', 'two', 'two', 'one', 'one'],
'C': ['dull', 'dull', 'shiny', 'shiny', 'dull', 'dull'],
'D': [1, 3, 2, 5, 4, 1]})
print(df)
```
<!-- % redefine the environment back to normal -->
\renewenvironment{verbatim}{\oldvrbtm}{\endoldvrbtm}
```{python}
#|echo: false
#|result: 'asis'
print(df)
```
Option 02
This idea actually is actually taken from this answer on TeXStackExchange. Here A command is defined to control the verbatim
font size. So change the font sizes as needed.
---
title: "Untitled"
format: pdf
include-in-header:
text: |
\makeatletter
\newcommand{\verbatimfont}[1]{\renewcommand{\verbatim@font}{\ttfamily#1}}
\makeatother
---
\verbatimfont{\tiny}
```{python}
#|echo: false
#|result: 'asis'
import pandas as pd
df = pd.DataFrame({'A': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'],
'B': ['one', 'one', 'two', 'two', 'one', 'one'],
'C': ['dull', 'dull', 'shiny', 'shiny', 'dull', 'dull'],
'D': [1, 3, 2, 5, 4, 1]})
print(df)
```
\verbatimfont{\normalsize}
```{python}
#|echo: false
#|result: 'asis'
print(df)
```
Note: The predefined font sizes that you can use in both of the above options are, \Huge
, \huge
, \LARGE
, \Large
, \large
, \normalsize
, \small
, \footnotesize
, \scriptsize
, \tiny
