在RMarkdown中更改块背景颜色 [英] Changing chunk background color in RMarkdown

查看:102
本文介绍了在RMarkdown中更改块背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用不同的颜色(例如红色)突出显示某个代码块,以表明这是不正确的做法.如果使用的是.Rnw,则可以添加块选项background = 'red'并获得所需的内容,但这在.Rmd中似乎不起作用.我的猜测是,我需要制作一个自定义的CSS样式表(尽管选择器是什么,我不知道),也许还要创建一个自定义的钩子.我希望它是基于每个块的,而不是整个文档的整体更改.

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.

推荐答案

我们可以在代码块标题中使用class.source选项,以向R Markdown提供自定义CSS.在以下帖子中对此进行了解释:

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:

将CSS类添加到单个代码块中RMarkdown

举个例子,我可能会设置一个名为"badCode"的类,然后根据需要使用一些CSS来更改背景.这是我的.Rmd:

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)
```

这篇关于在RMarkdown中更改块背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆