At first an input such as
\blist{<statement>
<statement>
<statement>
<statement>}
can seem more appealing than
\begin{lstlisting}[<options>]
<statement>
<statement>
<statement>
<statement>
\end{lstlisting}
but eventually it doesn't reveal such. For one thing: \end{lstlisting}
is much more evident in the input than a single brace.
Besides, there are technical reasons why the "macro with argument" is difficult to implement for lstlisting
: this environment is pretty much like verbatim
(but does more complicated things) and so it can't go inside the argument to another command, if you want that it treats correctly all the characters which are special to LaTeX (braces, #
, $
and %
, in particular).
A good text editor can help, but also listings
features: if you want to give particular options for typesetting chunks of code, you can define a new environment:
\lstnewenvironment{blist}[1][]
{\lstset{<common options>,#1}}
{}
and then
\begin{blist}
<statement>
<statement>
<statement>
<statement>
\end{blist}
will be typeset applying the <common options>
. Not very harder to type and good for marking your input and making it easy to find the chunks of code that use that common setting. You can also add "local options" by saying
\begin{blist}[<local options>]
Best Answer
You can define an environment just as you would define a macro. Only instead of saying
\newcommand\mycmd[1]{...#1...}
you must writeHere the last two parameters say what should be put before and after the contents.
This means the first try would be to write
But this does not reflect the intended nesting of parameters and you actually only give the begin part of your environment, while the end part is the newline character in the fourth line.
So in your case it is probably better to first collect everything inside the environments body and then use it. This can be done with the
environ
package. It provides the command\NewEnviron
where you can use\BODY
to access those content. Assumingblock
is the one frombeamer
, you can do the following: