2012-09-02 44 views
10

恐怕會出現這樣的反應:「Markdown的意思很簡單,它不會那麼做」,但它幾乎從不會傷害到問。打印時保留R 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) 
``` 

我推RStudio的「針織HTML」按鈕,打開瀏覽器或Safari HTML和不存在任何問題。如果我從瀏覽器的HTML中打印,則所有的語法突出顯示都會丟失。

+0

你可以給你的工作流程的一個最小的可重複的例子? – mnel

+0

什麼樣的R降價? knitr? –

+0

有沒有另一種使用R Markdown的方法?請詳細說明。 –

回答

9

後做您的「編織」到原來的example.Rmd你會在你的工作路徑的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") 
+1

+1擊敗了我,所以我剛剛合併了我的答案和你的答案。 – mnel