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.

guyabel
  • 8,014
  • 6
  • 57
  • 86

2 Answers2

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}
Community
  • 1
  • 1
scoa
  • 19,359
  • 5
  • 65
  • 80
  • 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? – guyabel Jul 14 '16 at 05:23
  • @gjabel we'll need to redefine `verbatim` as well, see updated answer – scoa Jul 14 '16 at 08:54
  • 1
    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. – guyabel Jul 14 '16 at 10:44
  • Doesn't seem to work in my case. I get: `LaTeX Error: Environment Shaded undefined.` Any ideas? – Philip Leifeld Oct 20 '19 at 13:57
  • this also works for Code blocks in pandoc markdown to latex/pdf conversion – Ian Turton May 11 '20 at 10:05
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

Maël
  • 45,206
  • 3
  • 29
  • 67
  • 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 @scoa approach above https://stackoverflow.com/a/38324868/4191485) – S. Robinson Aug 21 '23 at 22:12