10

I am using Knit PDF to compile a beamer presentation in RStudio.

---
title: "A.P. Statistics"
author: "Notes for Chapter 3.Rmd"
date: "Monday, October 13, 2014"
output: beamer_presentation
---

## Computer Output

```{r}
summary(lm(cars$dist~cars$speed))
```

How can I change the font size (just for this one chunk, leaving other chunks the same font size) so that the output of this command fits on one slide?

Thomas
  • 43,637
  • 12
  • 109
  • 140
David
  • 981
  • 1
  • 15
  • 27

3 Answers3

8

One solution is using knitr hooks. A hook is code that will run before or after the chunk code is executed. You could use it to insert a LaTeX fontsize command in the file.

```{r echo=FALSE}
knitr::knit_hooks$set(mysize = function(before, options, envir) {
  if (before) 
    return(options$size)
})
```

Know you can change the size by

```{r mysize=TRUE, size='\\large'}
1:10
```

One Drawback is that this type of hook will affect all the fonts on a slide, i.e. also the echoed R-Code. Though cumbersome, you could use two consecutive chunks (1st: echo, results no; 2nd: no echo, results yes) to evade this.

```{r results="'hide'}
1:10
```

```{r echo=FALSE, mysize=TRUE, size='\\large'}
1:10
```

PS. Maybe there is a better way by modifying output hooks instead of chunk hooks.

Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88
  • Maybe I'm missing something obvious here, but what is the advantage of using this hook function instead of adding \large "manually" before the chunk (and revoking it after the chunk, if necessary, using \normalsized)? – crsh Apr 20 '16 at 13:50
  • Yes, the way to modify the output hook is simple, as it can be seen in my answer below -- due to formatting restrictions in the comments, I wrote my onw answer to get better legibility – Marcelo Ventura Mar 08 '20 at 19:49
2

I was going to place it as a comment to Mark Heckmann's answer, but the formatting was all screwed.

Actually, if you write your chunk hook like this:

```{r echo=FALSE} 
knitr::knit_hooks$set(mysize = function(before, options, envir) { 
  if (before) { 
    return(options$size) 
  } else { 
    return("\\normalsize") 
  } 
}) 
```

it will do the trick.

In my case, I also included an

knitr::opts_chunk$set(mysize = TRUE, size = "\\tiny")

right below the knitr::knit_hooks$set().

Marcelo Ventura
  • 581
  • 7
  • 19
0

Here's how i do it ...

add the following to your slideStyle.sty file

% set font size to 7 with line breaks at 8
\newcommand\FontSmall{\fontsize{7}{8}\selectfont}

call the file at the top of your markdown:

output: 
  beamer_presentation:
    includes: 
      in_header: "P:/R/Slides/slideStyles.sty"

and then in your .Rmd file add the below

## Tiny font slide

\FontSmall

here is some tiny font ...
ricardo
  • 8,195
  • 7
  • 47
  • 69