【问题标题】:Keeping R Markdown syntax highlighting when printing打印时保持 R Markdown 语法高亮
【发布时间】:2012-08-27 18:10:24
【问题描述】:

恐怕回答会是:“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 中按下“Knit HTML”按钮并在 Chrome 或 Safari 中打开 HTML,没有任何问题。如果我从浏览器中的 HTML 打印,所有语法高亮都会丢失。

【问题讨论】:

  • 您能否举一个您的工作流程的最小可重现示例?
  • 什么样的 R 降价?针织?
  • 还有其他方法可以使用 R Markdown 吗?请详细说明。
  • 剥猫皮的方法有很多。对于 r 降价,您还可以使用 pander
  • knit2HTML 按钮的使用在这里至关重要。问题在于markdown::markdowntoHTML 突出显示的方式

标签: r markdown knitr rstudio


【解决方案1】:

对原始 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")

【讨论】:

    【解决方案2】:

    我还发现了一个解决方案是编织到 HTML,在浏览器中预览 html 文件,然后突出显示所有内容并将其粘贴到 MS Word 文档中。从那里您可以导出为 PDF 或打印。不幸的是,它不会将所有图像复制到 Word 文档中。但是,如果您只需要语法突出显示,而不是太担心图像或绘图,那么这个解决方案相当简单。它实际上比 pandoc 解决方案更好地复制代码框,但缺点是没有图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 2019-11-05
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多