Re: [R] How to format R code in LaTex documents

From: Frank E Harrell Jr <>
Date: Mon 15 Jan 2007 - 14:12:33 GMT

Benjamin Dickgiesser wrote:

> Hi,
> I am planning on putting some R script in an appendix of a LaTex
> document. Can anyone recommend me a way of how to format it? Is there
> a way to keep all line breaks without having to insert \\ in every
> single line?
> Thank you!
> Benjamin

Here's one way and I would appreciate anyone's improvements. I've also included solutions from two others. Please let me know what you decide to use. -Frank

  xleftmargin=4ex,literate={<-}{{$\leftarrow$}}1 {~}{{$\sim$}}1}
\lstset{escapeinside={(*}{*)}} % for (*\ref{ }*) inside lstlistings (S

. . .

. . . S code . . .

The following code was provided by Vincent Goulet:

listings is a great package to highlight R keywords and comments and --- that
was my main use of the package --- index those keywords. I found that I had to slightly redefine the list of keywords included in listings. I still did not take the time to submit a patch to the author, though...

In any case, here's what I use, if it can be of any help to anyone:







Anupam Tyagi provided the following:


Somethings .....

\lstset{% general command to set parameter(s)
basicstyle=\small, % print whole in small stringstyle=\ttfamily, % typewriter type for strings numbers=left, % numbers on the left
numberstyle=\tiny, % Tiny numbers
stepnumber=2, % number every second line of code numbersep=5pt, % 5pt seperation between numbering and code listing language=R }


\end{document} mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue Jan 16 01:17:40 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 16 Jan 2007 - 17:30:27 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.