How to change rmarkdown chunk background color when knitting to PDF

Viewed 595

I am preparing a manuscript for publication and need a way to change (remove actually) the background color of code chunks in my PDF file (generated through Latex).

Reproducible example:

---
title: TestFile

output:

  pdf_document: null
 
---

```{r,echo=TRUE}

test=c(1,2,3) print(test)

```


```{r,echo=TRUE}
test=c(1,2,3)
print(test)
2 Answers

I think I've found a solution. This is a purely latex solution, but it does the trick for what I need (which is outputting to PDF via latex) using the package xcolor. [PDF output] https://i.stack.imgur.com/yEADg.png

Reproducible Example:

---

title: TestFile

output:
  
pdf_document: null
  
header-includes: \usepackage{xcolor}
 
---

\definecolor{shadecolor}{RGB}{255,255,255}

```{r,echo=TRUE}

test=c(1,2,3)

print(test)

```

Another option, is to use Pandoc Syntax Highlighting styles, which is very simple and has very nice options. Compared to the LaTeX option you used, this is perhaps less flexible, but has the advantage of changing not only the background of code chunks, but also the font colour.

To do this, you only have to add the highlight style you want to the YAML. For example, adding the zenburn highlight:

output:
  pdf_document:
    highlight: zenburn

Will produce this:

enter image description here

Instead of this:

enter image description here

I hope this helps, but let me know if not.

Related