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

masu
  • 6,571
  • 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 Oct 04 '13 at 15:12
  • Thanks @egreg. Is it somehow my mistake, is it a bug, or is it an undocumented feature? – masu Oct 04 '13 at 17:49
  • 1
    A bad interaction with the commands you're using. I didn't find the culprit. – egreg Oct 04 '13 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} – Marco Daniel Oct 04 '13 at 19:52
  • @MarcoDaniel interestingly it works with mdframed's base environment but not with an environment newly defined with \newmdenv... – cgnieder Oct 06 '13 at 10:43
  • @cgnieder: Odd. :-) – Marco Daniel Oct 06 '13 at 11:12
  • 1
    @MarcoDaniel It works when I slightly change the definition of \newmdenv: \renewcommand\newmdenv[2][]{\newenvironment{#2}\mdfsetup{#1}\mdframed}{\endmdframed}} – cgnieder Oct 06 '13 at 15:19
  • I like this. However, this isn't even correct syntactically: you missed an opening curly bracket after \newenvironment{#2}. :) To enable copy/paste of your code: \renewcommand\newmdenv[2][]{\newenvironment{#2}{\mdfsetup{#1}\mdframed}{\endmdfr‌​amed}} – masu Oct 06 '13 at 16:04
  • masu: a copy/paste & edit error from my editor to the comment but I figured @Marco would understand :) – cgnieder Oct 06 '13 at 18:48
  • Marco would, but there are newbies around, like me. :) – masu Oct 06 '13 at 21:32

1 Answers1

2

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}
cgnieder
  • 66,645
  • 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 Oct 06 '13 at 13:03
  • 1
    @masu I have the doubt that listings environments do something to \end... I haven't really checked, though – cgnieder Oct 06 '13 at 20:45