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.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI'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.
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]
\addcontentsline{toc}{chapter}{The first section name}
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.
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:
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.
.