将R markdown中的代码背景颜色设置为PDF [英] Set code background colour in R markdown to PDF

查看:403
本文介绍了将R markdown中的代码背景颜色设置为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用RStudio和knitrRnw文件转换为PDF时,代码块具有令人愉悦的灰色背景色(就像它们在SO处所做的一样).当我将md文件转换为PDF时,背景颜色为白色,并且似乎无法更改它.编织代码修饰选项 background仅适用于LaTeX,在md中无效.

如何为从md文件获得的PDF中的代码块设置背景色?我在一些相关的问题中看到编辑markdown.css文件可能是相关的,但是我不确定在这里是否适用,因为在mdPDF文件之间没有html文件.

可以使用编织示例来重现此效果: Rnw来源:knitr-minimal.Rnw markdown源:001-minimal.Rmd .

要将Rnw转换为PDF,只需在RStudio中单击编译PDF"按钮.这是将md转换为PDF的方法:

# Load packages
require(knitr)
require(markdown)

setwd("C:/Users/.../Desktop/")

# Process .md and .pdf files
filen <- "myfile"
knit(paste0(filen,".md"))
system(paste0("pandoc -s ", paste0(filen,"-out.md"), " -t latex -o ", paste0(filen,".pdf")))

还有另一种方法可以将md转换为PDF以便获得彩色代码背景吗?

解决方案

由于已经使用Pandoc进行了设置,因此应该可以使用Pandoc的--highlight-style参数来实现此目的.从文档:

-highlight-style = STYLE指定要在突出显示的源代码中使用的着色样式.选项是pygments(默认设置),kate, 单色,浓缩咖啡,zenburn,黑线鳕和探戈.

如果您未指定markdown文件中每个代码块所包含的语言,则可能还必须设置--indented-code-classes参数:

-indented-code-classes = CLASSES指定用于缩进的代码块的类,例如perl,numberLines或haskell.多班 可以用空格或逗号分隔.

从内存来看,我认为这可能需要像fancyvrb这样的乳胶程序包,因此您可能必须先安装该程序.

When an Rnw file is converted to PDF with RStudio and knitr, the code blocks have a pleasant grey background colour (like they do here at SO). When I convert a md file to PDF, the background colour is white and I can't seem to change it. The knitr code decoration option background is only for LaTeX and has no effect in md.

How can I set a background colour for code blocks in the PDF I get from my md file? I see in some related questions that editing the markdown.css file might be relevant, but I'm not sure if that applies here since there's no html file in between the md and PDF files.

This effect can be reproduced using the knitr examples: Rnw source: knitr-minimal.Rnw and markdown source: 001-minimal.Rmd.

To convert Rnw to PDF I just click the 'compile PDF' button in RStudio. Here's what I do to convert md to PDF:

# Load packages
require(knitr)
require(markdown)

setwd("C:/Users/.../Desktop/")

# Process .md and .pdf files
filen <- "myfile"
knit(paste0(filen,".md"))
system(paste0("pandoc -s ", paste0(filen,"-out.md"), " -t latex -o ", paste0(filen,".pdf")))

Is there another way to convert md to PDF so I can get a coloured code background?

解决方案

Since you're already set up with Pandoc, you should be able to achieve this using Pandoc's --highlight-style argument. From the docs:

--highlight-style=STYLE Specifies the coloring style to be used in highlighted source code. Options are pygments (the default), kate, monochrome, espresso, zenburn, haddock, and tango.

If you're not specifying the language that each code block contains within the markdown file, you might also have to set the --indented-code-classes argument:

--indented-code-classes=CLASSES Specify classes to use for indented code blocks–for example, perl,numberLines or haskell. Multiple classes may be separated by spaces or commas.

From memory, I think this might require a latex package like fancyvrb, so you might have to install that before it works.

这篇关于将R markdown中的代码背景颜色设置为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆