63

In knitr, the size option works fine in a .Rnw file, the following code generates:

\documentclass{article}

\begin{document}

<<chunk1, size="huge">>=
summary(mtcars)
@


\end{document}

rnw

However, I can't get it to work in Rmarkdown. The following code does not change the font size, as it did in .rnw file. The same thing happens when trying to set options with opts_chunk$set(size="huge").

Is this the expected behavior? How does one change the chunk code font size? (I mean using knitr options, not by adding \huge before the code)

---
title: "Untitled"
output: pdf_document
---

```{r, size="huge"}
summary(mtcars)
```

enter image description here

I am using RStudio Version 0.98.987, knitr 1.6 and rmarkdown 0.2.68.

2
  • 2
    Unfortunately the size option only works for Rnw. For R Markdown (v2), I think the only way is to redefine the code environments for Pandoc/LaTeX (see the section "Includes" at rmarkdown.rstudio.com/pdf_document_format.html).
    – Yihui Xie
    Commented Sep 3, 2014 at 16:59
  • 6
    Personally I'd rather redefine environments in LaTeX instead of using hooks, because the latter approach may not be portable, e.g. you may use a chunk hook to add \begin{huge} and \end{huge} around a chunk, but then you will be stuck in LaTeX unless you do not want other document formats such as HTML, Word, ... A side comment: tweaking styles is like this kitten bothering you: i.imgur.com/17B4U.gif It is tempting to tweak the style while you work, and it is very distractive and endless work as well :)
    – Yihui Xie
    Commented Sep 3, 2014 at 20:35

5 Answers 5

44

Picking up the idea to alter a knitr hook we can do the following:

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

This snippet modifies the default chunk hook. It simply checks if the chunk option size is not equal to its default (normalsize) and if so, prepends the value of options$size to the output of the code chunk (including the source!) and appends \\normalsize in order to switch back.

So if you would add size="tiny" to a chunk, then all the output generated by this chunk will be printed that way.

All you have to do is to include this snippet at the beginning of your document.

6
  • 2
    This is a great answer!
    – jay.sf
    Commented Oct 26, 2017 at 0:11
  • 3
    @MartinSchmelzer: Afaik, your answer works for LaTex output only. Would you know of any handy solution which works for both HTML and LaTex?
    – mavericks
    Commented Sep 13, 2019 at 6:57
  • Great answer indeed. It worked flawlessly on my local machine but failed on a server. Solution on the server was to add an extra \n as discribed by YihuI here. I have made an edit to the answer to include this.
    – Ege Rubak
    Commented Oct 11, 2019 at 12:08
  • This is a great answer indeed if you want to a) have a LaTeX file with a lot of \small and \normalsize switches along the whole document, and b) if you want EVERY chunk to have the same size. I am using Bookdown's example environments and I don't want those to be \small. It's possible to setup this "by type of chunk" in some way?
    – El Andi
    Commented Sep 27, 2020 at 22:08
  • I think this answer, redefining the aspect of the environment is better for LaTeX, and this one, specifying it using CSS, is better for html output.
    – El Andi
    Commented Sep 27, 2020 at 22:19
34
\tiny

```{r}
summary(mtcars)
```
\normalsize

available options for size in descending order are:
Huge > huge > LARGE > Large > large > normalsize > small > footnotesize > scriptsize > tiny

1
  • This obliterated the use of figure references for me.
    – usεr11852
    Commented Dec 12, 2022 at 12:00
18

Per this Gist, you have to define the font size using css:

<style type="text/css">
body, td {
   font-size: 14px;
}
code.r{
  font-size: 20px;
}
pre {
  font-size: 20px
}
</style>

code.r will control the font size for R code echoed from the code chunk, while pre will apply to any R results output from the code.

A complete working .Rmd file might look like:

---
title: "FontTest"
author: "Thomas Hopper"
date: "January 13,2016"
output: html_document
---

<style type="text/css">

body, td {
   font-size: 14px;
}
code.r{
  font-size: 20px;
}
pre {
  font-size: 20px
}
</style>

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

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

The resulting html renders as:

screenshot showing 20pt R code and 20pt R output

3
  • 15
    I believe that the original poster asks for font size change in pdf output, while your example changes the font size for html output.
    – jarauh
    Commented Dec 2, 2016 at 14:41
  • 2
    However, trying to google a solution to change font size in html output I ended here. Therefore, the answer is useful - although it wasn't for the OP.
    – Pere
    Commented Dec 23, 2019 at 14:45
  • @Tom: very useful, but when I try it I find that 'per' applies to both code and R output, with 'code.r' being unresponsive. Also, is there a way of doing this for a specific chunk? Commented Mar 30, 2020 at 17:20
9

You can define you own document format by exporting something based on the following function from your package my_package:

my_report <- function(...) {

  fmt <- rmarkdown::pdf_document(...)

  fmt$knitr$knit_hooks$size = function(before, options, envir) {
    if (before) return(paste0("\n \\", options$size, "\n\n"))
    else return("\n\n \\normalsize \n")
  }

  return(fmt)
}

This will define a knitr chunk hook size that will put the appropriate latex command before the chunk, and \normalsize after the chunk.

Anyway, with the following R markdown you can check if it's working:

---
output: my_package::my_report
---

Test text for comparison

```{r}
print(1)
```
The next code chunk has `size = 'tiny'` in the chunk options.

```{r, size = 'tiny'}
print(1)
```

I get the following result from `markdown::render():

enter image description here

See also the issue I opened on github:

https://github.com/yihui/knitr/issues/1296

1

Following up on @Martin Schmelzer's output, here's a solution in the case you want to change the code and output default font size for the whole document, but not the size of the text.

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

For instance,

---
output: pdf_document
---

```{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 \\", "footnotesize","\n\n", x, "\n\n \\Huge")
})
```

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

# Section 2
```{r}
print(1)
```
This works for every chunks.

gives this: Example

1
  • 1
    Just what the doctor ordered! Thanks.
    – PatrickT
    Commented Feb 10, 2022 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.