# \newenvironment fails with special listings environment

I want to use the showexpl, but define a new environment with predefined settings. But all I get is an emergency stop.

\documentclass{scrbook}
\RequirePackage{showexpl}
\lstdefinestyle{demoLatexStyle}{
basicstyle=\small\ttfamily, % Standardschrift
numbers=none,               % Ort der Zeilennummern
frame=none,
}
\begin{document}
\newenvironment{showdemo}[1][]{\LTXexample[style=demoLatexStyle,#1]}{\endLTXexample}

\begin{LTXexample}[style=demoLatexStyle]
\LaTeX{} \LaTeX{}
\end{LTXexample}

\begin{showdemo}
\LaTeX{} \LaTeX{}
\end{showdemo}

\end{document}


\openout3 = testdemo.tmp'. Package Listings Warning: Text dropped after begin of listing on input line 24. ! Emergency stop. <> testdemo.tex ** (job aborted, no legal \end found)

Any hint what I am doing wrong?

-
See if the \lstnewenvironment command works any better, as shown in this similar question. –  Mike Renfro Sep 29 '11 at 20:36
I think it has to do with there being code within a verbatim environment. –  Peter Grill Sep 29 '11 at 20:37
@Matthias: Does the link Mike linked to solve your problem. If not, please let us know. –  Peter Grill Sep 29 '11 at 23:42
@P. Grill: it uses \lstnewenvironment which is not applicable in my case as can also be seen in the comment of the first answer. –  Matthias Pospiech Sep 30 '11 at 18:44

Section 4.16 of the listings package specifies that to define new environments you need to use the following with syntax similar to LaTeX's \newenvironment.

\lstnewenvironment
{<name>}[<number>][<optional default arg>]
{<starting code>}
{<ending code>}


So, if you replace the \newenvironment with the following:

\lstnewenvironment{showdemo}[1][]{%
\lstset{style=demoLatexStyle,#1}}{}%


you get the desired result:

Update

You could also use LTXinputExample and add the necessary code in separate files, or use the filecontents package. Since you asked for an environment below I have defined showdemoEnv, but I think the macro version showdemo is probably better in this case:

\documentclass{scrbook}
\RequirePackage{showexpl}
\lstdefinestyle{demoLatexStyle}{
basicstyle=\small\ttfamily, % Standardschrift
numbers=none,               % Ort der Zeilennummern
frame=none,
}

\newcommand{\TempFileName}{\jobname.filecontents.tmp}%
\usepackage{filecontents}
\begin{filecontents*}{\TempFileName}
\LaTeX{} \LaTeX{}
\end{filecontents*}

\newenvironment{showdemoEnv}[2][]{% Environment version
\LTXinputExample[style=demoLatexStyle,#1]{#2}%
}{%
% Add any end environemnt code here.
}%

\newcommand{\showdemo}[2][]{% Macro version
\LTXinputExample[style=demoLatexStyle,#1]{#2}%
}%

\begin{document}

\begin{LTXexample}[style=demoLatexStyle]
\LaTeX{} \LaTeX{}
\end{LTXexample}

\begin{showdemoEnv}{\TempFileName}
\end{showdemoEnv}

\showdemo{\TempFileName}
\immediate\write18{rm \TempFileName}% Remove file
\end{document}


Note that the starred version filecontents* was used. This prevents the header comments that filecontents would normally add to the file.

-
Yes I know that command. However what I want is to reuse an environment (in that case LTExample of showexpl package) which itsself calls listings. In that case \lstnewenvironment can not be applied. –  Matthias Pospiech Sep 30 '11 at 6:35
So it sounds like what you need is something to such as \lstrenewenvironment similar to \renewenvironment`. –  Peter Grill Sep 30 '11 at 17:36
Have updated solution. –  Peter Grill Sep 30 '11 at 18:00
Since I can not delete the created files within LaTeX, I can not consider any solution which is based on the creation of files –  Matthias Pospiech Sep 30 '11 at 18:36
Ok see update where I remove the temp file at end. Have tested this on Mac OS and it works, so it you are using Windows you'll need to see if that command needs to be adapted. –  Peter Grill Oct 1 '11 at 1:24