14

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?

3
  • 2
    Perhaps 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: animation.r-forge.r-project.org/html/knitr-highlight-theme.html
    – Yihui Xie
    Jul 10, 2012 at 21:19
  • @Yihui that's one nice list. I think a few themes would be suitable for printing in B&W. Jul 10, 2012 at 22:31
  • 3
    If 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. Jul 15, 2012 at 1:28

3 Answers 3

8
+100

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.)

2
  • 2
    If you hate your readers enough, you could sprinkle a few text-decoration:blinks into the style file. Jul 18, 2012 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, 2013 at 19:45
3

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

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?

4
  • Thanks for the answer. What do you mean by PS or PDF options? Jul 16, 2012 at 13:54
  • 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, 2012 at 17:19
  • I understand what a PS and PDF are, but not how to represent code in them. Jul 16, 2012 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<lang> is superb.)
    – smci
    Jul 19, 2012 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.