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

fontsrsweave

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"):

\documentclass{article}
\usepackage{float, hyperref}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{hyperref}

\usepackage{sectsty}
\sectionfont{\fontsize{12}{13}\selectfont}

\begin{document}

<<options, echo=FALSE, warning=FALSE>>=
  opts_chunk$set(cache=TRUE)
@

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

<<>>=
setwd("/Users/")

var1 = 3
var2 = var1 + 5
@

\end{document}

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"):

\documentclass{article}
\setlength{\parindent}{0pt} % Remove indent at new paragraphs
\setcounter{secnumdepth}{0}  % Remove section numbering at certain depth
\usepackage{tabu}
\usepackage[round,sort]{natbib}
\usepackage{fixltx2e}
\usepackage{graphicx}   % For external pictures
\usepackage{float}
\usepackage{subfig} % Add subfigures within figures
\usepackage{verbatim}
\usepackage[colorlinks=true,linkcolor=blue,citecolor=blue,urlcolor=blue]{hyperref}
\usepackage{amssymb,amsbsy,amsmath}
\usepackage{epsfig}
\usepackage[left=3cm,top=3cm,bottom=3.5cm,right=3cm]{geometry} % For easy document margins
\usepackage{fancyhdr} % For customization of header/footer
\usepackage{adjustbox}
\usepackage{framed}
\usepackage{enumitem}
\usepackage{caption}
\numberwithin{equation}{section} % Equation numbers relative to sections
\usepackage[dvipsnames]{xcolor}

%\usepackage[usenames]{colors}
%\definecolor{darkred}{rgb}{0.545,0,0}
%\definecolor{midnightblue}{rgb}{0.098,0.098,0.439}
%\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl,formatcom={\color{midnightblue}}}
%\DefineVerbatimEnvironment{Soutput}{Verbatim}{formatcom={\color{darkred}}}
%\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl,formatcom={\color{blue}}}

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

% \VignetteIndexEntry{ePort: Student performance report generation for statistics instructors}
%\VignettePackage{ePort}
%\documentclass{amsart}
\newcommand{\code}[1]{{\texttt{#1}}}
\newcommand{\pkg}[1]{{\texttt{#1}}}
\newcommand{\class}[1]{{\textit{#1}}}
\newcommand{\R}{{\normalfont\textsf{R }}{}}

\begin{document}
\sloppy

<<include=FALSE,echo=FALSE>>=
library(knitr)
opts_chunk$set(
concordance=TRUE
)
options(width=40)
@


<<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))))
@

%\SweaveOpts{prefix.string=fig,include=F,keep.source=T,eps=FALSE}

% <<echo=false>>=
% options(continue="  ")
% @
% %@% TO ELIMINATE THE "+" IN CONSECUTIVE SCRIPT LINES

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

\tableofcontents

\section{Introduction}

I will write my introduction here.

\subsection{Background}

Here is some background.

\subsection{Code}

Here is some code.

<<>>=
setwd("/Users/")

var1 = 3
var2 = var1 + 5
@

\section{Conclusions}

I will write my conclusions here

\end{document}

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).