Syntax highlighting for Python chunks does not work
Asked Answered
K

1

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

Keri answered 5/1, 2014 at 4:56 Comment(0)
T
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.

Tsuda answered 5/1, 2014 at 6:7 Comment(4)
Thanks! Pandoc did the trick. Now I need to learn how it works to get the result I want :-)Keri
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.Lilli
@Lilli Just use '''py and ''' to include your Python code (replace ' with a backtick, since I cannot type a literal backtick here).Tsuda
@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 geoscripting-wur.github.io/PythonWeek)Lilli

© 2022 - 2024 — McMap. All rights reserved.