使用 Sweave 和列表进行非连续行编号

发布于 2024-10-30 05:23:00 字数 1924 浏览 3 评论 0原文

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

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

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

\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 在后台执行此操作。

欢迎提出建议!

I'm writing a report in LaTeX, embedding several R scripts, which are reported in non-contiguous Sweave chunks.

I found some pretty useful comments on Stackoverflow on how to customize Sweave and the listings package to highlight code and numbering lines.
I modified the original sweave.sty package as follows, to take advantage of listings while avoiding to burden my LaTeX documents with too many settings.

Basically, I'm having trouble with the line numbering options. As configured, (firstnumber=last), the lines are numbered progressively across the document.
If I set firstnumber=auto , the numbering restarts from 1 at each chunk of R code.

\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
 }
}{}

I would like to find an intermediate solution, where the numbering progresses across different chunks of the same script, but restarts form 1 at the beginning of a different script (possibly by specifying it manually).

The trouble is, I think, that I cannot specify manually the name of the listings, because Sweave does it in the background.

Suggestions are welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2024-11-06 05:23:00

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

在开始打开新脚本的代码块之前,键入以下代码行:

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

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

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

I actually found a pseudo-solution.

Before beginning a chunk of code opening a new script, type the following lines of code:

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

This won't produce any visible output in the document and will reset the line numbering of the following chunk to 1 (or any other desired number).

Needless to say, any more elegant solutions are still welcome!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文