Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xaringan: Changing code background for specific chunks

In this feature request back in 2016 Yihui shows, at the bottom, how to have different code chunks have different backgrounds in R Markdown. I've attached a screenshot of this here. Is there a way to do this with xaringan?

rmd-solution

I found this answer, which works for changing the background color for all code chunks, but I can't figure out how to modify the css so I can change the background for just a few specific chunks.

The goal here is to have most chunks show up normally, but a few show up with a red background or similar to signify "This is bad". Any help would be appreciated.

like image 747
Daniel Anderson Avatar asked Oct 04 '18 23:10

Daniel Anderson


1 Answers

This was actually something I was trying to work out a few days ago.

Here's one workaround.
First in your css file or either place the following in xaringan Rmarkdown file:

```{css, echo=F}
.code-bg-red .remark-code, .code-bg-red .remark-code * {
 background-color:red!important;
}
```

and then wrap the code chunk like

.code-bg-red[
```{r}
lm(speed ~ dist, cars)
```
]

then your output will be: red chunk output

You can, of course, change the red to other colors to suit your needs.

like image 59
Emi Avatar answered Oct 20 '22 16:10

Emi