[Tex/LaTex] Latex escape interrupts frame of listing

boxesframedlistingstable of contents

I am (ab)using a framed listings environment include an enumeration environment (by escaping to latex). This compiles fine but in the resulting pdf the frame around the listing is interrupted.

Minimal working example:

\documentclass{report}

\usepackage[english]{babel}
\usepackage{float}
\usepackage[margin=10pt,font=small,labelfont=bf,justification=centering]{caption}

\usepackage{listings}
\renewcommand{\lstlistingname}{Box}
% Setup for listings:
\lstset{extendedchars=true,
    tabsize=3,
    frame=single,
    frameround=tttt,
    showspaces=false,
    framesep=10pt,
    boxpos=c,
    float=h,
    captionpos=b,
    escapechar=\%
}
\lstdefinestyle{Normaltext}{language=,numbers=none,basicstyle=\normalfont}

\lstnewenvironment{textbox}[2]
{%
\centering
\minipage{0.9\textwidth}
\lstset{style=Normaltext,label=#1,caption=#2}
}
{%
\endminipage
\endcenter
}

\begin{document}

\begin{textbox}{box:somelabel}{some caption}
%{Test 1 2 3, here comes the enumeration:
\begin{itemize}
\item item 1
\item item 2
\item item 3
\item item 4
\end{itemize}
}%
\end{textbox}

\end{document}

This produces something like this:
Interrupted listing frame

Any ideas how I can avoid that the frame is interrupted?

Or maybe somebody can suggest a better alternative to abusing listings for putting plain latex text in a frame box, because that's all I really wanted.
Basically what I want to achieve is this:

  • I want 3 types of "objects" in my document: figures, tables and boxes.
  • Figures have the "Figure" label and are listed in the listoffigures.
  • Tables have the "Table" label and are listed in the listoftables.
  • Similarly I want boxes to have the "Box" label and be listed in a listofboxes (I was planning to use listoflistings for this).
  • Boxes can either contain source code (using listings package) or plain latex text, but either way they should have the same rounded frame around them and be centered on the page and cover 90% of textwidth (I was using minipage for this, as you can see in my code).

Best Answer

The framed or mdframed packages allow you to put text in frames. Here's a way to do what you want with mdframed.

\documentclass{article}
\usepackage{caption}
\usepackage{listings}
\lstset{extendedchars=true,
    tabsize=3,
    frame=none,
    showspaces=false,
    boxpos=c,
    float=h,
    escapechar=\%
}
\usepackage[style=1]{mdframed}
\newcounter{boxctr}
\DeclareCaptionType{boxx}
\usepackage{needspace}
\newenvironment{Boxx}[2]{%
  \captionsetup{type=boxx}
  \begin{mdframed}[%
    linewidth=1pt,
    roundcorner=10pt,%
    leftmargin=.05\textwidth,
    rightmargin=.05\textwidth,
    skipabove=.7\baselineskip]%
    \refstepcounter{boxctr}%
    \label{#1}%
    \gdef\boxxcaption{\caption{#2}}
    \needspace{2\baselineskip}
  }{%
  \end{mdframed}%
  \boxxcaption%
}
\usepackage{lipsum}
\begin{document}
This is a reference to Box~\ref{box:one}!
\vspace{1.5cm}
\lipsum[1-4]
\begin{Boxx}{box:one}{This is a caption}
  \begin{enumerate}
  \item One
  \item Two
  \item Three
  \end{enumerate}
\end{Boxx}
\lipsum[1]
\begin{Boxx}{box:two}{This is also a caption}
  \begin{lstlisting}
    Also works with listings

    \foo is escaped!
  \end{lstlisting}
\end{Boxx}
\lipsum[2]
\end{document}

Boxes take two extra arguments: a label and a caption, just like the environment defined in the question. They use the caption package's captioning magic. So this should make doing lists of boxes easier. The label should stick with the bottom of the box now. This might lead to some ugly whitespace, but other than making the boxes float, this is probably as good as it gets.

Related Question