5

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?

4
  • See if the \lstnewenvironment command works any better, as shown in this similar question. Sep 29, 2011 at 20:36
  • I think it has to do with there being code within a verbatim environment. Sep 29, 2011 at 20:37
  • @Matthias: Does the link Mike linked to solve your problem. If not, please let us know. Sep 29, 2011 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. Sep 30, 2011 at 18:44

1 Answer 1

8

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:

enter image description here


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.

enter image description here

5
  • 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. Sep 30, 2011 at 6:35
  • So it sounds like what you need is something to such as \lstrenewenvironment similar to \renewenvironment. Sep 30, 2011 at 17:36
  • Have updated solution. Sep 30, 2011 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 Sep 30, 2011 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. Oct 1, 2011 at 1:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .