Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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).
share|improve this question

2 Answers 2

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.

share|improve this answer
    
Seamus: most importantly I want them to look the same as source code listings (rounded frame, centered, 90% width minipage, same caption label) I edited the original question to explain what I want. –  Matthias Aug 17 '11 at 13:12
    
OK. Everything apart from the "list of boxes" is easy (and should work in the above code). A list of boxes from scratch is trickier. Maybe there is a better way to escape from listings to allow you to use \listoflistings... –  Seamus Aug 17 '11 at 13:31
1  
Regarding the list: Since the caption package is loaded anyway one can use \DeclareCaptionType{boxx} and (re)define the boxx environment afterwards. Simply place \captionsetup{type=boxx} at first line of environment code (this will place the hyperref anchor, if necessary, and will initialize the use of \caption) and do the captions with \caption. –  Axel Sommerfeldt Aug 17 '11 at 14:26
1  
Looks ok to me. I just have modified Seamus example code so a minipage is used. Furthermore I removed the own counter, and use \caption@freeze and \caption@defrost instead of \boxxcaption so ordinary \caption can be used (with optional argument), and \ContinuedFloat and \captionsetup will work inside boxx, too. I uploaded it to latex.sommerfeldt.f-m.fm/test.tex Maybe some of the stuff used there is of any use for you? –  Axel Sommerfeldt Aug 17 '11 at 18:38
1  
@Matthias: Oh yes, you are right. \caption@freeze needs to redefine \label so it can utilize \caption and \label on \caption@defrost. You could include \let\Label\label just before \caption@freeze and use \Label for labeling your equations. (Of course this is only a workaround, will try to fix that for the caption package v3.2b right now...) –  Axel Sommerfeldt Aug 18 '11 at 5:44
up vote 0 down vote accepted

Because Seamus' answer was improved, thanks to additional input from Seamus and Axel Sommerfeldt (see discussion in comments), I think it is worthwhile to post a separate consolidated answer.

\documentclass{report}

\usepackage{color}
\usepackage{xcolor}

\usepackage[style=1]{mdframed}
\usepackage[font=small,labelfont=bf,justification=centering,belowskip=\baselineskip]{caption}
\usepackage[pdftex,bookmarks=true]{hyperref}%pagebackref=true
\usepackage{enumitem}
\usepackage{cleveref} %[nameinlink,noabbrev]

%%Paragraph indentation & spacing
\usepackage{parskip} %to disable paragraph indentation but still keep paragraphs separated (with \parskip)
\setlength{\parskip}{2.75ex plus 0.5ex minus 0.2ex}

\usepackage{listings}
\lstset{extendedchars=true,
    tabsize=3,
    frame=none,
    breaklines=true,
    breakautoindent=true,
    postbreak=\space,
    showspaces=false,
    keywordstyle=\color{blue},
    commentstyle=\color{red},
    stringstyle=\color{gray},
    identifierstyle=\color{black},
    framesep=0pt,
    boxpos=c,
    float=h,
    aboveskip=0pt,
    belowskip=0pt,
    escapechar=\%
}
\lstdefinestyle{Java}{language=Java, numbers=none, numberstyle=\tiny ,numbersep=5pt, basicstyle=\linespread{0.85}\scriptsize\ttfamily, showstringspaces=true}

\lstnewenvironment{java}{\lstset{style=Java}}{}

\usepackage{lipsum}
\usepackage{needspace} %\needspace{2\baselineskip}% %instead of \nopagebreak

\DeclareCaptionType[fileext=lob,placement=b,within=chapter]{boxx}[Box][List of Boxes]

\makeatletter
\caption@setbool{needfreeze}{true} %Tell the caption package that we would like to use \caption@freeze and \caption@defrost
\renewenvironment{boxx}
{%
  \pagebreak[2] %suggest pagebreak here
  \captionsetup{type=boxx,position=b} %skip BELOW the caption
  \nopagebreak
  \begin{mdframed}[%
    linewidth=1pt,
    linecolor=black,
    innerlinecolor=black,
    middlelinecolor=black,
    outerlinecolor=black,
    roundcorner=10pt,
    leftmargin=.05\textwidth,
    rightmargin=.05\textwidth,
    skipabove=\baselineskip,  %skip above the frame
    skipbelow=0.25\baselineskip, %skip between the frame and the caption
    innerleftmargin=10pt,    
    innerrightmargin=10pt,
    innertopmargin=10pt,
    innerbottommargin=10pt]%
    \nopagebreak
   \caption@freeze %"freeze" caption related commands
  }{%
  \end{mdframed}%
  \nopagebreak
  \caption@defrost %typeset caption, if necessary
}
\makeatother

\crefname{boxx}{box}{boxes}
\crefname{req}{requirement}{requirements}

\begin{document}

\chapter{Chapter}
\noindent This is a reference to \cref{box:one}.

\begin{boxx}
\lipsum[2]
\begin{enumerate}[label=\textbf{Req. \arabic*},ref=Req. \arabic*,leftmargin=*]
\item one\label{req:one}
\item two generalised.
\end{enumerate}
\caption[shorter]{This is a box with text}\label{box:one}
\end{boxx}

\noindent \lipsum[1-2]
Some more text, ref to item: \cref{req:one}

\begin{boxx}
\begin{java}
class HelloWorldApp
{
    public float pi = 3.14159;
    public int i = 0;

    public static void main(String[] args)
    {
        System.out.println("Hello World!"); //Display the string
    }
}
\end{java}
\caption{This is a box with Java code}\label{box:two}
\end{boxx}


\noindent Double uppercase reference: \Cref{box:one,box:two}.


\noindent \lipsum[3]

\pagebreak
\listofboxxs


\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.