# how fancyvrb background color fill completely with fillcolor?

I'd like the whole Verbatim (fancyvrb) block to have a background color. How do I do it?

The fancyvrb package does define a fillcolor -- but that only colors the area around the margin -- not the background of the block itself.

-
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  karlkoeller Mar 3 at 9:56

You can use tcolorbox with listings package seemlessly. There by one can combine the advantages and features of both. Here is an example.

\documentclass{article}
\usepackage{tcolorbox,listings}
\lstdefinestyle{mystyle}{
basicstyle=\ttfamily,
numbers=left,
numberstyle=\tiny,
numbersep=5pt
}
\tcbuselibrary{listings,skins,breakable}
\newtcblisting{BGVerbatim}{
arc=0mm,
top=0mm,
bottom=0mm,
left=3mm,
right=0mm,
width=\textwidth,
boxrule=0.5pt,
colback=yellow,
spartan,
listing only,
listing options={style=mystyle},
breakable
}

\begin{document}
\begin{BGVerbatim}
abc def {

xyz
\end{BGVerbatim}
\end{document}


-

Use package listings instead. It supports page breaks and also fancyvrb:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\begin{document}

\begin{lstlisting}[backgroundcolor=\color{yellow},frame=single,basicstyle=\ttfamily]
foo
\end{lstlisting}

\end{document}


-

You can use the very powerful tcolorbox package, in combination with fancyvrb

\documentclass{article}
\usepackage{tcolorbox,fancyvrb,xcolor,tikz}
\tcbuselibrary{skins,breakable}
\newenvironment{BGVerbatim}
{\VerbatimEnvironment
\begin{tcolorbox}[
breakable,
colback=yellow,
spartan
]%
\begin{Verbatim}}
{\end{Verbatim}\end{tcolorbox}}

\begin{document}
\begin{BGVerbatim}[numbers=left,numbersep=6pt]
abc def {

xyz
\end{BGVerbatim}
\end{document}


The numbers=left option has been added just to show that you can pass fancyvrb options to the new environment.

-
it doesn't allow a pagebreak inside –  Herbert Mar 3 at 10:50
@Herbert It's easy to add the feature, as I did in my edit –  egreg Mar 3 at 12:31

You can do that with the fancybox package, which defines the same Verbatim environment.

You can define a new verbatim environment, let's say MyVerbatim with background and text colors.

\newenvironment{MyVerbatim}{\VerbatimEnvironment%
\noindent\begin{Sbox}
\begin{minipage}{\dimexpr\linewidth-2\fboxsep-2\fboxrule}
\begin{Verbatim}
}{%
\end{Verbatim}%
\end{minipage}%
\end{Sbox}%
\fcolorbox{black}{yellow}{\TheSbox}%
}


Change black and yellow to whatever you like.

MWE:

\documentclass{article}
\usepackage{fancybox}
\usepackage{xcolor}

\newenvironment{MyVerbatim}{\VerbatimEnvironment%
\noindent\begin{Sbox}
\begin{minipage}{\dimexpr\linewidth-2\fboxsep-2\fboxrule}
\begin{Verbatim}
}{%
\end{Verbatim}%
\end{minipage}%
\end{Sbox}%
\fcolorbox{black}{yellow}{\TheSbox}%
}

\begin{document}

\begin{MyVerbatim}
Hello

I have a background!
\end{MyVerbatim}

\end{document}


Output:

-