[Tex/LaTex] TeX capacity exceeded

errorsprogramming

The following is a minimal example which demonstrates my problem. The error

TeX capacity exceeded, sorry [input stack size=5000] \end{latexresult}

is depending more or less on the number of codelines, not the actual code (therefore it is so long). One can comment out nearly any line and the error vanishes. I have no clue what could cause the problem.
I have mdframed in mind, but can not see what should cause the error.

\documentclass[]{scrbook}

\usepackage{url}
\RequirePackage{etoolbox}
\RequirePackage{filecontents}
\RequirePackage{mdframed}

\RequirePackage{listings}

\lstnewenvironment{latexcode}{}{}
\newenvironment{latexresult}{%
Result:
\mdframed[skipabove=6pt]%
}{
\endmdframed
\noindent
}

\listfiles

\begin{document}

\chapter{template demonstration}
\section{text markup}
\subsection{standard commands}
%
\begin{filecontents*}{democode}
The standard commands for font attributes:  
\textbf{bold}, \textit{italic}, \textsl{slanted},
\textsf{sans serif}, \textsc{small caps} and
\texttt{monospaced typewrite}.
And any combination of them:
\textit{\textbf{bold italic}},
\textsl{\textbf{bold slanted}},
\textsf{\textbf{bold sans serif}},
\textsc{\textbf{bold small caps}}
\textsl{\textsf{sans serif slanted}}. 
\end{filecontents*}

Code: 
\lstinputlisting{democode.tex}%
\begin{latexresult}%
\IfFileExists{democode.tex}{\input{democode.tex}}{}%
\end{latexresult}

\begin{filecontents*}{democode}
\url{http://www.dante.de} \\
\end{filecontents*}

\ifcsdef{url}{%
\subsection{package: url}

The url package provides a failsafe way to print
urls with characters not allowed by LaTeX.

\begin{minipage}[t]{0.49\textwidth}
\begin{latexresult}%
\IfFileExists{democode.tex}{\input{democode.tex}}{}%
\end{latexresult}
\end{minipage}

The font used for this command can be set up in the preamble.
}{}%

\section{paragraph alignment}
\subsection{standard alignment}

\begin{minipage}[t]{0.49\textwidth}
\begin{latexresult}%
\IfFileExists{democode.tex}{\input{democode.tex}}{}%
\end{latexresult}
\end{minipage}

\end{document}

EDIT:
I uncommented one line to let it compile again and got this filelist then:

*File List*
scrbook.cls 2011/06/16 v3.09a KOMA-Script document class (book)
scrkbase.sty 2011/06/16 v3.09a KOMA-Script package (KOMA-Script-dependent ba
sics and keyval usage)
scrbase.sty 2011/06/16 v3.09a KOMA-Script package (KOMA-Script-independent
basics and keyval usage)
keyval.sty 1999/03/16 v1.13 key=value parser (DPC)
scrlfile.sty 2011/03/09 v3.09 KOMA-Script package (loading files)
tocbasic.sty 2011/05/30 v3.09a KOMA-Script package (handling toc-files)
scrsize11pt.clo 2011/06/16 v3.09a KOMA-Script font size class option (11pt)
typearea.sty 2011/06/16 v3.09a KOMA-Script package (type area)
url.sty 2006/04/12 ver 3.3 Verb mode for urls, etc.
etoolbox.sty 2011/01/03 v2.1 e-TeX tools for LaTeX
etex.sty 1998/03/26 v2.0 eTeX basic definition package (PEB)
filecontents.sty 2009/03/17 v1.2 Create an external file from within a LaTeX
document
mdframed.sty 2011/08/22 v0.8: mdframed
kvoptions.sty 2010/12/23 v3.10 Keyval support for LaTeX options (HO)
ltxcmds.sty 2011/04/18 v1.20 LaTeX kernel commands for general use (HO)
kvsetkeys.sty 2011/04/07 v1.13 Key value parser (HO)
infwarerr.sty 2010/04/08 v1.3 Providing info/warning/message (HO)
etexcmds.sty 2011/02/16 v1.5 Prefix for e-TeX command names (HO)
ifluatex.sty 2010/03/01 v1.3 Provides the ifluatex switch (HO)
calc.sty 2007/08/22 v4.3 Infix arithmetic (KKT,FJ)
color.sty 2005/11/14 v1.0j Standard LaTeX Color (DPC)
color.cfg 2007/01/18 v1.5 color configuration of teTeX/TeXLive
pdftex.def 2011/05/27 v0.06d Graphics/color for pdfTeX
md-frame-0.mdf 2011/08/22 v0.8: md-frame-0
listings.sty 2007/02/22 1.4 (Carsten Heinz)
lstmisc.sty 2007/02/22 1.4 (Carsten Heinz)
listings.cfg 2007/02/22 1.4 listings configuration
supp-pdf.mkii
democode.tex
omscmr.fd 1999/05/25 v2.5h Standard LaTeX font definitions
democode.tex
democode.tex
***********

afterwards I updated texlive and then mdframed changed to version 0.9e and many packages are loaded which were not loaded before:

mdframed.sty 2011/09/11 v0.9e: mdframed
...
zref-abspage.sty 2011/03/18 v2.21 Module abspage for zref (HO)
zref-base.sty 2011/03/18 v2.21 Module base for zref (HO)
kvdefinekeys.sty 2011/04/07 v1.3 Defining keys (HO)
pdftexcmds.sty 2011/04/22 v0.16 Utilities of pdfTeX for LuaTeX (HO)
ifpdf.sty 2011/01/30 v2.3 Provides the ifpdf switch (HO)
auxhook.sty 2011/03/04 v1.3 Hooks for auxiliary files (HO)
atbegshi.sty 2011/01/30 v1.15 At begin shipout hook (HO)
md-frame-0.mdf 2011/09/10 v0.9e: md-frame-0

now it compiles without error.

Best Answer

As mentioned in the comments, the code as provided compiles fine for me using TeXLive2011, so perhaps you are using an older version of some package. What follows is not a direct solution to the problem reported here, but rather an alternate way of displaying code:

If you want to show LaTeX code and it's output you should consider using the showexpl package along with thelistings package as that produces very nice output:

enter image description here

\documentclass[]{scrbook}

\usepackage{url}
\usepackage{xcolor}

\usepackage{listings}
\usepackage{showexpl}
    \lstset{
        backgroundcolor=\color{yellow},
        basicstyle=\small\ttfamily,% print whole listing small
        keywordstyle=\color{blue}\bfseries\underbar,
        numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
        showstringspaces=false
    }
    \lstloadlanguages{[LaTeX]TeX}


\begin{document}
\begin{LTXexample}[pos=b]
The standard commands for font attributes:  
\textbf{bold}, \textit{italic}, \textsl{slanted},
\textsf{sans serif}, \textsc{small caps} and
\texttt{monospaced typewrite}.
And any combination of them:
\textit{\textbf{bold italic}},
\textsl{\textbf{bold slanted}},
\textsf{\textbf{bold sans serif}},
\textsc{\textbf{bold small caps}}
\textsl{\textsf{sans serif slanted}}. 
\end{LTXexample}

The url package provides a failsafe way to print
urls with characters not allowed by LaTeX.

\begin{LTXexample}
\url{http://www.dante.de}
\end{LTXexample}
\end{document}
Related Question