RNW文件:用Listings类设置替换Knitr格式

时间:2019-02-17 20:30:22

标签: r knitr

我有自己的前导代码,用于创建所需的Sinput和Soutput列表格式。我试图弄清楚如何使knitr在被编译为LaTeX的文档中使用它们。我一直在使用kintr挂钩进行测试,但我想不出如何使它们正常工作。我不要不要在文档顶部插入Sweavel.sty或Sweave.sty。

我希望R代码块看起来像这样,如我的清单style = Rinput所示:

listings output

不是用knitr看到的结果:

listings output

我想我知道我可以摆弄编织的配色方案来模仿清单的输出,但是我不想这么做,部分原因是我认为结果不会完全一样。

这是MRE:

\documentclass[11pt,letterpaper,english]{extarticle}
\usepackage{lmodern}
\renewcommand{\sfdefault}{lmss}
\renewcommand{\ttdefault}{lmtt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\setlength{\parskip}{\medskipamount}
\setlength{\parindent}{0pt}
\usepackage{color}
\usepackage{babel}
\usepackage[unicode=true,pdfusetitle,
 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
 breaklinks=true,pdfborder={0 0 0},pdfborderstyle={},backref=false,colorlinks=true]
 {hyperref}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
\special{papersize=\the\paperwidth,\the\paperheight}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\usepackage[natbibapa]{apacite}

\@ifundefined{date}{}{\date{}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
%\VignetteIndexEntry{Instructions for guides using knitted code chunks}
%\VignetteEngine{knitr}
%\VignetteBuilder{stationery::rnw2pdf}
%\VignetteEncoding{UTF-8}


\usepackage[includehead, includefoot,
lmargin=1in,
rmargin=1in,
tmargin=0.75in,
bmargin=1.0in,
headheight=0pt,
headsep=0pt,
marginparwidth=0pt,
footskip=1.5\baselineskip,
]{geometry}


%% 20180215: this does not cooperate with knitr, but someday we hope
%% it might.
%% This controls display of code chunks
\usepackage{ae,fancyvrb,relsize,listings}
\providecommand{\Sweavesize}{\normalsize}
\providecommand{\Rsize}{}
\renewcommand{\Rsize}{\normalsize}
\providecommand{\Routsize}{\scriptsize}

\providecommand{\Rcolor}{\color[rgb]{0.1, 0.1, 0.1}}
\providecommand{\Routcolor}{\color[rgb]{0.2, 0.2, 0.2}}
\providecommand{\Rcommentcolor}{\color[rgb]{0.101, 0.43, 0.432}}

\providecommand{\Rbackground}{\color[gray]{0.91}}
\providecommand{\Routbackground}{\color[gray]{0.935}}
% Can specify \color[gray]{1} for white background or just \color{white}

\lstdefinestyle{Rinput}{
  language=R,
  escapechar=`,
  fancyvrb=false,%
  tabsize=2,%
  breaklines=true,
  breakatwhitespace=true,%
  captionpos=b,%
  frame=single,%
  framerule=0.2pt,%
  framesep=1pt,%
  showstringspaces=false,%
  basicstyle=\Rsize\Rcolor\ttfamily,%
  columns=fixed%,
  \lst@ifdisplaystyle\scriptsize\fi,%,
  commentstyle=\Rcommentcolor\ttfamily,%
  identifierstyle=,%
  keywords=\bfseries,%
  keywordstyle=\color[rgb]{0, 0.5, 0.5},
  escapeinside={(*}{*)},
  literate={~}{{$\sim$}}1{==}{{=\,=}}2{--}{{-\,-}}2,
  alsoother={$},
  alsoletter={.<-},%
  otherkeywords={!,!=,~,$$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-,/},%
  backgroundcolor=\Rbackground,%
  numbers=left,%
  stepnumber=5,
  firstnumber=1,
  numberstyle={\tiny}
}%

% Other options of interest:
% frame=single,framerule=0.1pt,framesep=1pt,rulecolor=\color{blue},
% numbers=left,numberstyle=\tiny,stepnumber=1,numbersep=7pt,
% keywordstyle={\bf\Rcolor}

\lstdefinestyle{Routput}{fancyvrb=false,
  literate={~}{{$\sim$}}1{R^2}{{$R^{2}$}}2{^}{{$^{\scriptstyle\wedge}$}}1{R-squared}{{$R^{2}$}}2,%
  basicstyle=\Routcolor\Routsize\ttfamily,%
  backgroundcolor=\Routbackground,
  language=R,
  escapechar=`,
  fancyvrb=false,%
  tabsize=2,%
  breaklines=true,
  breakatwhitespace=true,%
  captionpos=b,%
  frame=single,%
  framerule=0.2pt,%
  framesep=1pt,%
  showstringspaces=false,%
  columns=fixed%,
  \lst@ifdisplaystyle\scriptsize\fi,%
  identifierstyle=,%
  keywords=\bfseries,%
  keywordstyle=\color[rgb]{0, 0.5, 0.5},
  escapeinside={(*}{*)},
  literate={~}{{$\sim$}}1 {==}{{=\,=}}2,
  alsoother={$},
  alsoletter={.<-},%
  otherkeywords={!,!=,~,$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-,/},
  numbers=left,
  numberstyle={\tiny}
}

\let\Schunk\relax
\let\Sinput\relax
\newenvironment{Schunk}{}{}
\let\Scode\relax
\let\Soutput\relax
\lstnewenvironment{Sinput}{\lstset{style=Rinput}}{}
\lstnewenvironment{Scode}{\lstset{style=Rinput}}{}
\lstnewenvironment{Soutput}{\lstset{style=Routput}}{}

\lstset{tabsize=2, breaklines=true, style=Rinput, breakatwhitespace=true}

\makeatother

\usepackage{listings}
\renewcommand{\lstlistingname}{\inputencoding{latin9}Listing}

\begin{document}
<<ignoremeRoptions, echo=FALSE>>=
tdir <- "tmpout"
if(!dir.exists(tdir)) dir.create(tdir, showWarnings=FALSE)
opts.orig <- options()
options(width=75, prompt=" ", continue="  ")
options(useFancyQuotes = FALSE) 
set.seed(12345)
pdf.options(onefile=FALSE,family="Times",pointsize=10)
library(stationery)
## Chunk output 
library(knitr)
opts_chunk$set(fig.path="tmpout/t-", split=FALSE, 
               ae=FALSE, fig.height=3, fig.width=5, comment=NA)
@

Here is a listings object that uses the style I want to offer with
R input code chunks. I reflects the style=Rinput that I have in my
preamble.

\inputencoding{latin9}\begin{lstlisting}
library(stationery)
initWriteup("rnw2pdf-guide-knit")
\end{lstlisting}
\inputencoding{utf8}
This is the color scheme preferred by knitr, which I want to replace
with a listings environment.

<<lib10, results="hide">>=
library(stationery)
initWriteup("rnw2pdf-guide-knit")
@

I want inout/output series of chunks to look in a certain way. Here
is input

\inputencoding{latin9}\begin{lstlisting}[style=Rinput]
set.seed(112)
x <- rnorm(1000)
\end{lstlisting}
\inputencoding{utf8}
and this output, using listings style Routput, has slightly smaller
font:

\inputencoding{latin9}\begin{lstlisting}[style=Routput]
[1] 0.04619816
\end{lstlisting}
\inputencoding{utf8}
Rather than what I get from knitr:

<<lib20>>=
x <- rnorm(1000)
mean(x)
@

Try as I might, I am unsuccessful in preventing the pastel tooty-fruity
colors preferred by knitr. I tried editing the knitr code base to
replace the code in render\_listings, but ended up breaking the whole
thing.

<<session10, echo=F>>=
## Please don't delete this.
sessionInfo()
@
\end{document}

0 个答案:

没有答案