21

I wish to insert a R code chunk in a LaTeX document. The default settings for the listings package also changes the font, but not the colour, which I need.

Although I understand that I can set the colours using the lstset function, I am not very sure of the combination that would look good. Thus, can anyone share their lstset settings for the listings package to colour R syntax? This way everyone who sees this post can use the same settings!

2
  • 3
    Take a look at knitr. Chunk options: eval=FALSE, highlight=TRUE should suffice.
    – tonytonov
    Jan 28, 2014 at 10:33
  • 1
    Actually, I would prefer to use listings as that can wrap my code. Do you have any solutions?
    – Wet Feet
    Jan 30, 2014 at 3:46

1 Answer 1

28
\documentclass[12pt]{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}    

\lstset{ 
  language=R,                     % the language of the code
  basicstyle=\tiny\ttfamily, % the size of the fonts that are used for the code
  numbers=left,                   % where to put the line-numbers
  numberstyle=\tiny\color{Blue},  % the style that is used for the line-numbers
  stepnumber=1,                   % the step between two line-numbers. If it is 1, each line
                                  % will be numbered
  numbersep=5pt,                  % how far the line-numbers are from the code
  backgroundcolor=\color{white},  % choose the background color. You must add \usepackage{color}
  showspaces=false,               % show spaces adding particular underscores
  showstringspaces=false,         % underline spaces within strings
  showtabs=false,                 % show tabs within strings adding particular underscores
  frame=single,                   % adds a frame around the code
  rulecolor=\color{black},        % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here))
  tabsize=2,                      % sets default tabsize to 2 spaces
  captionpos=b,                   % sets the caption-position to bottom
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
  keywordstyle=\color{RoyalBlue},      % keyword style
  commentstyle=\color{YellowGreen},   % comment style
  stringstyle=\color{ForestGreen}      % string literal style
} 


\begin{document}
\begin{lstlisting}
library(foreign)

foo <- rnorm(100)
# writing a function
bar <- apply(foo, 1, function(x){
  y <- sqrt(x)
  cat(paste('The result is ', x )))
})
bar

str(bar)

foo + bar
\end{lstlisting}

\end{document}

enter image description here

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.