【问题标题】:Non-continuous line numbering with Sweave and listings使用 Sweave 和列表进行非连续行编号
【发布时间】:2011-04-06 13:23:41
【问题描述】:

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

我在 Stackoverflow 上发现了一些非常有用的 cmets,介绍了如何自定义 Sweavelistings 包以突出显示代码和编号行。 我修改了原来的 sweave.sty 包如下,以利用 listings 同时避免过多的设置给我的 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 在后台执行此操作。

欢迎提出建议!

【问题讨论】:

    标签: r latex sweave listings


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多