3

How can I use a custom pygments syntax highlighter in rmarkdown? With pygments I created a file nice.py:

from pygments.style import Style
from pygments.token import Keyword, Name, Comment, String, Error, \
     Number, Operator, Generic

class niceStyle(Style):
    default_style = ""
    styles = {
        Operator:               "bold #818181",
        Number:                 "bold #0000cf",
        Generic:                "#000000",
        Comment:                'italic #08a542',
        Keyword:                'bold #0000cf',
        Name:                   '#f00',
        Name.Function:          '#0000cf',
        Name.Class:             'bold #0f0',
        String:                 '#ff7f50'
    }

Now I want to use this for syntax highlighting in rmarkdown:

---
output: beamer_presentation
# highlight: nice
---

```{r}
# Comment
f <- function(x = 10, y = "abc") {
  paste0(x, y)
}
f()
```

Maybe I can include this with knitr::knit_hooks$set? or is there another way?

needRhelp
  • 2,948
  • 2
  • 24
  • 48

0 Answers0