打印时保持R Markdown语法高亮显示

Zoë*_*ark 11 markdown r rstudio knitr

我担心会有这样的反应:"Markdown本来就很简单而且不会这样做",但它(几乎)从来都不会伤害.

在编写R Markdown文档时,我可以在浏览器中查看HTML文件,它看起来很棒.当我尝试以纸张或PDF格式打印时,会打印图中的颜色,但不会显示语法高亮显示.有没有办法在打印时保持语法高亮?

例:

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)
```
Run Code Online (Sandbox Code Playgroud)

我按下RStudio中的"Knit HTML"按钮并在Chrome或Safari中打开HTML,没有任何问题.如果我从浏览器中打印HTML,则所有语法突出显示都将丢失.

The*_*ell 9

在对原始示例进行"编织"后,您将在工作路径中使用example.md,然后使用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")
Run Code Online (Sandbox Code Playgroud)