1

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.

Martin Scharrer
  • 262,582
kraeki
  • 333

1 Answers1

5

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 ( https://texfaq.org/FAQ-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{..}.

David Carlisle
  • 757,742
Ulrike Fischer
  • 327,261