Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I know one can use \usepackage{listings} which supports line numbers. And I use that for pdf files. But I also generate HTML using Latex2html, and l2h does not support this package. It only supports \verbatiminput.

Therefore in my latex file (which generates both pdf and html from the same source), I have the following

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

So when I type pdflatex foo.tex I get line numbers, but when I type latex2html foo.tex I only get the source, but no line numbers there.

I have to use latex2html on this file. I know tex4ht supports this package, but for other reasons, I need to use l2h here.

Is there a trick to do this inside Latex? I know I can generate the source.txt with line numbers externally, and then include that file again using \verbatiminput but wanted to see first if possible to do inside Latex using \verbatiminput directly.

references:

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

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

Fyi,

In the above post by Dr Moore he refers to the file verbatimfiles.perl in the l2h distribution to edit to add this support for line numbers in l2h.

I do not know any Perl (l2h is written in Perl) and do not know how to start to change this file to do this. It is a small file. I'll paste here in the hope someone here who might know Perl can spot as easy one line change to to add a line number to the left. Here is the file /usr/share/latex2html/styles/verbatimfiles.perl

# $Id: verbatimfiles.perl,v 1.6 1999/04/09 18:16:51 JCL Exp $
# verbatimfiles.perl
#   Jens Lippmann <lippmann@rbg.informatik.tu-darmstadt.de> 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

btw, I tried to use do_cmd_verbatimlisting mentioned above in Latex like this:

\verbatimlisting{source.txt}

but nothing happened. l2h did not understand the command. So I am not sure if the support for line numbers is there in l2h based on the above source code, but need to figure how to use it or not.

share|improve this question
    
The executables in the`tex4ht` bundle support the listings package with line numbers –  cmhughes Jan 18 '13 at 17:06
    
@cmhughes, thank you. Yes I know and I mentioned this in my post. I like Tex4ht also. I am actually changing all my makefiles to use either Latex2html or Tex4ht for the build. This way, I can choose either one to generate HTML and compare the output until I decide which to pick. l2h is a little more flexible when it come to breaking pages and making toc for each page and such, but tex4ht seems to support more packages. –  Nasser Jan 19 '13 at 11:50

1 Answer 1

up vote 2 down vote accepted

The verbatimbox package has the provisions to add preconditioning commands (e.g., line numbers, etc.) to a file listing. But because it puts it into a box, it is restricted to one page. That is the first thing I show below (\verbfilebox places the file contents in a box, \theverbbox regurgitates it).

Then, I took the command from that package and by hand modified it (naming it \verbfilelist) to give a verbatim listing across page boundaries (shown in image below), while preserving the ability to give line numbers.

\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

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.