# Verbatim environment with background color (PDFLaTeX and tex4ht)

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

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}

\newenvironment{code}%

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

-
If there was room for another tag, obsolete would be appropriate. –  lockstep May 1 '12 at 12:28
Why not using listings? It should contain support for background colors. –  Martin Scharrer May 1 '12 at 12:57

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

\begin{document}
text

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

\end{document}


This document works with tex4ht and pdftex.

-
do you need that \definecolor{shadecolor}{rgb}{.9, .9, .9} line? –  mangledorf Apr 25 at 22:05
@mangledorf probably not –  topskip Apr 26 at 5:42

Another solution is to use tex4ht hook system.

Put the definition of the environment to the code.sty file:

\ProvidesPackage{mycode.sty}
\RequirePackage{framed,color,verbatim}

\newenvironment{code}%


then create file code.4ht which is called only with tex4ht:

\renewenvironment{code}{\:tempa\verbatim}{\endverbatim\:tempb}
\ConfigureEnv{code}{\HCode{<div class="code">}}{\HCode{</div>}}{}{}
\Css{.code{
border:1px solid black;
background-color: \#999;
}}


you need to redefine code environment, remove code that cause problems and put hooks \:tempa and \:tempb. Configurations from \ConfigureEnv will be inserted here.

-
I tried your solution, but I get an error during processing: ! Undefined control sequence. <argument> \Verb:N This may have to do with the quirks of verbatim. Is your solution supposed to work out of the box? I am using Texlive 2013 and the tex4ht version it provides. –  stefano May 21 at 14:45
@stefano I have access only to TeXlive 2012 at this moment, compilation is without problems –  michal.h21 May 21 at 15:26

Another solution is to check if I need the PDF or the HTML document.

\documentclass{article}
\usepackage{ifpdf,verbatim}

\ifpdf
\usepackage{framed,color}

\newenvironment{code}%

\else

\newenvironment{code}
{\HCode{<div class='code'>}\verbatim}
{\endverbatim\HCode{</div>}}

\fi

\begin{document}
text

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

\end{document}

-

Of course you can always use adjustbox to add the background color. Here you have to be careful not to take the \begin{adjustbox}{..} form but the \adjustbox{..}\bgroup .. \egroup form. I added some padding and margin.

\documentclass{article}

\usepackage{xcolor}
\usepackage{verbatim}

\newenvironment{code}%
{\endverbatim\endvarwidth\egroup}

\newenvironment{code2}%
{\endverbatim\endminipage\egroup}

\begin{document}
text text text text text text text text text
text text text text text text text text text

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

text text text text text text text text text
text text text text text text text text text

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

text text text text text text text text text
text text text text text text text text text

\end{document}


-
Mmm, for htlatex this adds __ with the final minipage/varwidth width at the beginning of the line one`. I'm trying to fix this. –  Martin Scharrer May 1 '12 at 14:07