[Tex/LaTex] R-code linebreaks AND code highlighting in knitr

knitrr

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}

Best Answer

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