# Passing listing-environment as option to command [duplicate]

I have a simple command which takes one argument. Now I want to pass a small listing-environment to the command.

Here is the code:

\documentclass{article}
\usepackage{listings}
\begin{document}

\newcommand{\test}[1]{ #1 }

\test
{%
\begin{lstlisting}
asdf
\end{lstlisting}
}%

\end{document}


While compiling I get this error:

Package Listings Warning: Text dropped after begin of listing on input line 12.

(/usr/share/texmf-texlive/tex/latex/base/omscmr.fd))
*


Any suggestions would be great appreciated.

-

## locked by Joseph Wright♦Nov 27 '14 at 6:55

This post has been locked due to the high amount of off-topic comments generated. For extended discussions, please use chat.

## marked as duplicate by Jubobs, Jesse, Martin Schröder, Sean Allred, Marco DanielMar 2 '14 at 17:33

Don't do it. lstlisting is a verbatim-like environment and must do a lot of \catcode magic which will not work if you use it in an argument ( http://www.tex.ac.uk/cgi-bin/texfaq2html?label=verbwithin ). Even if you perhaps can get it to work for some case it will be prone to break in other cases. What you can try is to put your listing in an external file and then use \lstlistinginput{..}.