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



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

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, 2013 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, 2013 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, 2013 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. Jul 2, 2013 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, 2013 at 7:58

1 Answer 1


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.




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

  • 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, 2013 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.