Marius Hofert
2012-Nov-01 09:32 UTC
[R] How to nicely display R code with the LaTeX package 'listings'?
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}
Frank Harrell
2012-Nov-01 18:23 UTC
[R] How to nicely display R code with the LaTeX package 'listings'?
The knitr package makes this relatively easy to do. See for example http://biostat.mc.vanderbilt.edu/KnitrHowto Frank Marius Hofert-3 wrote> 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} > > ______________________________________________> R-help@> mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.----- Frank Harrell Department of Biostatistics, Vanderbilt University -- View this message in context: http://r.789695.n4.nabble.com/How-to-nicely-display-R-code-with-the-LaTeX-package-listings-tp4648110p4648149.html Sent from the R help mailing list archive at Nabble.com.