[Tex/LaTex] Create a new environment that uses a verbatim environment

colorenvironmentsverbatim

Consider the following:

\documentclass{article}
\usepackage{environ}
\usepackage{verbatim}
\usepackage{color}
\NewEnviron{vred}{%
\color{red}
\begin{verbatim}
  \BODY
\end{verbatim}
}
\begin{document}
\begin{vred}
test
\end{vred}
\end{document}

Compiling with pdflatex gives the error:

Runaway argument?
\end {document} 
! Paragraph ended before \next was complete.
<to be read again> 
                   \par 
l.16 

Best Answer

In this particular case the problem is a typical one: You can't pass verbatim content as an argument to another macro. This is indeed the case with using environ, since content is accumulated in the macro \BODY for processing later.

From the looks of your minimal example, it seems like you're after printing verbatim content using a specific colour (say red). For this I'd suggest defining a new verbatim environment with this specific quality/attribute. As such, fancyvrb can come in handy. Here's a MWE that illustrates this concept (taken, virtually verbatim, from the fancyvrb documentation (section 4.1.3 Customization of formatting, p 5 and 4.2.4 Personalized environments, p 18)):

enter image description here

\documentclass{article}
\usepackage{fancyvrb,xcolor}% http://ctan.org/pkg/{fancyvrb,xcolor}
\DefineVerbatimEnvironment%
  {MyVerbatim}{Verbatim}
  {formatcom=\color{red}}
\begin{document}
\begin{MyVerbatim}
First verbatim line.
Second verbatim line.
\end{MyVerbatim}
\end{document}
Related Question