Question

I am trying to highlight R code in a Latex document, but I can't seem to change the font colors inside the code box. For instance, I think commentstyle=\color{red} should give me red font for the comments, but comments appear in either blue or black (can't quite tell). Also, I think backgroundcolor=\color{white} should give me a white background...

I'm using knitr and RStudio to write and compile the pdf. Example below.

\documentclass{article}
\usepackage{inconsolata}
\usepackage{listings}
\usepackage{xcolor}

\lstset{ %
  language=R,                     
  basicstyle=\footnotesize,       
  numbers=left,                   
  numberstyle=\tiny\color{gray},  
  stepnumber=1,                   
  numbersep=5pt,                  
  backgroundcolor=\color{white},  
  showspaces=false,               
  showstringspaces=false,         
  showtabs=false,                     
  rulecolor=\color{black},        
  tabsize=2,                      
  captionpos=b,                   
  breaklines=true,                
  breakatwhitespace=false,        
  title=\lstname,                 
  keywordstyle=\color{blue},      
  commentstyle=\color{red},   
  stringstyle=\color{green},     
  escapeinside={\%*}{*)},         
  morekeywords={*,...}            
} 

\begin{document}

This is my document

<<code>>=
render_listings()
# This is a comment
test <- 1 + 1
@

\end{document}

Looks like this:

enter image description here

Was it helpful?

Solution

If you have your own Sweavel.sty in your texmf tree, knitr will use it (otherwise knitr will copy a built-in version), so you are free to modify anything in Sweavel.sty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top