使用knitr对输入和输出进行不同的着色

时间:2021-10-07 18:10:12

I would like to have my input R code printed/displayed on a light grey background and the output on a white background. So for example if my .Rnw file contains this chunk:

我希望我的输入R代码打印/显示在浅灰色背景上,输出在白色背景上。例如,如果我的。rnw文件包含这个块:

<<>>=
t.test(zinc~sex, data = zinc.df)
@

I would like the input to be rendered with a light grey background:

我希望输入以浅灰色背景呈现:

> t.test(zinc~sex, data = zinc.df)

> t。测试(锌~性,data = zinc.df)

and the output to be rendered on a white background, e.g.

在白色背景上呈现的输出。

Welch Two Sample t-test

韦尔奇两样本t检验

data: zinc by sex

数据:通过性锌

t = -15.08, df = 2678, p-value < 2.2e-16

t = -15.08, df = 2678, p值< 2.22 -16

(Output deliberately truncated but you get the idea). It seems like I might be able to do this with knitr themes, but I can't quite see it. Like many, I'm not keen to start fiddling with the listings package.

(输出被故意截断,但你懂的)。似乎我可以用knitr主题来做这件事,但是我看不太清楚。和许多人一样,我也不想开始摆弄上市计划。

3 个解决方案

#1


1  

This is the best I can do:

这是我所能做的最好的:

\documentclass[12pt,letterpaper,twoside]{book}  
\usepackage{inconsolata}
\usepackage{listings,color}

<<setup, include=FALSE>>=
options(width=60)
opts_chunk$set(fig.align='center', concordance=TRUE, fig.show='hold', size='footnotesize', prompt=FALSE, comment=NA, tidy=TRUE, results='markup', tidy.opts=list(width.cutoff=50), comment='#-#', highlight=TRUE)
opts_knit$set(concordance=TRUE,self.contained=FALSE)
color_block_output = function(x) {
function(x, options)  paste('\\begin{outputblock}',x,'\\end{outputblock}',sep="")
}
knit_hooks$set(output = color_block_output(''))
@

\lstnewenvironment{outputblock}{
\lstset{backgroundcolor=\color{white},
frame=single,
framerule=0pt,
basicstyle=\ttfamily,
columns=fullflexible}}{}

\begin{document}


<<test>>=
print('test')

(mat <- matrix(data=1:25,nrow=5,ncol=5)) 
@

\end{document}

The result will be:使用knitr对输入和输出进行不同的着色

结果将是:

I haven't find a solution for the situation when there is no output (the white space looks a bit strange) and the trailing grey space. Anyway, this is a start, hope this helps.

对于没有输出(空格看起来有点奇怪)和尾灰空格的情况,我还没有找到一个解决方案。不管怎样,这是一个开始,希望这能有所帮助。

#2


1  

Your problem is best suited using the listings package, but it need not be painful -- here is an example (note the backgroundcolor parm in the lstdefinestyle):

您的问题最适合使用列表包,但是它不需要很痛苦——这里有一个例子(请注意lstdefinestyle中的背景色参数):

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{listings}
\usepackage{color}

%-- Define your colors, easy to look up
\definecolor{Rcodegreen}{rgb}{0,0.6,0}
\definecolor{Rcodegray}{rgb}{0.5,0.5,0.5}
\definecolor{Rcodepurple}{rgb}{0.58,0,0.82}
\definecolor{Rbackcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{customstyle}{
backgroundcolor=\color{Rbackcolour},   
commentstyle=\color{Rcodegreen},
keywordstyle=\color{Rmagenta},
numberstyle=\tiny\color{Rcodegray},
stringstyle=\color{Rcodepurple},
basicstyle=\footnotesize,
breakatwhitespace=false,         
breaklines=true,                 
captionpos=b,                    
keepspaces=true,                 
numbers=left,                    
numbersep=5pt,                  
showspaces=false,                
showstringspaces=false,
showtabs=false,                  
tabsize=2
}

\lstset{style=customstyle}

\title{Fooing with foo}

\begin{document}
\maketitle

<<tidy=TRUE,highlight=FALSE>>=
x <- "h3ll0 w0rld"
cat(x, "\n")

@

\end{document}

#3


1  

If your working with Rnw and Latex only, you can use the background option of the chunks. However, you will need to write your chunk twice.
(1) with echo=TRUE, eval=FALSE and background='white'

如果只使用Rnw和Latex,则可以使用块的background选项。然而,您将需要编写您的块两次。(1) echo=TRUE, eval=FALSE, background='white'

<<echo=TRUE, eval=FALSE, background='white'>>=
@  

(2) with echo=FALSE, eval=TRUE and background='yellow'

(2) echo=FALSE时,eval=TRUE, background='yellow'

<<echo=FALSE, eval=TRUE, background='yellow'>>=
@    

#1


1  

This is the best I can do:

这是我所能做的最好的:

\documentclass[12pt,letterpaper,twoside]{book}  
\usepackage{inconsolata}
\usepackage{listings,color}

<<setup, include=FALSE>>=
options(width=60)
opts_chunk$set(fig.align='center', concordance=TRUE, fig.show='hold', size='footnotesize', prompt=FALSE, comment=NA, tidy=TRUE, results='markup', tidy.opts=list(width.cutoff=50), comment='#-#', highlight=TRUE)
opts_knit$set(concordance=TRUE,self.contained=FALSE)
color_block_output = function(x) {
function(x, options)  paste('\\begin{outputblock}',x,'\\end{outputblock}',sep="")
}
knit_hooks$set(output = color_block_output(''))
@

\lstnewenvironment{outputblock}{
\lstset{backgroundcolor=\color{white},
frame=single,
framerule=0pt,
basicstyle=\ttfamily,
columns=fullflexible}}{}

\begin{document}


<<test>>=
print('test')

(mat <- matrix(data=1:25,nrow=5,ncol=5)) 
@

\end{document}

The result will be:使用knitr对输入和输出进行不同的着色

结果将是:

I haven't find a solution for the situation when there is no output (the white space looks a bit strange) and the trailing grey space. Anyway, this is a start, hope this helps.

对于没有输出(空格看起来有点奇怪)和尾灰空格的情况,我还没有找到一个解决方案。不管怎样,这是一个开始,希望这能有所帮助。

#2


1  

Your problem is best suited using the listings package, but it need not be painful -- here is an example (note the backgroundcolor parm in the lstdefinestyle):

您的问题最适合使用列表包,但是它不需要很痛苦——这里有一个例子(请注意lstdefinestyle中的背景色参数):

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{listings}
\usepackage{color}

%-- Define your colors, easy to look up
\definecolor{Rcodegreen}{rgb}{0,0.6,0}
\definecolor{Rcodegray}{rgb}{0.5,0.5,0.5}
\definecolor{Rcodepurple}{rgb}{0.58,0,0.82}
\definecolor{Rbackcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{customstyle}{
backgroundcolor=\color{Rbackcolour},   
commentstyle=\color{Rcodegreen},
keywordstyle=\color{Rmagenta},
numberstyle=\tiny\color{Rcodegray},
stringstyle=\color{Rcodepurple},
basicstyle=\footnotesize,
breakatwhitespace=false,         
breaklines=true,                 
captionpos=b,                    
keepspaces=true,                 
numbers=left,                    
numbersep=5pt,                  
showspaces=false,                
showstringspaces=false,
showtabs=false,                  
tabsize=2
}

\lstset{style=customstyle}

\title{Fooing with foo}

\begin{document}
\maketitle

<<tidy=TRUE,highlight=FALSE>>=
x <- "h3ll0 w0rld"
cat(x, "\n")

@

\end{document}

#3


1  

If your working with Rnw and Latex only, you can use the background option of the chunks. However, you will need to write your chunk twice.
(1) with echo=TRUE, eval=FALSE and background='white'

如果只使用Rnw和Latex,则可以使用块的background选项。然而,您将需要编写您的块两次。(1) echo=TRUE, eval=FALSE, background='white'

<<echo=TRUE, eval=FALSE, background='white'>>=
@  

(2) with echo=FALSE, eval=TRUE and background='yellow'

(2) echo=FALSE时,eval=TRUE, background='yellow'

<<echo=FALSE, eval=TRUE, background='yellow'>>=
@