Romain Francois, Professional R Enthusiast

To content | To menu | To search

Tag - latex

Entries feed - Comments feed

Saturday, May 29 2010

highlight 0.1-9

The version 0.1-8 of highlight introduced a small bug in the latex renderer.

This is now fixed in version 0.1-9 and the latex renderer also gains an argument "minipage" which wraps the latex code in a minipage environment. I've used this to make this vignette for an upcoming feature of Rcpp

Friday, December 11 2009

new R package : bibtex

I've pushed to CRAN the package bibtex package

The package defines the read.bib function that reads a file in the bibtex format. The code is based on bibparse

The read.bib function generates an object of class citationList, just like utils::citation

Sunday, November 22 2009

new R package : highlight

I finally pushed highlight to CRAN, which should be available in a few days. The package uses the information gathered by the parser package to perform syntax highlighting of R code

The main function of the package is highlight, which takes a number of argument including :

  • file : the file in which the R code is
  • output : some output connection or file name where to write the result (The default is standard output)
  • renderer : a collection of function controlling how to render code into a given markup language

The package ships three functions that create such renderers

  • renderer_html : renders in html/css
  • renderer_latex: renders in latex
  • renderer_verbatim: does nothing

And additionally, the xterm256 package defines a renderer that allows syntax highlighting directly in the console (if the console knows xterm 256 colors)

Let's assume we have this code file (/tmp/code.R)

f <- function( x){
        x + rnorm(1)

g <- function(x){}
h <- function(x){}

Then we can syntax highlight it like this :

> highlight( "/tmp/code.R", renderer = renderer_html(), output = "/tmp/code.R.html" )
> highlight( "/tmp/code.R", renderer = renderer_latex(), output = "/tmp/code.R.latex" )

which makes these files : code.R.html and code.R.latex

The package also ships a sweave driver that can highlight code chunks in a sweave document, but I'll talk about this in another post

Thursday, January 22 2009

Code2html speaks Latex too

For highlighting code inside latex documents there are several options:
This might be a judgement call, but I don't find the latex output of listings or highlight to be visually pleasing enough, but maybe it is just a lack of understanding on how to customize them. Anyway, jedit has the Code2HTML plugin which does a good job of translating the buffer being edited in jedit into html markup, preserving pretty much everything that appear on the jedit text area. This takes advantage of jedit mode files which offers a great deal of flexibility, including nested languages, such as javascript/php, or R and Sweave if you have the right mode files.
So I had a look into the code of the Code2HTML plugin which is simple enough, and have basically done a big s/html/latex/g to support latex output (I realize this is not the best option and I should probably abstract things out so that it would be easier to add new formats: ansi, svg, ...) but it does the trick for now. Here is a screenshot of an example file edited in jedit: ScreenshotJedit.png And a screenshot of the pdf file ScreenshotPdf.png Here are the relevant files if you want to try it out: Next steps:
  • do something about the gutter
  • figure out how to invoke that from the command line
  • write a sweave driver that would call it so that R code appears formatted in sweave documents