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

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

4 Answers 4

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
    
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
add comment
up vote 3 down vote accepted

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}
share|improve this answer
add comment

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

share|improve this answer
    
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
add comment

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.