One of the nice things about knitr is that you can easily change the colouring of R code. However, most documents are printed in black and white. So would be a good styling setting for R code when printing documents using a black and white printer?
-
2Perhaps you can try to generate colors using `grDevices::hsv()` holding `s` and `v` constant (with maximum `s` and a moderate `v`) and vary `h`; you will get some colors which will not look too light on b/w printer. I guess there must be some themes which have taken this into account; here is a preview of all themes in knitr: http://animation.r-forge.r-project.org/html/knitr-highlight-theme.html – Yihui Xie Jul 10 '12 at 21:19
-
@Yihui that's one nice list. I think a few themes would be suitable for printing in B&W. – Roman Luštrik Jul 10 '12 at 22:31
-
3If you are going to construct your own css theme, you might also want to play around with font faces in addition to the grey tones you're already thinking about. For example, you could print comments in an italic face with `.comment{font-style: italic;}`, function calls with a bold face, etc. – Josh O'Brien Jul 15 '12 at 01:28
3 Answers
As other commenters have mentioned, you only really have a choice of altering a few shades of grey, plus bold and italics. Here's a stylesheet in loose order of most prominent to least prominent items. Your preference may vary.
.background {
color: #ffffff;
}
.source, .output, .warning, .error, .message {
padding: 0em 1em;
border: solid 1px #f7f7f7;
}
.error, .warning, .message {
font-weight: bolder;
font-style: italic;
color: #000000;
}
.keyword {
font-weight: bolder;
color: #000000;
}
.functioncall, .package {
font-weight: bolder;
color: #202020;
}
.source, .output, .number, .argument, .formalargs, .eqformalargs, .assignement, .symbol, .prompt {
color: #404040;
}
.string {
font-weight: bold;
color: #606060;
}
.comment, .roxygencomment, .slot {
font-style: italic;
color: #808080;
}
The easiest way to make this available is to save as e.g., "knitr/themes/bw.css" in whichever library the knitr package is in. Then you can use it by calling
knit_theme$set("bw")
(Alternately, for a small amount of extra typing, you can provide knit_theme
a path to the CSS file.)

- 118,240
- 47
- 247
- 360
-
2If you hate your readers enough, you could sprinkle a few `text-decoration:blink`s into the style file. – Richie Cotton Jul 18 '12 at 13:50
-
This throws the following warning `Warning in readLines(file) : incomplete final line found on 'C:/Users/R/win-library/3.0/knitr/themes/bw.css' Warning in color_def(foreground, "fgcolor") : the color '' is invalid;using default color...see http://yihui.name/knitr/options`. I'm using `R 3.0.2` and `knitr 1.5.15`. – MYaseen208 Dec 14 '13 at 19:45
There are now grey scale themes within knitr: greyscale0
, greyscale1
and greyscale2
. You can view all knitr themes via:
library("knitr")
knit_theme$get()
To set the theme in a knitr document, add (for example) the line
knit_theme$set("greyscale2")

- 59,189
- 14
- 150
- 185
Frankly I'm not sold on any of those knitr themes for B&W printing, they're all inferior to good old enscript -E<lang>
Here are the criteria I would consider important for legibility on a B&W printout, with italicizing and bolding as well as coloring:
#Comments should be italicized - very important
fn.name.declarations.should.be.heavily.bolded <- function(...) {
"strings should be bolded"
numbers, NA, Nan should be a different color (prints as something dark gray)
Your choice of how to treat variable names
Your choice of how to treat fncalls, builtins
You didn't say it specifically needed to be LaTex, so why not consider outputting PS or PDF format instead?

- 32,567
- 20
- 113
- 146
-
-
**PS**: Postscript, **PDF**: Adobe PDF format. Possible alternatives to LaTex, depending on whether you want to print this, embed it in a document, or whatever. – smci Jul 16 '12 at 17:19
-
I understand what a PS and PDF are, but not how to represent code in them. – csgillespie Jul 16 '12 at 19:24
-
You didn't state your requirements, but it sounds like you want to both a) view the code in GUI and b) pretty-print it. (The original question made it sound like you only wanted legible pretty-printing, in which case *`enscript -E
`* is superb.) – smci Jul 19 '12 at 18:14