# Use Listing in newenvironment

I am trying to do a new environment or command (i tried both) where i can put in the name, description and code of a Method in c++ and it gives me a new section with the MethodName, the code listed and the description below. But i am not able to use the listings environment in a new command or new environment.. Consider my MWE

 \documentclass[a4paper,11pt]{article}
\usepackage{listings}

\newcommand{\method}[3]
{
\section{#1}\label{#1}
\begin{lstlisting}
#2
\end{lstlisting}
#3
}

\begin{document}
\method{MethodName}{Method Void MethodName()}{Description of the Method blablabla}
\end{document}


How can I fix this?

I would prefer to do it using a macro since I don't need an ending.

• Take a look at for example \newenvironment fails with special listings environment. You can't wrap up lstlistings (or any other verbatim-like environment) inside another command, you have to use dedicated approaches. Also, you really need to use an environment rather than a command for verbatim to work. – Joseph Wright Jul 2 '13 at 7:28
• yes but how can i use Listing and still get the new section and description added? I think a new listing-environment won't do the trick.. :( – Kathiieee Jul 2 '13 at 7:39
• The \lstnewenvironment approach will work just fine. You cannot wrap listings environments like that anyway. Besides, \macro{}{}{} makes the source code very hard to read. Environments provide a much better structure. – daleif Jul 2 '13 at 7:51
• Kathiieee, @JosephWright: For short verbatim which doesn't include } itself a macro solution would be possible, if you don't read the verbatim material as macro argument. For example \newcommand{\method}[1]{\section{#1}\label{#1}\lstinline would do it for this particular MWE. Here the second argument is read by \lstinline and the third argument is just kept as is. However, if as soon you want to modify the material after the verbatim it's getting complicated, because you need to execute a macro after the \lstinline. I will have a look later at home at it. – Martin Scharrer Jul 2 '13 at 7:56
• @MartinScharrer I know that for a subset of verbatim cases you can get around this, but the scenario above wants an argument after the verbatim one. I guess one could use xparse to handle things, but even then I'm wary. – Joseph Wright Jul 2 '13 at 7:58

Verbatim material can't be placed in a macro argument because then the material got already parsed by TeX and the switching to verbatim mode comes to late. For custom environments listings provides \lstnewenvironment which also allows to add your own code before and after the verbatim material. There is unfortunatly no equivalent for macros.

This solution requires to provide the description before the code, store it in a temporary macro and place that it after the code.

\documentclass{article}
\usepackage{listings}

\lstnewenvironment{method}[2]
{\section{#1}\label{#1}\def\MethodDescription{#2}}
{\MethodDescription}

\begin{document}

\begin{method}{MethodName}{Description of the Method blablabla}
Method Void MethodName()
\end{method}

\end{document}

• This works, thanks. But i want to go on in the method environment using a parameter environment (which provides a list and works itself). So I would need to do all this BEFORE I type the code and this is the best I can get? \begin{method}{MethodName}{Description of the Method blablabla \begin{parameters} blablabla \end{parameters}} instead of \begin{method}{MethodName}{Description of the Method blablabla} Method Void MethodName() \begin{parameters} blablabla \end{parameters} \end{method} – Kathiieee Jul 2 '13 at 8:14