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)
```
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
.
Available font sizes are 8pt, 9pt, 10pt, 11pt, 12pt, 14pt, 17pt, 20pt. Default font size is 11pt (which corresponds to 22pt at the full screen mode).
A code chunk is a runable piece of R code. Re-producing the document will re-run calculations. Code chunk technology is beneficial because the risk of mismatch between the commentary in a paper and the results being discussed is reduced.
You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).
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:
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With