[Tex/LaTex] Captioning an mdframed box without making unbreakable

captionslistingsmdframedpage-breaking

I would like to a have a caption below my mdframed box (outside the border if possible) which sometimes span across multiple pages. I managed to create a caption several ways but the pagebreaks are unfriendly. How can I do that without making the box unbreakable at pagebreaks?

The listings package is not an alternative, because I already use it inside somewhere in the mdframed box, and nesting them doesn't seem like a good idea.

Associated thread: use this inside \lstnewenvironment

Best Answer

The following might be what the after:

enter image description here

\documentclass{article}
\usepackage[a6paper]{geometry}% Just for this example
\usepackage{lipsum,mdframed}% http://ctan.org/pkg/{lipsum,mdframed}
\newmdenv[linecolor=red,frametitle=Infobox]{infoboxmd}
\makeatletter
\def\@noargument{noargument}
\newenvironment{infobox}[1][noargument]
  {\gdef\@opt@arg{#1}% Caption optional argument
   \infoboxmd}
  {\endinfoboxmd\par\nobreak%
   \ifx\@opt@arg\@noargument\else\centering\@opt@arg\par\fi}%
\begin{document}

\begin{infobox}[This is a caption]
\lipsum[1-2]
\end{infobox}

Some text.

\end{document}
Related Question