3

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 
  • 1
    This is defined as the age-old problem of trying to pass verbatim content as a macro argument, which requires special treatment. Perhaps you could elaborate on what you're after really. – Werner Apr 23 '13 at 19:12
  • I want to make an environment for red verbatim text.. – Håkon Hægland Apr 23 '13 at 19:18
  • 1
    With listings: \lstnewenvironment{vred}{<something before the verbatim part>}{<something after>}. This command also allows to add arguments the same way \newenvironment does. – cgnieder Apr 23 '13 at 21:04
7

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}
| improve this answer | |
  • 1
    Thank you! Could you pass a parameter (#1) to the environment such that before the red verbatim text we also insert a \subsection*{#1}.. (which should not be in red) ? – Håkon Hægland Apr 23 '13 at 19:33
2

Regarding my last comment to Werners answer, I found the following to work:

\documentclass{article}
\usepackage{fancyvrb,xcolor}
\newenvironment{MyVerbatim}[1]{
  \subsection*{#1}\VerbatimEnvironment
  \begin{Verbatim}[formatcom=\color{red}]}{\end{Verbatim}}
\begin{document}
\begin{MyVerbatim}{A verbatim subsection}
 First verbatim line.
 Second verbatim line.
\end{MyVerbatim}
\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.