Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping R Markdown syntax highlighting when printing

I'm afraid the response will be: "Markdown is meant to be simple and it doesn't do that", but it (almost) never hurts to ask.

When writing an R Markdown document I can view the HTML file in a browser and it looks great. When I try to print it, either on paper or as PDF, the color in the figures is printed but not the syntax highlighting. Is there a way to maintain syntax highlighting when printing?

Example:

Minimal Example
=====

This text looks great in the file and the plot prints in color, but see commented code     below.

```{r}
# this commented line will be green in the HTML file, but will be black when I print it
z <- cor(mtcars) 
require(lattice) # 'require' will be blue in the HTML file, but will be black when I print it
levelplot(z)
```

I push the "Knit HTML" button in RStudio and open the HTML in Chrome or Safari and there are no problems. If I print from the HTML from the browser all the syntax highlighting is lost.

like image 686
Zoë Clark Avatar asked Sep 02 '12 23:09

Zoë Clark


1 Answers

After doing your 'Knit' to your original example.Rmd you will have an example.md in your working path, then use pandoc...

# for pdf (you need to have latex installed)
system( "pandoc example.md -o example.pdf")

# for syntax-highlight persistant html
system("pandoc example.md -o example.html -s -S")
like image 181
Thell Avatar answered Oct 12 '22 14:10

Thell