Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I used the great answer from @Sharpie on 19 Jan 2012 Getting Sweave code chunks to stay inside page margins? to get linebreaks in the R-code, but how can there be code highlighting (colors) too?

\documentclass[a4paper]{article}

\usepackage{listings}
\usepackage{inconsolata}

<<echo=FALSE>>=
  options(width=60)

  listing <- function(x, options) {
    paste("\\begin{lstlisting}[basicstyle=\\ttfamily,breaklines=true]\n",
      x, "\\end{lstlisting}\n", sep = "")
  }
  knit_hooks$set(source=listing, output=listing)
@

\title{Function listings with linebreaks and code highlighting}

\begin{document}
\maketitle

Two ways of printing the code.

<<tidy=TRUE,highlight=FALSE>>=
theFunction <- function(x) {
  tmp <- "A really long string that should be line-broken but it would be nice to also see code highlighting colors. The function is in the real code sourced, but for the sake of easier reproducibility, it is written here instead."
}

theFunction
@

\end{document}
share|improve this question
    
Thanks for comments. After a lot of testing, I think it seems like SweaveListingUtils do not work with knitr (only Sweave). I'm using knitr. –  Chris Feb 27 '13 at 20:37
    
just redefine the default listings style as the two answers below showed –  Yihui Feb 28 '13 at 18:42
add comment

2 Answers

up vote 3 down vote accepted

You can directly set your listings settings in your .Rnw file. Here I have defined a new style Rsetings.

\lstdefinestyle{Rsettings}{
  basicstyle=\ttfamily,
  breaklines=true,
  showstringspaces=false,
  keywords={if, else, function, theFunction, tmp}, % Write as many keywords 
  otherkeywords={},
  commentstyle=\itshape\color{Rcommentcolor},
  keywordstyle=\color{keywordcolor},
  moredelim=[s][\color{delimcolor}]{"}{"},
}

where the colors are defined as follows.

\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}

A few words about the key-value list in the \lstset.

  • All characters in between and including the " are typeset in \color{delimcolor}
  • You can set up otherkeywords and morekeywords if you are using more of these in your actual document. (Sorry, I am not yet very familiar with R since I am just starting with it.)
  • Click here to learn more about the listings package or you can type and enter texdoc listings in your terminal.

With these, you can now re-write your .Rnw file as

\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{inconsolata}

%-----------------
%Define the colors you want to use

\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}
%-----------------
%Set up your listings. You can type `texdoc listings` in your terminal

\lstdefinestyle{Rsettings}{
  language=R,
  basicstyle=\ttfamily,
  breaklines=true,
  showstringspaces=false,
  keywords={if, else, function, theFunction, tmp},
  otherkeywords={},
  commentstyle=\itshape\color{Rcommentcolor},
  keywordstyle=\color{keywordcolor},
  moredelim=[s][\color{delimcolor}]{"}{"},
}

\title{Function listings with linebreaks and code highlighting}

\begin{document}
\maketitle

Two ways of printing the code.

<<echo=FALSE>>=
  options(width=60)

  listing <- function(x, options) {
    paste("\\begin{lstlisting}[style=Rsettings]\n",
      x, "\\end{lstlisting}\n", sep = "")
  }
  knit_hooks$set(source=listing, output=listing)
@


<<tidy=TRUE,highlight=FALSE>>=
theFunction <- function(x) {
  tmp <- "A really long string that should be line-broken but it would be nice to also see code highlighting colors. The function is in the real code sourced, but for the sake of easier reproducibility, it is written here instead."
}

theFunction
@

\end{document}

And here is the output.

enter image description here

share|improve this answer
add comment

Another option is to use the predefined style of knitr-listings and/or knitr-themes built in knitr by the developer. Some examples can be seen in the knitr demo site.

Here is applied to your example.

\documentclass[a4paper]{article}

\usepackage{listings}
\usepackage{inconsolata}

\lstset{breaklines=true,showstringspaces=false}

<<setup, include=FALSE, cache=FALSE>>=
opts_chunk$set(fig.path = 'figure/listings-')
options(replace.assign = TRUE, width=60)
render_listings()
@

\title{Function listings with linebreaks and code highlighting}

\begin{document}
\maketitle

Two ways of printing the code.

<<tidy=TRUE,highlight=FALSE>>=
theFunction <- function(x) {
  tmp <- "A really long string that should be line-broken but it would be nice to also see code highlighting colors. The function is in the real code sourced, but for the sake of easier reproducibility, it is written here instead."
}

theFunction
@

\end{document}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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