[Tex/LaTex] minipage with verbatim package doesn’t compile

minipageverbatim

This is my code:

\usepackage{verbatim}
\newenvironment{listing}
{\begin{minipage}{\linewidth}\verbatim}
{\endverbatim\end{minipage}}

However, it doesn't compile:

Runaway argument?
! File ended while scanning use of \next.
<inserted text>
                \par

What is wrong?

Best Answer

You have to do

\usepackage{verbatim}

\newenvironment{listing}
{\minipage{\linewidth}\verbatim}
{\endverbatim\endminipage}

There's almost the same example on page 3 of the package documentation, see the definition of the fred environment.

Note that \begin{minipage} executes (among other things) \minipage and similarly \end{minipage} executes \endminipage.