[Rd] Off-topic: Pretty-printing R (or S) code with GNU a2ps

Dirk Eddelbuettel edd@debian.org
Fri, 26 Jan 2001 22:37:38 -0600 (CST)


--oP8mQFO4PT
Content-Type: text/plain; charset=us-ascii
Content-Description: message body text
Content-Transfer-Encoding: 7bit


GNU a2ps is a fairly versatile text-to-anything processor.  It is useful for
typsetting source code from a wide variety of programming languages.  As R
(ie S) wasn't supported yet, I quickly cobbled a style sheet for the S
language together. Stylesheets are documented in the texinfo docs for a2ps,
and examples can be found in /usr/share/a2ps/sheets (on Debian at least,
others distros or unices probably have similar locations). IIRC a2ps exists
for NT but might require Cygwin. I typeset on Linux and only print on NT :)

Well, as I still don't really know what I'm doing with R, I would appreciate
comments / feedback / input from anyone could spend a moment or two on this.
To activate the stylesheet, simple patch
      /usr/share/a2ps/sheets/sheets.map
as per the short diff below which declares that the stylesheet s.ssh shall be
used for filenames matching /*.[RS]/.  The sheet itself is attached, just
place it in the same directory, ie /usr/share/a2ps/sheets/.

Usage then simply becomes
      a2ps -Pdisplay foo.R
or
      a2ps foo.R -o foo.ps; gv foo.ps
in case the "display" device isn't defined.

A few points / questions:

 * content:  a2ps uses tricks to use TeX symbols. It works well to typset
   <- with \leftarrow (uncomment that in s.ssh to see) but I couldn't tell
   it to parse
      a<-2
   it only finds <- with whitespace, eg
      a <- 2
   What could be done about <<- ?  Is there a matching TeX symbol?

 * more content: Is the list of keywords really that short? I couldn't find
   much else...  

 * scope:  Are other file extensions in use? Are there conventions for S or
   Splus files?  Any clever way to make a2ps understand the files in 
   $R_HOME/library/$PACKAGE/R  are also R?  Could file(1) be taught what 
   an R file is?

 * editorial: shall this be the s.ssh or r.ssh or rs.ssh? 

Thanks, Dirk


edd@sonny:/tmp/a2ps> cat sheets.map.diff
*** /usr/share/a2ps/sheets/sheets.map   Fri Jan 26 22:13:55 2001
--- sheets.map  Fri Jan 26 22:17:42 2001
***************
*** 373,378 ****
--- 373,381 ----
        <*python script*>
        /*.py/
 
+ # GNU R files, as well as S language files
+ s:    /*.[RS]/
+
  # REXX command files
  rexx: /*.cmd/i
                                                  


--oP8mQFO4PT
Content-Type: application/octet-stream
Content-Disposition: attachment;
	filename="s.ssh"
Content-Transfer-Encoding: base64

IyBTdHlsZSBzaGVldCBmb3IgdGhlIFMgbGFuZ3VhZ2UsIHBhcnRpY3VsYXJseSBpdHMgUiBk
aWFsZWN0CiMKIyBHTlUgUiBpcyBhbiBpbXBsZW1lbnRhdGlvbiBvZiBTLCBhIGxhbmd1YWdl
IChkZXZlbG9wZWQgYXQgQmVsbCAKIyBMYWJvcmF0b3JpZXMpIGZvciBzdGF0aXN0aWNhbCBj
b21wdXRpbmcgYW5kIGdyYXBoaWNzLiAKIwojIEEgc3VpdGFibGUgc2hlZXRzLm1hcCBlbnRy
eSB3b3VsZCBiZQojIEdOVSBSIGZpbGVzLCBhcyB3ZWxsIGFzIFMgbGFuZ3VhZ2UgZmlsZXMK
IyBzOgkvKi5bUlNdLwojCiMgQ29weXJpZ2h0IChjKSAyMDAxIERpcmsgRWRkZWxidWV0dGVs
CiMKCiMKIyBUaGlzIGZpbGUgaXMgcGFydCBvZiBhMnBzLgojCiMgVGhpcyBwcm9ncmFtIGlz
IGZyZWUgc29mdHdhcmU7IHlvdSBjYW4gcmVkaXN0cmlidXRlIGl0IGFuZC9vciBtb2RpZnkK
IyBpdCB1bmRlciB0aGUgdGVybXMgb2YgdGhlIEdOVSBHZW5lcmFsIFB1YmxpYyBMaWNlbnNl
IGFzIHB1Ymxpc2hlZCBieQojIHRoZSBGcmVlIFNvZnR3YXJlIEZvdW5kYXRpb247IGVpdGhl
ciB2ZXJzaW9uIDIsIG9yIChhdCB5b3VyIG9wdGlvbikKIyBhbnkgbGF0ZXIgdmVyc2lvbi4K
IwojIFRoaXMgcHJvZ3JhbSBpcyBkaXN0cmlidXRlZCBpbiB0aGUgaG9wZSB0aGF0IGl0IHdp
bGwgYmUgdXNlZnVsLAojIGJ1dCBXSVRIT1VUIEFOWSBXQVJSQU5UWTsg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
IGtleXdvcmRzCgprZXl3b3JkcyBpbiBLZXl3b3JkIGFyZQogICBlbHNlLCBmb3IsIGZ1bmN0
aW9uLCBpZiwgImluIiwgcmVwZWF0LCBzd2l0Y2gsIHdoaWxlCmVuZCBrZXl3b3JkcwoKI29w
dGlvbmFsIG9wZXJhdG9ycyBhcmUKb3BlcmF0b3JzIGFyZQogICA8PiBcbmVxLAogICAhPSBc
bmVxLAogICA8PSBcbGVxLAogICA+PSBcZ2VxLAogICA9ICBcZXF1aXYsCiMgICA8LSBcbGVm
dGFycm93LAogICA8LSwKICAgPDwtLAogICAtPiwKICAgXwplbmQgb3BlcmF0b3JzCgpzZXF1
ZW5jZXMgYXJlCiAgICAiIyIgQ29tbWVudCwKICAgIEMtc3RyaW5nLAogICAgQy1jaGFyCmVu
ZCBzZXF1ZW5jZXMKCmVuZCBzdHlsZQo=

--oP8mQFO4PT
Content-Type: text/plain; charset=us-ascii
Content-Description: .signature
Content-Transfer-Encoding: 7bit



-- 
According to the latest figures, 43% of all statistics are totally worthless.

--oP8mQFO4PT--

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._