Set code background color in R markdown to PDF

When an Rnw file Rnw converted to PDF using RStudio and knitr , the code blocks have a nice gray background color (for example, they are here in SO). When I convert the md file to PDF , the background color is white and I cannot change it. The knitr background option is for LaTeX only and has no effect in md .

How to set the background color for code blocks in a PDF that I get from my md file? I see in some related questions that editing the markdown.css file may be relevant, but I'm not sure if this is applicable here since there is no html file between the md and PDF files.

This effect can be reproduced using examples of knitr: 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 is what I am doing 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 any other way to convert md to PDF so that I can get a colored code background?

+4
source share
2 answers

Since you are already configured with Pandoc, you can achieve this using the Pandoc argument --highlight-style . From docs :

- highlight-style = STYLE Defines the coloring style that will be used in the selected source code. Parameters are pygments (default), kate, monochrome, espresso, zenburn, haddock and tango.

If you do not specify the language that contains each block of code in the markdown file, you may also need to set the --indented-code-classes argument:

- indent-code-classes = CLASSES Specify the classes to use indented blocks of code, such as perl, numberLines or haskell. Multiple classes can be separated by spaces or commas.

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

+5
source

Marius' answer is exactly what I was. Since comments cannot accept images, I insert a few screenshots here if others are interested in this.

To get the background code in the PDF generated from md , I adjusted my code like this:

 # Load packages require(knitr) require(markdown) setwd("C:/Users/.../Desktop/") # Create .md and .pdf files filen <- "test" knit(paste0(filen,".md")) system(paste0("pandoc -s ", paste0(filen,"-out.md"), " -t latex -o ", paste0(filen,".pdf"), " --highlight-style=tango -S")) 

When testing seven pandoc highlighting pandoc I found that only three give background code characters. Here are screenshots of the PDF files generated with each of the three options for future reference.

This is the tango that best matches what I need with light gray:

enter image description here And this is zenburn: enter image description here And this is espresso enter image description here

+6
source

Source: https://habr.com/ru/post/1487820/


All Articles