2

How can I apply the same formatting with R chunks that I apply to regular listings? The following example shows a nicely formatting listing and an (IMHO) awful looking R chunk. I already loaded SweaveListingUtils and use SweaveListingUtils, no change in the output.

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{listings}
\lstset{language=R,breaklines=true}

\definecolor{hellgelb}{rgb}{1,1,0.8}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{1,0,0}
\definecolor{colString}{rgb}{0,0.5,0}

\setbeamertemplate{navigation symbols}{}

\lstset{%
    float=hbp,%
    basicstyle=\ttfamily\small, %
    identifierstyle=\color{colIdentifier}, %
    keywordstyle=\color{colKeys}, %
    stringstyle=\color{colString}, %
    commentstyle=\color{colComments}, %
    columns=flexible, tabsize=2, %
    frame=single, extendedchars=true, %
    showspaces=false, showstringspaces=false, %
    numbers=left, numberstyle=\tiny, %
    breaklines=true, backgroundcolor=\color{hellgelb}, %
    breakautoindent=true, captionpos=b%
}

\begin{document}

<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
library(SweaveListingUtils)
opts_chunk$set(fig.path = 'figure/listings-')
options(formatR.arrow = TRUE)
render_listings()
@

\begin{frame}[fragile]

\begin{lstlisting}
rnorm(3)
\end{lstlisting}

<<eval=TRUE>>=
rnorm(3)
@

\end{frame}
\end{document}

enter image description here

Update

Using the example (modified to article)and listings.css from Boris' answer below and running knitr on the boris.Rnw I receive a warning in the knitr output:

[1] "boris.tex"
Warnmeldung:
In color_def(options$background) :
  the color '' is invalid;using default color...see http://yihui.name/knitr/options

the TeX file Boris.tex is generated. Running pdflatex on this file gives:

 Undefined control sequence.
l.9 \hlkwd{rnorm}\hlstd{(}\hlnum
                                {3}\hlstd{)}
?

I have an up-to-date R, knitr and TeX Live 2015:

 *File List*
 article.cls    2014/09/29 v1.4h Standard LaTeX document class
  size10.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
graphicx.sty    2014/10/28 v1.0g Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
graphics.sty    2014/10/28 v1.0p Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg    2010/04/23 v1.9 graphics configuration of TeX Live
  pdftex.def    2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/error messages (HO)
 ltxcmds.sty    2011/11/09 v1.22 LaTeX kernel commands for general use (HO)
   color.sty    1999/02/16
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
  framed.sty    2011/10/22 v 0.96: framed or shaded text with page breaks
   alltt.sty    1997/06/16 v2.0g defines alltt environment
inputenc.sty    2015/03/17 v1.2c Input encoding file
    utf8.def    2015/06/27 v1.1n UTF-8 support for inputenc
   t1enc.dfu    2015/06/27 v1.1n UTF-8 support for inputenc
  ot1enc.dfu    2015/06/27 v1.1n UTF-8 support for inputenc
  omsenc.dfu    2015/06/27 v1.1n UTF-8 support for inputenc
 fontenc.sty
   t1enc.def    2005/09/27 v1.99g Standard LaTeX file
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
listings.sty    2015/06/04 1.6 (Carsten Heinz)
 lstmisc.sty    2015/06/04 1.6 (Carsten Heinz)
listings.cfg    2015/06/04 1.6 listings configuration
lstlang1.sty    2015/06/04 1.6 listings language file
lstlang2.sty    2015/06/04 1.6 listings language file
lstlang3.sty    2015/06/04 1.6 listings language file
 upquote.sty    2012/04/19 v1.3 upright-quote and grave-accent glyphs in verbat
im
textcomp.sty    2005/09/27 v1.99g Standard LaTeX package
  ts1enc.def    2001/06/05 v3.0e (jk/car/fm) Standard LaTeX file
  ts1enc.dfu    2015/06/27 v1.1n UTF-8 support for inputenc
  ts1cmr.fd    2014/09/29 v2.5h Standard LaTeX font definitions
supp-pdf.mkii
pdftexcmds.sty    2011/11/29 v0.20 Utility functions of pdfTeX for LuaTeX (HO)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
   ifpdf.sty    2011/01/30 v2.3 Provides the ifpdf switch (HO)
epstopdf-base.sty    2010/02/09 v2.5 Base part for package epstopdf
  grfext.sty    2010/08/19 v1.1 Manage graphics extensions (HO)
kvdefinekeys.sty    2011/04/07 v1.3 Define keys (HO)
kvoptions.sty    2011/06/30 v3.11 Key value format for package options (HO)
kvsetkeys.sty    2012/04/25 v1.16 Key value parser (HO)
etexcmds.sty    2011/02/16 v1.5 Avoid name clashes with e-TeX commands (HO)
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
  t1cmtt.fd    2014/09/29 v2.5h Standard LaTeX font definitions
 ***********
3
  • You can suppress the r-code with echo=FALSE in the chunk header. Then if you put your code into a file with extension .R you can just have source('.R') in the execution LaTeX r-chunk with echo=FALSE. And you have use the listings command to import the file *.R for the formal listing of your R-code. . Aug 5, 2015 at 18:02
  • I don't want to suppress it or load it from external files, I just want to have the second rnorm in the same format as the first. Aug 5, 2015 at 18:21
  • 1
    By the way, Uwe, I always wanted to thank you for introducing me to R, Sweave and knitr. Your lecture at a TUG meeting was very useful for me
    – Boris
    Aug 5, 2015 at 22:23

1 Answer 1

4

The simplest solution is not to use lstlistings but instead put the code into a chunk with eval=FALSE:

\begin{frame}[fragile]

<<eval=FALSE>>=
rnorm(3)
@

<<eval=TRUE>>=
rnorm(3)
@

\end{frame}

UPDATE

In my previous answer I assumed you wanted listings output to look like knitr. Now I see you want knitr output to look like listings. Well, this is also possible.

The simplest way is to use knitr theme mechansim, see https://github.com/yihui/knitr/blob/master/inst/examples/knitr-themes.Rnw.

First, create the CSS file, e.g. listings.css:

.background {
  color: #ffffcc;
}
.num {
  color: #000000;
}
.str {
  color: #008000;
}
.com {
  color: #ff0000;
  font-style: italic;
}
.opt {
  color: #0000ff;
  font-weight: bold;
}
.std {
  color: #000000;
}
.kwa {
  color: #0000ff;
  font-weight: bold;
}
.kwb {
  color: #0000ff;
  font-weight: bold;
}
.kwc {
  color: #0000ff;
  font-weight: bold;
}
.kwd {
  color: #0000ff;
  font-weight: bold;
}

You may want to tune up further. Then input the file in your rnw file:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{listings}
\lstset{language=R,breaklines=true}

\definecolor{hellgelb}{rgb}{1,1,0.8}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{1,0,0}
\definecolor{colString}{rgb}{0,0.5,0}

\setbeamertemplate{navigation symbols}{}

\lstset{%
    float=hbp,%
    basicstyle=\ttfamily\small, %
    identifierstyle=\color{colIdentifier}, %
    keywordstyle=\color{colKeys}, %
    stringstyle=\color{colString}, %
    commentstyle=\color{colComments}, %
    columns=flexible, tabsize=2, %
    frame=single, extendedchars=true, %
    showspaces=false, showstringspaces=false, %
    numbers=left, numberstyle=\tiny, %
    breaklines=true, backgroundcolor=\color{hellgelb}, %
    breakautoindent=true, captionpos=b%
}

\begin{document}

<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
opts_chunk$set(fig.path = 'figure/listings-')
knit_theme$set("listings.css")
options(formatR.arrow = TRUE)
@

\begin{frame}[fragile]

\begin{lstlisting}
rnorm(3)
\end{lstlisting}

<<eval=TRUE>>=
rnorm(3)
@

\end{frame}
\end{document}

Here is the result:

enter image description here

5
  • Using this approach both "rnorm" rows come as dirtgreen on gray, which looks (as I think) horrible if compared to the lstlisting piece of code. Aug 5, 2015 at 22:24
  • 1
    Oh, I did not get your question. Please see update.
    – Boris
    Aug 5, 2015 at 23:02
  • This looks really promising, I'll test tomorrow. Aug 5, 2015 at 23:06
  • I unfortunately receive error messages regarding the \hl commands when I run your code: ! Undefined control sequence. l.5 \hlkwd{rnorm}\hlstd{(}\hlnum{3}\hlstd{)} Aug 7, 2015 at 8:08
  • can you post an mwe including your version of knitr and latex styles? Maybe you need to delete refs to sweavelistingutils
    – Boris
    Aug 7, 2015 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .