9

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?

2
  • If there was room for another tag, obsolete would be appropriate.
    – lockstep
    Commented May 1, 2012 at 12:28
  • 3
    Why not using listings? It should contain support for background colors. Commented May 1, 2012 at 12:57

4 Answers 4

19

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.

3
  • do you need that \definecolor{shadecolor}{rgb}{.9, .9, .9} line? Commented Apr 25, 2014 at 22:05
  • @mangledorf probably not
    – topskip
    Commented Apr 26, 2014 at 5:42
  • is it possible to make this put the gray backgroup that extends only up to the text inside? instead the whole width of the page? Similar to the answer here Thanks.
    – Nasser
    Commented Mar 14, 2022 at 12:07
9

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}
\definecolor{shadecolor}{rgb}{.9, .9, .9}

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

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.

2
  • 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
    Commented May 21, 2014 at 14:45
  • @stefano I have access only to TeXlive 2012 at this moment, compilation is without problems
    – michal.h21
    Commented May 21, 2014 at 15:26
4

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

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

\ifpdf
\usepackage{framed,color}
\definecolor{shadecolor}{gray}{0.9}

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

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

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{adjustbox}
\usepackage{verbatim}
\definecolor{shadecolor}{rgb}{.9, .9, .9}

\newenvironment{code}%
   {\par\noindent\adjustbox{margin=1ex,bgcolor=shadecolor,margin=0ex \medskipamount}\bgroup\varwidth\linewidth\verbatim}%
   {\endverbatim\endvarwidth\egroup}

\newenvironment{code2}%
   {\par\noindent\adjustbox{margin=1ex,bgcolor=shadecolor,margin=0ex \medskipamount}\bgroup\minipage\linewidth\verbatim}%
   {\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}

Result

1
  • Mmm, for htlatex this adds __ with the final minipage/varwidth width at the beginning of the line one. I'm trying to fix this. Commented May 1, 2012 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .