Search code examples
rsyntax-highlightingsweave

Sweave syntax highlighting in output - revisit


My question is the same as the one in the link Sweave syntax highlighting in output.

I have followed the suggestion by daroczig https://tex.stackexchange.com/questions/5113/how-to-colorize-syntax-using-r-sweave, and created the following test.Rnw file:

\documentclass{article}
\RequirePackage{fancyvrb}
\RequirePackage{listings}
\SweaveOpts{keep.source=TRUE}
<<SweaveListingsPreparations, results=tex, echo=FALSE, strip.white=false>>=
require(SweaveListingUtils)
SweaveListingPreparations()
 setToBeDefinedPkgs(pkgs = c("SweaveListingUtils","distr"),
                   keywordstyles = c("\\bf\\color{blue}","\\bf\\color{red}"))
@

\begin{document}
\SweaveOpts{concordance=TRUE}
\section{Example}
This is an example with three variables in R.

<<>>=
options(continue = " ")
x  <- 10
t <- 'The brown fox'
b <- TRUE
@

<<>>=
x
@

<<>>=
t
@

<<>>=
b
@
\end{document}

<<cleaup, echo=FALSE>>=
unloadNamespace("SweaveListingUtils")
@

However, I get the following error message: enter image description here

Suggestions?


Solution

  • Since you don't have \usepackage{Sweave} in your document, Sweave will insert it. But the SweaveListingsUtils package also inserts conflicting code.

    So you need to follow the advice in the SweaveListingsUtils documentation, and put in a comment

    % \usepackage{Sweave}
    

    near the start of your document. This will prevent R from inserting it.

    However, do note that support for SweaveListingsUtils is ending; you are better off using knitr.