11

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.

1
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Mar 3, 2014 at 9:56

5 Answers 5

3

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

10

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

1
9

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

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

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

0

You can do this with the package fancyvrb defined the environment Verbatim. In my code, I use the invironment figure and especially apply \textcolor without affecting to the pre-formatting of the text inside Verbatim.


\documentclass{article}
\usepackage{fancyvrb,xcolor}

\begin{document}
\newcommand\hlca[1]{\textcolor{red}{\textbf{#1}}}
\newcommand\hlcb[1]{\textcolor{blue}{\textbf{#1}}}
\begin{figure}[h!]
\centering
\begin{SaveVerbatim}[commandchars=\\\{\}]{efms}
EFM1: \hlca{R9}  R10i T1  \hlcb{T2}    
EFM2: \hlca{R9}  R10i R11i R12  R13  R14 T1 \hlcb{T5}   
EFM3: R6i \hlca{R9}   R10i R11i R12  R13 R14 \hlcb{R15} T1 T6 T7
EFM4: \hlcb{R6i} \hlca{R7i}  R8i \hlca{R9} R10i R11i R12 R13 R14 T1 T6 T7
EFM5: \hlca{R7i} R8i  \hlcb{R15} T1 T6
EFM6: \hlcb{R6i} \hlca{R7i}  R8i T1 \hlcb{T5} T6  T7    
EFM7: \hlcb{R6i} \hlca{R7i}  R8i R11i R12 R13  R14  T1 \hlcb{T2}  T6  T7
\end{SaveVerbatim}
\fcolorbox{black}{TeaGreen}{\BUseVerbatim{efms}}
\caption{List of 7 EFMs concerning the production of external citrate}
\label{box:7-efms-external-citrate}
\end{figure}
\end{document}

Below is the result when running the code. Verbatim with fill color and text color.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .