Update (2017-11-11)
For an alternative method for performing syntax highlighting using the CodeMirror Javascript text editor rather than the highr package (which is described below), see my blog post at https://calex.org/blog/r-markdown-syntax-highlighting.


Thanks to Yihui, I now know that R Markdown utilizes the highlighting-kate syntax highlighter. The list of languages that are supported can be found here.

Update (2017-11-11)
Currently, pandoc utilizes the skylighting Haskell library for highlighting.

If I understand correctly, there is a three step process to take an Rmd file and produce an HTML file. Forgive the rough pseudo code.

md <- Rmd %>% knitr
html <- md %>% rmarkdown %>% pandoc

SAS Syntax Highlighting

Unfortunately, I wanted SAS highlighting to work within R Markdown. This would require me submitting a pull request to highlighting-kate for SAS highlighting. Rather than creating a syntax file, I set about figuring out how to make use of knitr and the highr package to get the highlighting I was after.

Below is an example of highlighting SAS code within an R Markdown document. I thought you could simply use the {r engine = 'sas', eval = FALSE} chunk options to highlight SAS code. Unfortunately this does not work as the highlighting is being done by pandoc and not knitr.

Instead, I created a source hook to appropriately highlight and display the SAS code. The SAS code is put through the highlight syntax highlighting program via the highr package. The resulting SAS + CSS code is wrapped in the appropriate html tags using the source hook, which produces HTML within the R Markdown document of the form below.

<div class="sourceCode">
  <pre class="sourceCode">
    <code class="sourceCode">
    ** SAS code + CSS goes here **

Source Hook

Below is the actual source hook needed. It is based on an example from Ramnath Vaidyanathan and tons of trial and error.

The source hook actually works with other languages as well. See the Alternative Syntax Highlighting section below.

knitr::knit_hooks$set(source = function(x, options) {
  if (!is.null(options$hilang)) {
      code_open <- "\n\n<div class=\"sourceCode\">\n<pre class=\"sourceCode\">\n<code class=\"sourceCode\">"
      code_close <- "\n</code>\n</pre>\n</div>\n"
      code_body <- highr::hi_andre(x, language = options$hilang, format = "html")
      knitr:::indent_block(paste(code_body, collapse = '\n'), ""),
  } else {
    stringr::str_c("\n\n```", tolower(options$engine), "\n",
                   paste(x, collapse = '\n'), "\n```\n\n")

When you want to actually highlight SAS code, you need utilize the eval = FALSE and hilang = 'sas' options rather than utilizing the engine = 'sas' option.

For example, if you add the following to your R Markdown document

```{r eval = FALSE, hilang = 'sas'}
data _null_;
  x = 1;
  /* comment */

then the result would be the following.

data _null_;
  x = 1;
  /* comment */

Note: in order to display the backticks and chunk options, I followed the article by Jenny Bryan at the R Markdown site.

Alternative Syntax Highlighting

The source hook allows for highlighting any source code that the highlight program supports. A full list can be found here.

In order to utilize the source hook, the syntax name passed to the hilang chunk option must match the name needed for the highlight program. A full list of specific names can be found at the webpage above or by running highlight --list-langs from the command line.

As another example, below is code taken from the Go language’s home page.

package main

import "fmt"

func main() {
    fmt.Println("Hello, 世界")


highlight --print-style --style-outfile=highlight.css