2

我正在用 LaTeX 写一份报告,嵌入几个 R 脚本,这些脚本以非连续的 Sweave 块报告。

我在 Stackoverflow 上发现了一些关于如何自定义Sweave列表包以突出显示代码和编号行的非常有用的评论。我修改了原始的sweave.sty包如下,以利用列表,同时避免过多的设置给我的 LaTeX 文档带来负担。

基本上,我在行号选项方面遇到了麻烦。按照配置,(firstnumber=last),行在文档中逐渐编号。如果我设置 firstnumber=auto ,编号从 1 在每个 R 代码块处重新开始。

\RequirePackage[T1]{fontenc}
\RequirePackage{graphicx,ae,fancyvrb}
\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}
\setkeys{Gin}{width=0.8\textwidth}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl, fontsize=\small}

\newenvironment{Schunk}{}{}

\usepackage{listings}
\newcommand{\indexfonction}[1]{\index{#1@\texttt{#1}}}
\usepackage[usenames,dvipsnames]{color}

\definecolor{gris90}{gray}{0.95}

\lstdefinelanguage{Renhanced}[]{R}{%
   sensitive,%
   morecomment=[l]\#,%
   morestring=[d][\color{RoyalPurple}]",%
   morestring=[d][\color{RoyalPurple}]',
  alsoletter={.\%_},
  alsoother={:_\$}}

\lstset{language=Renhanced,extendedchars=false,
  basicstyle=\small\ttfamily, 
  columns=flexible,
  commentstyle=\textsl,
  numbers=left, 
  numberstyle=\small \ttfamily,
  keywordstyle=\mdseries,
  showstringspaces=false,
  index=[1][keywords], 
  indexstyle=\indexfonction}


\lstnewenvironment{Sinput}[1][]{
 \lstset{%
   language={Renhanced},
   basicstyle=\small \ttfamily,
   columns=flexible,  
   frame=single,                             
   backgroundcolor=\color{gris90},           
   numbers=left, 
   numberstyle=\small \ttfamily,
   firstnumber=last,
   #1
 }
}{}
\lstnewenvironment{Soutput}[1][]{
 \lstset{%
   language={Renhanced},
   basicstyle=\small \ttfamily, 
   columns=flexible,
   numbers=right, 
   numberstyle=\tiny,
   firstnumber=last,
   #1
 }
}{}

我想找到一个中间解决方案,其中编号在同一脚本的不同块中进行,但在不同脚本的开头从 1 重新开始(可能通过手动指定)。

我认为,问题在于我无法手动指定列表的名称,因为 Sweave 在后台执行此操作。

欢迎提出建议!

4

1 回答 1

1

我实际上找到了一个伪解决方案。

在开始一段代码打开一个新脚本之前,输入以下代码行:

\begin{lstlisting}[firstnumber=1] 
\end{lstlisting}

这不会在文档中产生任何可见的输出,并将下一个块的行号重置为 1(或任何其他所需的数字)。

不用说,仍然欢迎任何更优雅的解决方案!

于 2011-04-06T15:28:21.383 回答