是否可以使用 \verbatiminput 读取输入文件并添加行号?

是否可以使用 \verbatiminput 读取输入文件并添加行号?

我知道可以使用\usepackage{listings}支持行号的。我将其用于 pdf 文件。但我还使用 生成 HTML Latex2html,并且l2h不支持此包。它仅支持\verbatiminput

因此,在我的 latex 文件(从同一源生成 pdf 和 html)中,我有以下内容

\documentclass[]{book}%
\usepackage{html}
\usepackage{verbatim}
\usepackage{listings}
\begin{htmlonly}
   \providecommand{\lstinputlisting}[2][]{\verbatiminput{#2}}
\end{htmlonly}
\lstset{numbers=left}
\begin{document}
  \lstinputlisting{source.txt}
\end{document}

因此,当我输入时,pdflatex foo.tex我会得到行号,但是当我输入时,latex2html foo.tex我只能得到源代码,但没有行号。

我必须对这个文件使用 latex2html。我知道tex4ht支持这个包,但出于其他原因,我需要l2h在这里使用。

在 Latex 中是否有一个技巧可以做到这一点?我知道我可以在外部生成带有行号的 source.txt,然后再次使用该文件包含该文件,\verbatiminput但我想首先看看是否可以\verbatiminput直接在 Latex 中使用。

参考:

http://www.ctan.org/pkg/verbatim

http://tug.org/pipermail/texhax/2005-March/003659.htm

供参考,

verbatimfiles.perl在 Moore 博士的上述帖子中,他引用了分发版中的文件l2h来进行编辑以添加对 l2h 中的行号的支持。

我不懂任何 Perl(l2h 是用 Perl 编写的),也不知道如何开始更改此文件以执行此操作。这是一个小文件。我将粘贴在这里,希望可能了解 Perl 的人能够发现,只需更改一行即可在左侧添加行号。以下是文件/usr/share/latex2html/styles/verbatimfiles.perl

# $Id: verbatimfiles.perl,v 1.6 1999/04/09 18:16:51 JCL Exp $
# verbatimfiles.perl
#   Jens Lippmann <[email protected]> 6-FEB-96
#
# Extension to LaTeX2HTML to support verbatim.sty/verbatimfiles.sty.

package main;

&do_require_package("verbatim");

sub do_cmd_verbatimfile {
    &do_cmd_verbatiminput;
}

sub do_cmd_verbatimlisting {
    local($_,$outer);
    local($counter) = 0;

    # Read in file, get markup ready.
    $outer = &do_cmd_verbatiminput;

    # Postprocess verbatim content.
    $_ = $verbatim{$global{'verbatim_counter'}};

    #insert numbers for every line
    #but not the first line if it's empty (LaTeX'ism?)
    local($firstemptyline);
    $firstemptyline = $1 if s/^([ \t]+\n)//;

    #and not the last end of line
    s/\n$//;
    s/(^|\n)/$1.sprintf("%4d ",++$counter)/ge;

    #add the stuff from the first(if empty) and last line also
    $verbatim{$global{'verbatim_counter'}} = $first.$_;
    $outer;
}

&process_commands_wrap_deferred (<<_RAW_ARG_DEFERRED_CMDS_);
verbatimfile # {}
verbatimlisting # {}
_RAW_ARG_DEFERRED_CMDS_

1;              # Must be last line

do_cmd_verbatimlisting顺便说一句,我尝试在 Latex 中像这样使用上面提到的内容:

\verbatimlisting{source.txt}

但什么也没发生。l2h 不理解该命令。因此,根据上述源代码,我不确定 l2h 是否支持行号,但需要弄清楚如何使用它。

答案1

verbatimbox软件包提供了向文件列表添加预处理命令(例如,行号等)的功能。但由于它将其放入一个框中,因此仅限于一页。这是我在下面展示的第一件事(\verbfilebox将文件内容放入一个框中,\theverbbox然后重新整理)。

然后,我从该包中获取命令并手动修改它(将其命名\verbfilelist)以提供跨页面边界的逐字列表(如下图所示),同时保留提供行号的能力。

\documentclass{article}
\usepackage{verbatimbox}
\begin{document}
\verbfilebox[\arabic{VerbboxLineNo}:\hspace{1ex}]{junk.tex}
\theverbbox
%    
\makeatletter
\newcommand\verbfilelist[2][]{%
  \setcounter{VerbboxLineNo}{0}%
  \def\verbatim@processline{%
    {\addtocounter{VerbboxLineNo}{1}%
    #1\setbox0=\hbox{#1\the\verbatim@line}%
    \hsize=\wd0 \the\verbatim@line\par}}%
  \verbatiminput{#2}
  \let\verbatim@processline\sv@verbatim@processline
}
\makeatother

\verbfilelist[\arabic{VerbboxLineNo}:\hspace{1ex}]{junk.tex}
\end{document}

This
is
extra
text
to
force
a
page
break
in
the
listing

在此处输入图片描述

相关内容