Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code chunk font size in Rmarkdown with knitr and latex

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.

like image 559
Carlos Cinelli Avatar asked Sep 03 '14 13:09

Carlos Cinelli


People also ask

How do I change the font size in R markdown?

To change the font size, you don't need to know a lot of html for this. Open the html output with notepad ++. Control F search for "font-size". You should see a section with font sizes for the headers (h1, h2, h3,...).

When using R markdown and knitr How do you indicate the height and width of a plot created in a code chunk?

When using knitr, how do you denote the height an width of a plot created in a code chunk ? Answer : Set the 'fig. height' and 'fig. width' options for the code chunk.

Can I use LaTeX code in R markdown?

By default, Pandoc will preserve raw LaTeX code in Markdown documents when converting the document to LaTeX, so you can use LaTeX commands or environments in Markdown.


1 Answers

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.

like image 156
Martin Schmelzer Avatar answered Sep 22 '22 18:09

Martin Schmelzer