Henry Scharf
Henry Scharf

Reputation: 11

custom syntax highlighting rmd

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...

Upvotes: 1

Views: 895

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30114

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.

Upvotes: 2

Related Questions