I would like to have a certain code chunk highlighted in a different color (e.g. red) to indicate that it is bad practice. If I was using .Rnw
, I could add the chunk option background = 'red'
and get what I want, but this does not seem to work in .Rmd
. My guess is that I need to make a custom css stylesheet (though what the selector would be, I don't know), and maybe also create a custom hook. I'd like it to be on a per-chunk basis, not an overall change for the entire document.

- 787
- 1
- 8
- 9
-
2I have almost asked the same question recently, for different reasons. I want to format a chunk differently depending on whether it is "optional" (i.e. only prints and explores objects) vs. "mission-critical" (i.e. assigns to objects used downstream). I am happy to be the judge of that and explicitly set a chunk option. This would be handy for exposition. – jennybryan Dec 08 '16 at 04:32
5 Answers
We can use the class.source
option in the code chunk header to provide custom CSS to R Markdown. This is explained in the following post:
Add a CSS class to single code chunks in RMarkdown
Putting together an example, I might set a class called "badCode" then have a bit of CSS to change the background as you might like. Here's my .Rmd
:
---
output: html_document
---
```{css}
.badCode {
background-color: red;
}
```
```{r mtcars}
summary(mtcars)
```
```{r cars, class.source="badCode"}
summary(cars)
```

- 14,721
- 2
- 60
- 84

- 986
- 7
- 10
-
Very nice. Could you explain what the second call to `paste0()` is doing? I can't figure out what ````{.r ...}```` (with surrounding triple backticks) does and how it gets processed. – Josh O'Brien Dec 08 '16 at 15:32
-
2You have exposed the difference between getting one thing to work a few times and actually understanding what's going on :) I'll do my best with the latter. I think the `{.r ...}` notation is a signal to pandoc to process these as html classes. The second call to `paste0()` is simply inserting the contents of `options$class` as the `...` in `{.r ...}`. FWIW, my debugging method was to look at the `.md` file (in RStudio, using the "keep md file" option). – Ian Lyttle Dec 08 '16 at 20:33
-
1Ahah -- that helps a lot. Looks like it's a combination of the Pandoc markdown extensions `backtick_code_blocks` and `fenced_code_attributes` which can be used to insert all sorts of attributes in a wrapping `` tag. [See here](http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#fenced-code-blocks) for documentation. Thanks also for the debugging tip. I had tried setting `keep_md: true` in my YAML header, which for some reason didn't work here. Next time I have trouble with that, I will try from RStudio. Thanks for your response! – Josh O'Brien Dec 08 '16 at 20:58
-
-
1I found a post elsewhere with your name on it: https://github.com/yihui/knitr-examples/blob/master/116-html-class.Rmd . I considered posting it as a new answer but I updated yours instead as it felt like I was stealing :) – Michael Harper Oct 06 '18 at 08:22
-
when using `bookdown` and `output: gitbook`, I had to add the `!important` tag to my css attributes to get them to display. For example `background-color: red !important;` – RTbecard Mar 23 '22 at 14:15
Remember markdown supports HTML outside of code blocks.
I would surround the code chunks with a div with a custom class that styled them how I wanted. This example styles the code in blue, the output in light blue
<style>
div.blue pre { background-color:lightblue; }
div.blue pre.r { background-color:blue; }
</style>
<div class = "blue">
```{r bluecars}
summary(cars)
```
</div>
```{r normal}
summary(cars)
```

- 4,687
- 29
- 30
-
This is smart. For some reason I remember having problems wrapping around code chunks. But now that I think about it, that was probably when I tried wrapping//– mkearney Dec 08 '16 at 19:15
around slides using slidify. -
This also works for different language code chunks; I was able to use this for pre.js too. – Nancy Apr 02 '17 at 20:25
This solution is a bit hack-y, but it works. The gist of it is to make two code chunks, swapping the {r} designator with a unique class name. Then add css code to style each chunk.
---
output: html_document
---
<style>
pre.bluecars {
background-color: #aabbff !important;
}
pre.redcars {
background-color: #ffbbbb !important;
}
</style>
## chunk-specific bg colors
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
- blue
```{bluecars}
summary(cars)
```
```{r, echo=FALSE}
summary(cars)
```
- normal
```{r}
summary(cars)
```
- red
```{redcars}
summary(cars)
```
```{r, echo=FALSE}
summary(cars)
```

- 1,266
- 10
- 18
I found the following way to color (code) chunks in Rmarkdown documents that I knit to PDF:
Use a pandoc highlighting scheme as a basis
pandoc --print-highlight-style pygments > my.theme
Then edit my.theme by using a regex to set all
"text-color": null
Then, you can change this property to any color you like (this one here is lightgrey)
"background-color": "#f8f8f8"
In the YAML of your .Rmd document under "pdf_document", put the following
pandoc_args: --highlight-style=my.theme
For my use case this is all I need.

- 344
- 2
- 12
-
To run the first command, you'd need a unix terminal and must have pandoc installed in your machine. This helped in my case, I can change the `text-color` to whatever I want + the nice grey background for PDF files. – domin thomas Mar 18 '20 at 15:17
you can also use following examples:
```{css, echo = F}
h1 { color: rgb(255, 125, 0); }
h2 { color: brown; }
div.yellow pre { background-color: lightyellow; }
div.yellow pre.r { background-color: lightblue; }
.my_pink1 { background-color: pink; }
.my_pink2 { background-color: rgb(255, 113, 181); color: white; border: 3px solid blue; }
.my_span { background-color: orange; font-family: courier}
```
# Title 1
## Title 2
<div class = "yellow">
```{r yellow_cars1}
summary(cars)
```
</div>
```{r yellow_cars2, class.source = "my_pink1", class.output = "my_pink2"}
summary(cars)
```
Test: <span class = "my_span">change properties</span> \
Roses are $\color{red}{\text{beautiful red}}$,
violets are $\color{blue}{\text{lovely blue}}$.
read more e.g. at
Changing chunk background color in RMarkdown
https://bookdown.org/yihui/rmarkdown-cookbook/chunk-styling.html
How to change the font color?

- 99
- 5