8

I'm trying to create notebooks in which I present the same algorithm written in R and Python. I can easily integrate the R and Python code in a Rmd file and then create a HTML output using the 'Knit HTML' button in RStudio. However the Python code is always displayed without syntax highlighting (not like in the example at https://github.com/yihui/knitr-examples/blob/master/023-engine-python.md). I have Andre Simon's highlight software installed and functioning on my computer (MacBook Pro Retina, MacOS X 10.9).

Currently, I have the following libraries installed in R: markdown 0.6.4, knitr 1.5.15, highr 0.3.1.

Any idea what could be the problem? Thanks for your help!.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
Simon Garnier
  • 555
  • 1
  • 5
  • 12

1 Answers1

6

That is specific to the RStudio Markdown render, which did not enable syntax highlighting for code blocks that are not R or C++. RStudio uses highlight.js, and you can use your own version of highlight.js to highlight Python code. Or use other Markdown converters such as Pandoc.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • Thanks! Pandoc did the trick. Now I need to learn how it works to get the result I want :-) – Simon Garnier Jan 05 '14 at 18:12
  • Can you add a simple example how to use pandoc to enable the code highlighting for a .Rmd doc containing both R and Python chuncks? I am working with Rstudio and want to enable syntax highlighting for python chuncks. – Janvb Dec 26 '14 at 13:31
  • @Janvb Just use `'''py` and `'''` to include your Python code (replace `'` with a backtick, since I cannot type a literal backtick here). – Yihui Xie Dec 26 '14 at 16:49
  • @Yihui: I solved it by adding the following to the output as defined in the .Rmd in Rstudio: html_document: theme: united toc: true toc_depth: 4 number_sections: true highlight: pygments (see https://geoscripting-wur.github.io/PythonWeek/) – Janvb Dec 26 '14 at 18:08