1

Consider the following quarto document:

---
title: "Untitled"
format: pdf
---

```{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)
```

How to scale down the output of the python chunk, say, to 50%. Is that possible?

shafee
  • 15,566
  • 3
  • 19
  • 47
PaulS
  • 21,159
  • 2
  • 9
  • 26
  • 1
    Does this [answer](https://stackoverflow.com/a/75894967/10858321) solve your problem? – shafee Jun 02 '23 at 15:02
  • Thanks, @shafee! Maybe: I do not know how to use the hook in my case. – PaulS Jun 02 '23 at 15:24
  • 1
    Oh! No I was wrong to suggest that answer because that would decrease the font size for both code chunk and output. Anyway, do you want to decrease the font size just for one case or for every output in the document? – shafee Jun 02 '23 at 17:12
  • Thanks, @shafee! I am needing to change the size of the output of a single python code chunk. – PaulS Jun 02 '23 at 17:17

1 Answers1

1

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


reduced font size for a single code chunk

shafee
  • 15,566
  • 3
  • 19
  • 47