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.

In an existing document which is not to be modified (except for the preamble), all instances of \begin{verbatim}...\end{verbatim} should have their contents rendered not only in a monospaced font but also on a grey background.

How can this be done?

share|improve this question
    
Would this suffice: verbatim with black background and white text? –  azetina Jul 4 '12 at 19:11
    
@azetina no, it wouldn't. –  sampablokuper Jul 4 '12 at 19:46
add comment

1 Answer 1

up vote 3 down vote accepted

enter image description here

\documentclass{article}

\usepackage{color}

\let\oldv\verbatim
\let\oldendv\endverbatim

\def\verbatim{\par\setbox0\vbox\bgroup\oldv}
\def\endverbatim{\oldendv\egroup\fboxsep0pt \noindent\colorbox[gray]{0.8}{\usebox0}\par}

\begin{document}

text text text text text 
text text text text text 
text text text text text 
text text text text text 
text text text text text 
\begin{verbatim}
1
12
123\foo{\ $ &
}
\end{verbatim}
text text text text text 
text text text text text 
text text text text text 
text text text text text 
text text text text text 

\end{document}
share|improve this answer
    
However this prevents page breaks. Maybe an approach with mdframed? –  egreg Jul 4 '12 at 19:45
    
@egreg true, I meant to mention that. This question just got asked with a nice looking answer tex.stackexchange.com/questions/15940/… –  David Carlisle Jul 4 '12 at 19:48
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.