Code styling for black and white documents Code styling for black and white documents r r

Code styling for black and white documents


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


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


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 importantfn.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 namesYour 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?