custom syntax highlighting rmd

折月煮酒 提交于 2019-12-11 11:34:52

问题


I would like to be able to customize the syntax highlighting used with RMarkdown so that I can, for example, draw attention to functions coming from a certain package.

I noticed an option mentioned in the knitr NEWS document which says this option can be set via:

opts_knit$set(highr.opts = list(markup = cmd_mine))

for .Rnw and .Rhtml documents. However, when I set this option in my .Rmd file (simplified here to messing with the highlight for numerical constants)

```{r setup, include=TRUE}
# set global chunk options
library(knitr)
opts_chunk$set(cache=TRUE)
library(highr)
cmd_mine = highr:::cmd_html
cmd_mine[rownames(cmd_mine)=="NUM_CONST", 1] <- '<span class="three">'
opts_knit$set(highr.opts = list(markup = cmd_mine))
cmd_mine
sum(1:2)
```

I see no change. This leads me to understand that this option is not implemented for .Rmd, or that I've misunderstood how the option works. If it doesn't exist, it'd be great if it could someday! If it does, I'd appreciate any suggestions. Thanks!

PS: in fact, I tried to set this for an .Rhtml document and also had no success...


回答1:


The syntax highlighting in knitr only works for LaTeX (.Rnw) and HTML (.Rhtml). It does not apply to R Markdown (.Rmd).

If it does not work for .Rhtml, it could be a bug. But keep in mind that even if it works, you may not see it. It depends on what style you defined for span.three in CSS. Some screenshots and a minimal reproducible example (plus sessionInfo()) will be helpful.



来源:https://stackoverflow.com/questions/27081354/custom-syntax-highlighting-rmd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!