4

I have a problem with the listings and the mdframed package together. The following code compiles with errors, but the only info I managed to get about it is that it was fatal.

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{mdframed}
\usepackage{listings}
\newmdenv[linecolor=red,frametitle=Infobox]{infoboxmd}
\makeatletter
\def\@noargument{noargument}
\newenvironment{infobox}[1][noargument]
  {\def\@opt@arg{#1}% Caption optional argument
   \infoboxmd}
  {\endinfoboxmd\par\nobreak%
   \ifx\@opt@arg\@noargument\else\centering\@opt@arg\par\fi}%
\makeatother

\lstnewenvironment{codetext}[1]{
Something.
#1
\infobox[#1]
Some text.
}{
Some text.
\endinfobox
Something.
}
%\BeforeBeginEnvironment{codetext}{\begin{infobox}[myonlycaption]}
%\AfterEndEnvironment{codetext}{\end{infobox}}

\begin{document}

\begin{codetext}{caption}
Some code.
\end{codetext}

Some text.

\end{document}

If I use the (commented out) \BeforeBeginEnvironment command instead of \infobox it works, but I can't specify the parameter that way.

Associated thread: mdframed captioning thread

10
  • What happens is that LaTeX doesn't exit from the “verbatim” mode initiated with \begin{codetext}, so with a non-interactive compilation TeX exits. With an interactive run (from the command line) the input prompt appears, but of course no command can be recognized and forcing exit is the only escape.
    – egreg
    Commented Oct 4, 2013 at 15:12
  • Thanks @egreg. Is it somehow my mistake, is it a bug, or is it an undocumented feature?
    – masu
    Commented Oct 4, 2013 at 17:49
  • 1
    A bad interaction with the commands you're using. I didn't find the culprit.
    – egreg
    Commented Oct 4, 2013 at 17:50
  • 2
    @egreg: The culprit isn't related to mdframed (I guess). I tried the following modified MWE: \documentclass{article} \usepackage[a6paper]{geometry} \usepackage{listings} \makeatletter \lstnewenvironment{codetext}[1]{ \begin{lrbox}{\@tempboxa} }{% \end{lrbox}% \fbox{\usebox\@tempboxa}% } \begin{document} \begin{codetext}{caption} Some code. \end{codetext} \end{document} Commented Oct 4, 2013 at 19:52
  • 1
    @MarcoDaniel It works when I slightly change the definition of \newmdenv: \renewcommand\newmdenv[2][]{\newenvironment{#2}\mdfsetup{#1}\mdframed}{\endmdframed}}
    – cgnieder
    Commented Oct 6, 2013 at 15:19

1 Answer 1

2
+50

I have no idea what's going wrong but I have a working alternative: instead of defining a new mdframed environment define a new mdframed style and use it with mdframed's base environment:

enter image description here

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{mdframed}
\usepackage{listings}

\mdfdefinestyle{infoboxmd}{linecolor=red,frametitle=Infobox}

\makeatletter
\def\@noargument{noargument}
\newenvironment{infobox}[1][noargument]
  {\def\@opt@arg{#1}% Caption optional argument
   \mdframed[style=infoboxmd]}
  {\endmdframed\par\nobreak%
   \ifx\@opt@arg\@noargument\else\centering\@opt@arg\par\fi}%
\makeatother

\lstnewenvironment{codetext}[1]{%
  Something.
  #1
  \infobox[#1]
  Some text.
}{%
  Some text.
  \endinfobox
  Something.
}

\begin{document}

\begin{codetext}{caption}
Some code.
\end{codetext}

Some text.

\end{document}
2
  • Thank you, I've just accepted your answer. I'm still eager to know what was the problem with the original, but this solution is working.
    – masu
    Commented Oct 6, 2013 at 13:03
  • 1
    @masu I have the doubt that listings environments do something to \end... I haven't really checked, though
    – cgnieder
    Commented Oct 6, 2013 at 20:45

You must log in to answer this question.

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