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


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.





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;


sub do_cmd_verbatimfile {

sub do_cmd_verbatimlisting {
    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?)
    $firstemptyline = $1 if s/^([ \t]+\n)//;

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

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

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

1;              # Must be last line

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


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.

  • The executables in thetex4ht 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

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.

    \hsize=\wd0 \the\verbatim@line\par}}%



enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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