[R] How to nicely display R code with the LaTeX package 'listings'?
Marius Hofert
marius.hofert at math.ethz.ch
Thu Nov 1 10:32:46 CET 2012
Dear expeRts,
What's a 'good' (nice-looking, easy-to-read) setup for the LaTeX package
'listings' to display R code?
The two versions below are partly inspired by the settings of the package
SweaveListingUtils and
http://r.789695.n4.nabble.com/R-How-to-format-R-code-in-LaTex-documents-td816055.html
Any suggestions, comments, or improvements are welcome.
Cheers,
Marius
### Version 1 ##################################################################
\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{graphicx}
\usepackage{fancyvrb}
\usepackage{listings}
\lstset{% setup listings
language=R,% set programming language
basicstyle=\small,% basic font style
keywordstyle=\bfseries,% keyword style
commentstyle=\ttfamily\itshape,% comment style
numbers=left,% display line numbers on the left side
numberstyle=\scriptsize,% use small line numbers
numbersep=10pt,% space between line numbers and code
tabsize=3,% sizes of tabs
showstringspaces=false,% do not replace spaces in strings by a certain character
captionpos=b,% positioning of the caption below
breaklines=true,% automatic line breaking
escapeinside={(*}{*)},% escaping to LaTeX
fancyvrb=true,% verbatim code is typset by listings
extendedchars=false,% prohibit extended chars (chars of codes 128--255)
literate={"}{{\texttt{"}}}1{<-}{{$\leftarrow$}}1{<<-}{{$\twoheadleftarrow$}}1
{~}{{$\sim$}}1{<=}{{$\le$}}1{>=}{{$\ge$}}1{!=}{{$\neq$}}1{^}{{$^\wedge$}}1,% item to replace, text, length of chars
alsoletter={.<-},% becomes a letter
alsoother={$},% becomes other
otherkeywords={!=, ~, $, *, \&, \%/\%, \%*\%, \%\%, <-, <<-, /},% other keywords
deletekeywords={c}% remove keywords
}
\begin{document}
\noindent Just some text; see Line \ref{foo}.
\begin{lstlisting}[caption={A first example}, label=list:ex]
x <- c(1, 3, 2)
id <- function(x){
x # just a dummy (*\label{foo}*)
}
3 <= 4
3 != 4
!TRUE
y <- "foo"
(pv <- sum(x*x^x))
y ~ x + a
\end{lstlisting}
\end{document}
### Version 2 ##################################################################
\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{graphicx}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage{bm}
\usepackage{xcolor}
\xdefinecolor{gray}{rgb}{0.4,0.4,0.4}
\xdefinecolor{blue}{RGB}{58,95,205}% R's royalblue3; #3A5FCD
\lstset{% setup listings
language=R,% set programming language
basicstyle=\ttfamily\small,% basic font style
keywordstyle=\color{blue},% keyword style
commentstyle=\color{gray},% comment style
numbers=left,% display line numbers on the left side
numberstyle=\scriptsize,% use small line numbers
numbersep=10pt,% space between line numbers and code
tabsize=3,% sizes of tabs
showstringspaces=false,% do not replace spaces in strings by a certain character
captionpos=b,% positioning of the caption below
breaklines=true,% automatic line breaking
escapeinside={(*}{*)},% escaping to LaTeX
fancyvrb=true,% verbatim code is typset by listings
extendedchars=false,% prohibit extended chars (chars of codes 128--255)
literate={"}{{\texttt{"}}}1{<-}{{$\bm\leftarrow$}}1{<<-}{{$\bm\twoheadleftarrow$}}1
{~}{{$\bm\sim$}}1{<=}{{$\bm\le$}}1{>=}{{$\bm\ge$}}1{!=}{{$\bm\neq$}}1{^}{{$^{\bm\wedge}$}}1,% item to replace, text, length of chars
alsoletter={.<-},% becomes a letter
alsoother={$},% becomes other
otherkeywords={!=, ~, $, \&, \%/\%, \%*\%, \%\%, <-, <<-, /},% other keywords
deletekeywords={c}% remove keywords
}
\begin{document}
\noindent Just some text; see Line \ref{foo}.
\begin{lstlisting}[caption={A first example}, label=list:ex]
x <- c(1, 3, 2)
id <- function(x){
x # just a dummy (*\label{foo}*)
}
3 <= 4
3 != 4
!TRUE
y <- "foo"
(pv <- sum(x*x^x))
y ~ x + a
\end{lstlisting}
\end{document}
More information about the R-help
mailing list