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.

For reasons unclear to me, fancyvrb doesn't have a background option. However, the minted package, which it is based on, has one, which suggests it should not be too difficult to add.

The relevant code from minted.sty might be

\newenvironment{minted@colorbg}[1]{
  \def\minted@bgcol{#1}
  \noindent
  \begin{lrbox}{\minted@bgbox}
  \begin{minipage}{\linewidth-2\fboxsep}}
 {\end{minipage}
  \end{lrbox}%
  \colorbox{\minted@bgcol}{\usebox{\minted@bgbox}}}

I've only a sketchy idea what this code is doing, but I think it is defining the bgcolor option.

Both How to change the background color of \framebox?, and Background color in Verbatim seem relevant, but don't answer this question, at least the way I would like.

I also found http://groups.google.com/group/latexusersgroup/browse_thread/thread/c8c2e5dd1e9ff5cf, which has the following code

\documentclass{report}
\usepackage{fancybox, fancyvrb, calc}
\usepackage[svgnames]{xcolor}

\newenvironment{colframe}{%
  \begin{Sbox}
    \begin{minipage}
      {\columnwidth-\leftmargin-\rightmargin-2\fboxsep-2\fboxrule-4pt}
    }{%
    \end{minipage}
  \end{Sbox}
  \begin{center}
    \fcolorbox{black}{LightSteelBlue}{\TheSbox}
  \end{center}
}

\begin{document}  
  \begin{colframe}
    \begin{Verbatim}[frame=single]
      auto eth0
    \end{Verbatim}
  \end{colframe}
\end{document}

For some reason this produces a double line round the box, but is in the ballpark of what I want. I would prefer a bgcolor option like minted has, so one can do something like the following code. I spent some time fiddling with this, but quickly realised I had no idea what I was doing. Maybe I'll learn how to program in LaTeX someday, but it probably won't be today. Thanks.

\documentclass{article}
\usepackage{xcolor}
\definecolor{bg}{rgb}{0.95, 0.95, 0.95}
\usepackage{minted} % syntax coloring.
\newminted{c}{frame=single, bgcolor=bg}
\begin{document}
\begin{ccode*}{}
for(int i=0; i< 5; i++)
    x[i];
\end{ccode*}
\end{document}
share|improve this question
    
Please have a look at this question: tex.stackexchange.com/questions/27673/… –  Marco Daniel Dec 30 '11 at 8:46
    
@MarcoDaniel: Did you mean to point me to mdframed? Or something else? –  Faheem Mitha Dec 30 '11 at 8:54
    
You can use the package framed in combination with the etoolbox command \BeforeBeginEnvironment. –  Marco Daniel Dec 30 '11 at 8:59
    
Be aware that the background colour option in minted is implemented quite badly. It breaks under various circumstances. I have no idea how to implement it better, though (but TikZ could help …). –  Konrad Rudolph Dec 30 '11 at 14:46
    
@KonradRudolph: Thanks for the comment. How could TikZ help? And did you ask for feedback/suggestions about the minted background thing? –  Faheem Mitha Dec 31 '11 at 11:19

2 Answers 2

up vote 7 down vote accepted
\documentclass[border=2]{standalone}
\usepackage[svgnames]{xcolor}
\usepackage{fancyvrb}


\makeatletter
\newif\ifFV@bgcolor
\newbox\FV@bgbox
\define@key{FV}{bgcolor}{\FV@bgcolortrue\def\FV@bgcolor{#1}}

\def\FV@BeginVBox{%
  \leavevmode\ifFV@bgcolor\setbox\FV@bgbox=\fi
  \hbox\ifx\FV@boxwidth\relax\else to\FV@boxwidth\fi\bgroup
  \ifcase\FV@baseline\vbox\or\vtop\or$\vcenter\fi\bgroup}
\def\FV@EndVBox{\egroup\ifmmode$\fi\hfil\egroup
  \ifFV@bgcolor\colorbox{\FV@bgcolor}{\box\FV@bgbox}\fi}
\makeatother

\begin{document}
\begin{BVerbatim}[bgcolor=LightSteelBlue]
abc
def
\end{BVerbatim}
\end{document}

Here's the result:

enter image description here

Add options to BVerbatim to have a fixed width instead of the minimum one.

Change the code into

\makeatletter
\newif\ifFV@bgcolor
\newbox\FV@bgbox
\define@key{FV}{bgcolor}{\FV@bgcolortrue\def\FV@bgcolor{#1}}
\define@key{FV}{framecolor}{\FV@bgcolortrue\def\FV@framecolor{#1}}
\def\FV@framecolor{white}

\def\FV@BeginVBox{%
  \leavevmode\ifFV@bgcolor\setbox\FV@bgbox=\fi
  \hbox\ifx\FV@boxwidth\relax\else to\FV@boxwidth\fi\bgroup
  \ifcase\FV@baseline\vbox\or\vtop\or$\vcenter\fi\bgroup}
\def\FV@EndVBox{\egroup\ifmmode$\fi\hfil\egroup
  \ifFV@bgcolor\fcolorbox{\FV@framecolor}{\FV@bgcolor}{\box\FV@bgbox}\fi}
\makeatother

to get a new key framecolor (default white, so no frame):

\begin{BVerbatim}[bgcolor=LightSteelBlue,framecolor=black]
abc
def
\end{BVerbatim}

Comments on the code

Let's look at the code for adding a background color (the extension for the frame color is very similar).

I add a conditional (\ifFV@bgcolor) and a box register (\FV@bgbox) and a key for the FV family; when the user says bgcolor=somecolor, the conditional will be set to true and somecolor is stored in \FV@bgcolor.

Then I redefine \FV@BeginVBox which is responsible for starting the box for a BVerbatim environment; its definition starts with

\leavevmode\hbox...

so what I do is to change it into

\leavevmode\ifFV@bgcolor\setbox\FV@bgbox=\fi\hbox...

In this way, when the conditional is true, the box will be stored in our new register, instead that typeset immediately.

Now the end part: \FV@EndVBox usually ends with

...\hfil\egroup}

where this \egroup closes the \hbox construction; if the conditional is true, this wouldn't typeset the box, so I make it into

...\hfil\egroup
\ifFV@bgcolor\colorbox{\FV@bgcolor}{\box\FV@bgbox}\fi}

which will typeset the box over a suitably colored background.

share|improve this answer
1  
egreg: With the following the result is rather weird (no margins and no background color). And because of \colorbox, the outcome doesn't break across pages. \CustomVerbatimEnvironment{myverbatim}{Verbatim}{% numbers=left,numberblanklines=false, firstnumber=last,frame=single,rulecolor=\color{purple}, framerule=1pt,framesep=7pt,formatcom=\color{blue}, xrightmargin=\rightmargin,xleftmargin=\leftmargin, commandchars=\|\(\),showtabs=true,commentchar=&% } I am soryy, it won't fit here properly. –  Ahmed Musa Dec 31 '11 at 5:20
1  
That's impressive. Would you consider annotating your answer for the TeX-illiterate? I find LaTeX code hard to understand. Also, is BVerbatim necessary because it puts the environment in a TeX box? Lastly, how would one put a line around the frame? frame=single does not do anything. –  Faheem Mitha Dec 31 '11 at 6:22
    
@AhmedMusa The background color is intended only for the BVerbatim environment. I'm afraid that patching fancyvrb to allow for multipage background colored verbatim environments is too difficult. –  egreg Dec 31 '11 at 10:15
    
@FaheemMitha frame=single doesn't work with BVerbatim. However I'll add a small patch to my answer. –  egreg Dec 31 '11 at 10:25
    
Nice work. Could this reasonably added to fancyvrb functionality, or is it too fragile? Also, maybe annotating everything is too much work, but how about a sample? –  Faheem Mitha Dec 31 '11 at 11:18

This is not a complete answer to your question, just a way how to get rid of the double frame. To get rid of the "inside" frame, just remove the [frame=single] optional argument of the Verbatim environment, that is, use

\begin{Verbatim}

instead of

\begin{Verbatim}[frame=single]

To get rid of the "outside" frame, replace \fcolorbox by simply \colorbox:

\colorbox{LightSteelBlue}{\TheSbox}

In that case, you should also change the calculation of the minipage width, since you do not need to subtract the thickness of the frame. The complete code without the double frame will then look like this:

\documentclass{report}
\usepackage{fancybox, fancyvrb, calc}
\usepackage[svgnames]{xcolor}

\newenvironment{colframe}{%
  \begin{Sbox}
    \begin{minipage}
      {\columnwidth-\leftmargin-\rightmargin-4pt}
    }{%
    \end{minipage}
  \end{Sbox}
  \begin{center}
    \colorbox{LightSteelBlue}{\TheSbox}
  \end{center}
}

\begin{document}  
  \begin{colframe}
    \begin{Verbatim}
      auto eth0
    \end{Verbatim}
  \end{colframe}
\end{document}

That will create:

color box around verbatim

share|improve this answer
    
Thanks Jan. Actually, I would like the option of having one frame. Of course, this would need to be on the boundary where the color ends, otherwise it will look funny. Is this possible. BTW, I'm surprised nobody has answered the original question. I thought it seemed straightforward for people with the necessary knowledge/expertise. Perhaps I'm missing something. –  Faheem Mitha Dec 30 '11 at 6:39

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.