# Color Text and BG of Verbatim, without affecting fancyvrb line numbers

The MWE below aims to color the text and background of a Verbatim environment.

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{color}
\usepackage{calc}

\definecolor{PYtxt}{rgb}{0.97,0.97,0.95}
\definecolor{PYbgc}{rgb}{0.15,0.16,0.13}

\newsavebox{\PYbgbox}
\newenvironment{PYcolorbox}%
{\noindent%
\begin{lrbox}{\PYbgbox}%
\begin{minipage}{\linewidth-2\fboxsep}%
\color{PYtxt}}%
{\ignorespacesafterend%
\end{minipage}%
\end{lrbox}%
\colorbox{PYbgc}{\usebox{\PYbgbox}}}

\begin{document}

\begin{PYcolorbox}
\begin{Verbatim}[commandchars=\\\{\},numbers=left,firstnumber=1,stepnumber=1]
File "<stdin>", line 1
1+
^
SyntaxError: invalid syntax
\end{Verbatim}
\end{PYcolorbox}

\end{document}


The problem with the code above is that line numbers produced by fancyvrb are also colored white, and cannot be seen when compiled.

How may I do both of the following (but not at the same time):

• independently change the text color and line number color?
• expand PYcolorbox to the left to include the (white) line numbers?

Alternative code method: adding formatcom=\color{white} to the Verbatim arguments yields white text (including line numbers), but also affects the bottom margin of the color background.

The goal would be to use this in tandem with Pygments.

Any help would be appreciated.

With a redefinition of \theFancyVerbLine you can change to color for the numbers independently; a little example:

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

\definecolor{PYtxt}{rgb}{0.97,0.97,0.95}
\definecolor{PYbgc}{rgb}{0.15,0.16,0.13}

\newsavebox{\PYbgbox}
\newenvironment{PYcolorbox}%
{\noindent%
\renewcommand\theFancyVerbLine{%
\textcolor{red!80!black}{\small\arabic{FancyVerbLine}}}%
\begin{lrbox}{\PYbgbox}%
\begin{minipage}{\linewidth-2\fboxsep}%
\color{PYtxt}}%
{\ignorespacesafterend%
\end{minipage}%
\end{lrbox}%
\colorbox{PYbgc}{\usebox{\PYbgbox}}}

\begin{document}

\begin{PYcolorbox}
\begin{Verbatim}[commandchars=\\\{\},numbers=left,firstnumber=1,stepnumber=1]
File "<stdin>", line 1
1+
^
SyntaxError: invalid syntax
\end{Verbatim}
\end{PYcolorbox}

\end{document}


I wonder, however, if you wouldn't be better off using a package such as minted or listings which offer you colored backgrounds out of the box. Specially minted might be useful here since it uses Pygments.