我如何才能将应用于常规列表的相同格式应用于 R 块?以下示例显示了一个格式良好的列表和一个(在我看来)看起来很糟糕的 R 块。我已经加载SweaveListingUtils
并使用了SweaveListingUtils
,输出没有变化。
\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{listings}
\lstset{language=R,breaklines=true}
\definecolor{hellgelb}{rgb}{1,1,0.8}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{1,0,0}
\definecolor{colString}{rgb}{0,0.5,0}
\setbeamertemplate{navigation symbols}{}
\lstset{%
float=hbp,%
basicstyle=\ttfamily\small, %
identifierstyle=\color{colIdentifier}, %
keywordstyle=\color{colKeys}, %
stringstyle=\color{colString}, %
commentstyle=\color{colComments}, %
columns=flexible, tabsize=2, %
frame=single, extendedchars=true, %
showspaces=false, showstringspaces=false, %
numbers=left, numberstyle=\tiny, %
breaklines=true, backgroundcolor=\color{hellgelb}, %
breakautoindent=true, captionpos=b%
}
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
library(SweaveListingUtils)
opts_chunk$set(fig.path = 'figure/listings-')
options(formatR.arrow = TRUE)
render_listings()
@
\begin{frame}[fragile]
\begin{lstlisting}
rnorm(3)
\end{lstlisting}
<<eval=TRUE>>=
rnorm(3)
@
\end{frame}
\end{document}
更新
使用下面鲍里斯的回答中的示例(已修改article
)并运行,我在 knitr 输出中收到警告:listings.css
knitr
boris.Rnw
[1] "boris.tex"
Warnmeldung:
In color_def(options$background) :
the color '' is invalid;using default color...see http://yihui.name/knitr/options
Boris.tex
生成TeX 文件。在此文件上运行 pdflatex 可得到:
Undefined control sequence.
l.9 \hlkwd{rnorm}\hlstd{(}\hlnum
{3}\hlstd{)}
?
我有最新的 R、knitr 和 TeX Live 2015:
*File List*
article.cls 2014/09/29 v1.4h Standard LaTeX document class
size10.clo 2014/09/29 v1.4h Standard LaTeX file (size option)
graphicx.sty 2014/10/28 v1.0g Enhanced LaTeX Graphics (DPC,SPQR)
keyval.sty 2014/10/28 v1.15 key=value parser (DPC)
graphics.sty 2014/10/28 v1.0p Standard LaTeX Graphics (DPC,SPQR)
trig.sty 1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg 2010/04/23 v1.9 graphics configuration of TeX Live
pdftex.def 2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty 2010/04/08 v1.3 Providing info/warning/error messages (HO)
ltxcmds.sty 2011/11/09 v1.22 LaTeX kernel commands for general use (HO)
color.sty 1999/02/16
color.cfg 2007/01/18 v1.5 color configuration of teTeX/TeXLive
framed.sty 2011/10/22 v 0.96: framed or shaded text with page breaks
alltt.sty 1997/06/16 v2.0g defines alltt environment
inputenc.sty 2015/03/17 v1.2c Input encoding file
utf8.def 2015/06/27 v1.1n UTF-8 support for inputenc
t1enc.dfu 2015/06/27 v1.1n UTF-8 support for inputenc
ot1enc.dfu 2015/06/27 v1.1n UTF-8 support for inputenc
omsenc.dfu 2015/06/27 v1.1n UTF-8 support for inputenc
fontenc.sty
t1enc.def 2005/09/27 v1.99g Standard LaTeX file
xcolor.sty 2007/01/21 v2.11 LaTeX color extensions (UK)
color.cfg 2007/01/18 v1.5 color configuration of teTeX/TeXLive
listings.sty 2015/06/04 1.6 (Carsten Heinz)
lstmisc.sty 2015/06/04 1.6 (Carsten Heinz)
listings.cfg 2015/06/04 1.6 listings configuration
lstlang1.sty 2015/06/04 1.6 listings language file
lstlang2.sty 2015/06/04 1.6 listings language file
lstlang3.sty 2015/06/04 1.6 listings language file
upquote.sty 2012/04/19 v1.3 upright-quote and grave-accent glyphs in verbat
im
textcomp.sty 2005/09/27 v1.99g Standard LaTeX package
ts1enc.def 2001/06/05 v3.0e (jk/car/fm) Standard LaTeX file
ts1enc.dfu 2015/06/27 v1.1n UTF-8 support for inputenc
ts1cmr.fd 2014/09/29 v2.5h Standard LaTeX font definitions
supp-pdf.mkii
pdftexcmds.sty 2011/11/29 v0.20 Utility functions of pdfTeX for LuaTeX (HO)
ifluatex.sty 2010/03/01 v1.3 Provides the ifluatex switch (HO)
ifpdf.sty 2011/01/30 v2.3 Provides the ifpdf switch (HO)
epstopdf-base.sty 2010/02/09 v2.5 Base part for package epstopdf
grfext.sty 2010/08/19 v1.1 Manage graphics extensions (HO)
kvdefinekeys.sty 2011/04/07 v1.3 Define keys (HO)
kvoptions.sty 2011/06/30 v3.11 Key value format for package options (HO)
kvsetkeys.sty 2012/04/25 v1.16 Key value parser (HO)
etexcmds.sty 2011/02/16 v1.5 Avoid name clashes with e-TeX commands (HO)
epstopdf-sys.cfg 2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
t1cmtt.fd 2014/09/29 v2.5h Standard LaTeX font definitions
***********
答案1
最简单的解决方案是不使用lstlistings
,而是将代码放入一个块中eval=FALSE
:
\begin{frame}[fragile]
<<eval=FALSE>>=
rnorm(3)
@
<<eval=TRUE>>=
rnorm(3)
@
\end{frame}
更新
在我之前的回答中,我假设您希望listings
输出看起来像knitr
。现在我明白您希望knitr
输出看起来像listings
。嗯,这也是可能的。
最简单的方法是使用knitr
主题机制,请参阅https://github.com/yihui/knitr/blob/master/inst/examples/knitr-themes.Rnw。
首先,创建 CSS 文件,例如listings.css
:
.background {
color: #ffffcc;
}
.num {
color: #000000;
}
.str {
color: #008000;
}
.com {
color: #ff0000;
font-style: italic;
}
.opt {
color: #0000ff;
font-weight: bold;
}
.std {
color: #000000;
}
.kwa {
color: #0000ff;
font-weight: bold;
}
.kwb {
color: #0000ff;
font-weight: bold;
}
.kwc {
color: #0000ff;
font-weight: bold;
}
.kwd {
color: #0000ff;
font-weight: bold;
}
您可能需要进一步调整。然后将文件输入到您的rnw
文件中:
\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{listings}
\lstset{language=R,breaklines=true}
\definecolor{hellgelb}{rgb}{1,1,0.8}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{1,0,0}
\definecolor{colString}{rgb}{0,0.5,0}
\setbeamertemplate{navigation symbols}{}
\lstset{%
float=hbp,%
basicstyle=\ttfamily\small, %
identifierstyle=\color{colIdentifier}, %
keywordstyle=\color{colKeys}, %
stringstyle=\color{colString}, %
commentstyle=\color{colComments}, %
columns=flexible, tabsize=2, %
frame=single, extendedchars=true, %
showspaces=false, showstringspaces=false, %
numbers=left, numberstyle=\tiny, %
breaklines=true, backgroundcolor=\color{hellgelb}, %
breakautoindent=true, captionpos=b%
}
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
opts_chunk$set(fig.path = 'figure/listings-')
knit_theme$set("listings.css")
options(formatR.arrow = TRUE)
@
\begin{frame}[fragile]
\begin{lstlisting}
rnorm(3)
\end{lstlisting}
<<eval=TRUE>>=
rnorm(3)
@
\end{frame}
\end{document}
结果如下: