# 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.

-

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 2 at 13:16

## 1 Answer

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{..}.

-