更改针织/减价中的代码块颜色 [英] change code block color in knitr/markdown

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

问题描述

我正在Rstudio中处理Markdown文档,该文档将Perl和R进行了比较. 能够做到的是根据所使用的语言使用不同的代码块背景色. 例如

I'm working on a markdown document in Rstudio that compares Perl and R. What I'd like to be able to do is have different code block background colors depending on the language used. For example

R代码块

```{r}
dog <- 1
cat <- 2
dog + cat
```

Perl代码块

```{r, engine='perl'}
$dog = 1;
$cat = 2;
print $dog + $cat;
```

如果使用带有以上代码的knitr生成html文件,则r代码块的背景为纯灰色,而代码块的输出为白色/透明背景.

If you generate an html file using knitr with the above code, the r code block has a solid grey background while the output from the code block has a white/transparent background.

但是,Perl代码块和输出具有白色/透明背景,看起来很混乱.我希望在markdown/knitr中有一种优雅的方法可以做到这一点.

However, the Perl code block and output has a white/transparent background which looks confusing. My hope is that there's an elegant way to do this in markdown/knitr.

推荐答案

根据Yihui的建议,我与Rstudio支持进行了交谈.他们指出,从本质上讲,我可以告诉R使用下面的R代码使用我自己的样式表:

I talked to Rstudio support as per Yihui's suggestion. They pointed out that I could essentially tell R to use my own style sheet with the following R code:

options(rstudio.markdownToHTML = 
function(inputFile, outputFile) {      
require(markdown)
markdownToHTML(inputFile, outputFile, stylesheet='custom.css')   
}
)

'custom.css'必须在您的工作目录中.我下载了R studio的CSS表单(链接)进行查找修改部分.在样式表中有一个代码块

'custom.css' must be in your working directory. I downloaded R studio's CSS sheet (link) to look for a section to modify. Within the style sheet there's a block of code

code.r, code.cpp {   background-color: #F8F8F8;}

正如Yihui指出的那样,这将仅支持R和C ++的颜色编码块.对以下内容的快速更改包括perl: code.r,code.cpp,code.perl {background-color:#F8F8F8;} 或通过在code.r背景块下面添加以下内容来改变颜色.

As Yihui pointed out this would only support color coded blocks for R and C++. A quick change to the following includes perl: code.r, code.cpp, code.perl { background-color: #F8F8F8;} Or make a different color by adding the following below the code.r background block.

code.perl {
background-color: #B53389;
}

这篇关于更改针织/减价中的代码块颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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