[Tex/LaTex] Sweave R code chunk default colors in vignette


Typically, if I create a .Rnw file and add R code chunks to it, and then convert it to .pdf file, the R code text will have default color and font than the rest of the document text. An example follows (say it is called "exampleRnw.Rnw"):

\usepackage{float, hyperref}



<<options, echo=FALSE, warning=FALSE>>=

Here is the typically font in the document. Below is R code font.


var1 = 3
var2 = var1 + 5


To run the code above, I simply used knit2pdf("exampleRnw.Rnw"). You can see in the image below that the R code has different color and font from the rest of the text.

Output from exampleRnw.Rnw

However, I am now developing a .Rnw vignette file using some outlines I found online (i.e. that I might not fully understand all the details). Unfortunately, when create the vignette into a .pdf file, it does not seem to create the default font and color in the R code as a typical .Rnw file as shown above. An example follows (say it is called "exampleVignette.Rnw"):

\setlength{\parindent}{0pt} % Remove indent at new paragraphs
\setcounter{secnumdepth}{0}  % Remove section numbering at certain depth
\usepackage{graphicx}   % For external pictures
\usepackage{subfig} % Add subfigures within figures
\usepackage[left=3cm,top=3cm,bottom=3.5cm,right=3cm]{geometry} % For easy document margins
\usepackage{fancyhdr} % For customization of header/footer
\numberwithin{equation}{section} % Equation numbers relative to sections


% ---------------------------------------------------------------------------------------------------------------------------------------

% \VignetteIndexEntry{ePort: Student performance report generation for statistics instructors}
\newcommand{\R}{{\normalfont\textsf{R }}{}}



<<label=R options,echo=FALSE>>=
options(width = 60)
options(SweaveHooks = list(fig = function() par(mar=c(3,3,1,0.5),mgp = c(2,1,0))))


% <<echo=false>>=
% options(continue="  ")
% @

\title{My title will be here}
\author{My author list will be here}



I will write my introduction here.


Here is some background.


Here is some code.


var1 = 3
var2 = var1 + 5


I will write my conclusions here


To run the code above, I simply used devtools build_vignettes("exampleVignette.Rnw"). You can see in the image below that the R code does not have the nice coloring and font as in the exampleRnw.Rnw file above.

Output of exampleVignette.Rnw

Is it possible for me to use the same color and font in the R code of my vignette with what is used in the R code of the typical .Rnw files?

Best Answer

Please see http://yihui.name/knitr/demo/vignette/ Basically you have to tell R that you are using knitr as the vignette engine, and the default is Sweave (which gives you the output like you saw above).