[Tex/LaTex] Verbatim environment with background color (PDFLaTeX and tex4ht)

backgroundscolorpdftextex4htverbatim

I need to make a verbatim environment with gray background color, such as:

colored background verbatim envrionment

That needs to work with PDFTeX and tex4ht on an older TeX installation (from 2009 I believe). My approach so far is

\documentclass{article}

\usepackage{framed,color,verbatim}
\definecolor{shadecolor}{rgb}{.9, .9, .9}

\newenvironment{code}%
   {\snugshade\verbatim}%
   {\endverbatim\endsnugshade}


\begin{document}
text

\begin{code}
line one
line two
\end{code}


\end{document}

which works when I run it with pdftex main.tex. But when I give the command htlatex main xhtml,4,next I get errors:

(/opt/texlive2011/texmf-dist/tex/generic/tex4ht/html4-math.4ht)) (./main.aux)
! You can't use `\prevdepth' in horizontal mode.
\nointerlineskip ->\prevdepth 
                              -\@m \p@ 
l.17 \end{code}

? 
! Emergency stop.
\nointerlineskip ->\prevdepth 
                              -\@m \p@ 
l.17 \end{code}

No pages of output.

What can I do to get the custom gray background verbatim environment as in the example (called "code") with PDFTeX and tex4ht with TeXlive from 2009?

Best Answer

The credits should go to Martin Scharrer. It is possible with the listings package:

\documentclass{article}
\usepackage{listings,color}

\definecolor{verbgray}{gray}{0.9}

\lstnewenvironment{code}{%
  \lstset{backgroundcolor=\color{verbgray},
  frame=single,
  framerule=0pt,
  basicstyle=\ttfamily,
  columns=fullflexible}}{}

\definecolor{shadecolor}{rgb}{.9, .9, .9}

\begin{document}
text

\begin{code}
line one
line two
\end{code}

\end{document}

This document works with tex4ht and pdftex.