[R] Best use of LaTeX listings package for pretty printing R code

Michael Kubovy kubovy at virginia.edu
Thu Sep 28 03:35:02 CEST 2006


I tried to use Frank's formatting improvements to compile Sweave- 
produced .tex, but it doesn't seem to make a difference. Advice?

On Sep 25, 2006, at 9:31 AM, Frank E Harrell Jr wrote:

> This is what I have been using.  Does anyone have a better way?  In
> particular I would like to see letters in comment strings not  
> stretched
> so much.  Thanks -Frank
>
> \documentclass{article}
> \usepackage{listings,relsize}
> \lstloadlanguages{R}
> \newcommand{\lil}[1]{\lstinline|#1|}
>
> \begin{document}
> \lstset{language=R,basicstyle=\smaller,commentstyle=\rmfamily\smaller,
>   showstringspaces=false,%
>   xleftmargin=4ex,literate={<-}{{$\leftarrow$}}1 {~}{{$\sim$}}1}
> \lstset{escapeinside={(*}{*)}}   % for (*\ref{ }*) inside  
> lstlistings (S
> code)
> \begin{lstlisting}
> a <- b   # this is a test line
> if(i==3) {  # another line, for y^2
>   y <- 3^3
>   z <- 'this string'
>   qqcat <- y ~ pol(x,2)
> } else y <- 4
> \end{lstlisting}
> That was \lstinline|x <- 22| \lil{q <- 'cat'}.
> \end{document}

_____________________________
Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
Parcels:    Room 102        Gilmer Hall
         McCormick Road    Charlottesville, VA 22903
Office:    B011    +1-434-982-4729
Lab:        B019    +1-434-982-4751
Fax:        +1-434-982-4766
WWW:    http://www.people.virginia.edu/~mk9y/



More information about the R-help mailing list