Code chunk font size in Beamer with knitr and latex
Asked Answered
D

2

17

I am trying get some R code to fit on my beamer slides. It does not seem possible to change the font size via the size argument for the code chunk as you might do for other knitr type documents. The only way seems to be with \footnotesize before every code chunk. This gets frustrating, as I have lots of code chunks and in many cases I then have to use \normalsize after for my LaTeX bullet points.

---
title: "Untitled"
output:
 beamer_presentation:
  includes:
   in_header: header.txt
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, size = "footnotesize")
```

## R Markdown

```{r}
summary(cars)
```

\footnotesize
```{r}
summary(cars)
```

enter image description here

In my header.txt (below) I have experimented with a couple of bits of code from http://yihui.name/knitr/demo/beamer/ but with no luck.

\ifdefined\knitrout
\renewenvironment{knitrout}{\begin{footnotesize}}{\end{footnotesize}}
\else
\fi

\makeatletter
\let\oldalltt\alltt
\def\alltt{\@ifnextchar[\alltt@i \alltt@ii}
\def\alltt@i[#1]{\oldalltt[#1]\footnotesize}
\def\alltt@ii{\oldalltt\footnotesize}
\makeatother

... but really out my depth with \def.

Doubleteam answered 12/7, 2016 at 8:12 Comment(0)
H
7

Drawing on this tex.SE answer, we could redefine the Shaded environment that surrounds R code to make it footnotesize (and the verbatim environment for output). Add this to your header.txt:

%% change fontsize of R code
\let\oldShaded\Shaded
\let\endoldShaded\endShaded
\renewenvironment{Shaded}{\footnotesize\oldShaded}{\endoldShaded}

%% change fontsize of output
\let\oldverbatim\verbatim
\let\endoldverbatim\endverbatim
\renewenvironment{verbatim}{\footnotesize\oldverbatim}{\endoldverbatim}
Hylomorphism answered 12/7, 2016 at 9:26 Comment(5)
many thanks. as far as I am seeing this is only working on the R code (in the shaded area) and not the R output. is there way to cover both?Doubleteam
@gjabel we'll need to redefine verbatim as well, see updated answerHylomorphism
awesome. thanks. also found that if I switch knitr::opts_chunk$set(collapse = TRUE) I do not need to worry about your verbatim fix (which has knock on effects when used elsewhere in the slides) as the R output is then also in the shaded area.Doubleteam
Doesn't seem to work in my case. I get: LaTeX Error: Environment Shaded undefined. Any ideas?Duckweed
this also works for Code blocks in pandoc markdown to latex/pdf conversionCutup
G
1

Following @Martin Schmelzer's output, you can change code font size and text font size independently and for the whole document by adding this to you rmarkdown file:

def.chunk.hook  <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
  x <- def.chunk.hook(x, options)
  paste0("\n \\", "footnotesize","\n\n", x, "\n\n \\normalsize")
})

Out of this code snippet, you only have to change the "footnotesize" and "normalsize" parameters to whatever font size you want; the first being the code and output font size and the second being the text font size.

For instance, with code as "tiny" and text as "normalsize":

---
output: beamer_presentation
---

```{r setup, include=FALSE}
def.chunk.hook  <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
  x <- def.chunk.hook(x, options)
  paste0("\n \\", "tiny","\n\n", x, "\n\n \\normalsize")
})
```

# Section 1
```{r}
summary(cars)
```
Text.

# Section 2
```{r}
summary(cars)
```
This works for every chunks.

Gives this: Example

Granny answered 18/11, 2021 at 10:21 Comment(1)
This works in most cases, unless you're trying to use code chunks as part of an incremental list, and have the list elements revealed with code chunks sequentially (in which case, I would use the @Hylomorphism approach above https://mcmap.net/q/323096/-code-chunk-font-size-in-beamer-with-knitr-and-latex)Papyraceous

© 2022 - 2024 — McMap. All rights reserved.