Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set code background colour in R markdown to PDF

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?

like image 824
Ben Avatar asked Jun 24 '13 06:06

Ben


2 Answers

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.

like image 150
Marius Avatar answered Sep 19 '22 02:09

Marius


Marius' answer is exactly what I was after. Since comments can't take images, I'm pasting a few screenshots here in case others are curious about this.

To get code backgrounds in the PDF generated from the md, I adjusted my code like so:

# 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"))

In testing the seven pandoc highlighting options, I found that only three give code backgrounds. Here are screenshots of the PDFs generated with each of the three options, for future reference.

This is tango, the one that best matches what I'm after with the light grey:

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

like image 25
Ben Avatar answered Sep 22 '22 02:09

Ben