xaringan: Changing code background for specific chunks

  • A+
Category:Languages

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?

xaringan: Changing code background for specific chunks

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.

 


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: xaringan: Changing code background for specific chunks

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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: