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'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.

share|improve this question
    
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

4 Answers 4

up vote 4 down vote accepted

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}

enter image description here

share|improve this answer

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]
\addcontentsline{toc}{chapter}{The first section name}
foo
\end{lstlisting}

\end{document}

enter image description here

share|improve this answer

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.

enter image description here

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

enter image description here

share|improve this answer

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.