The following example constructs floating listings using the tcolorbox
package without numbering the listings . The environment preamblelisting
takes the preamble as mandatory parameter. The optional parameter is any tcolorbox
option. The underlying package for typesetting is listings
.
Note: You may have to update the tcolorbox
package to compile.
\documentclass{report}
\usepackage[skins,listings]{tcolorbox}
\usepackage{lipsum}
\newtcblisting{preamblelisting}[2][]{%
float=htb,empty,
boxsep=0pt,left=0pt,right=0pt,boxrule=2pt,leftrule=0pt,rightrule=0pt,
borderline horizontal={2pt}{0pt}{black},
segmentation engine=path,
segmentation style={solid,line width=1pt},
comment and listing,
fontupper=\itshape,
listing options={% put any listing options here
language={[LaTeX]TeX},
aboveskip=0pt, belowskip=0pt,numbers=left,numberstyle=\tiny,
basicstyle=\ttfamily,columns=fullflexible},
comment={#2},#1
}
\begin{document}
\lipsum[1]
\begin{preamblelisting}{This is the preamble. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.}
\begin{test}{Example}
This is an example
\end{test}
\end{preamblelisting}
\begin{preamblelisting}[float=b]{This is the preamble. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.}
\begin{test}{Example}
This is an example
This is an example
This is an example
\end{test}
\end{preamblelisting}
\lipsum
\end{document}
UPDATE:
This update considers the comments of the OP and adds numbering with titles set below. Also, this blends into normal listings from the listings package, i.e. both can be used in parallel. Finally, cleveref
is used:
\documentclass{report}
\usepackage{cleveref,varwidth}
\usepackage[skins,listings]{tcolorbox}
\usepackage{lipsum}
\Crefname{lstlisting}{Listing}{Listings}
\AtBeginDocument{%
\newtcblisting[blend into=listings]{preamblelisting}[3][]{%
float=htb,empty,
boxsep=0pt,left=0pt,right=0pt,boxrule=2pt,leftrule=0pt,rightrule=0pt,
borderline horizontal={2pt}{0pt}{black},
segmentation engine=path,
segmentation style={solid,line width=1pt},
comment and listing,
fontupper=\itshape,
listing options={% put any listing options here
language={[LaTeX]TeX},
aboveskip=0pt, belowskip=0pt,numbers=left,numberstyle=\tiny,
basicstyle=\ttfamily,columns=fullflexible},
attach boxed title to bottom center,
varwidth boxed title,
coltitle=black,
boxed title style={blanker,top=2mm},
title={#2},comment={#3},#1
}}
\begin{document}
\lstlistoflistings
\chapter{Example}
\begin{lstlisting}[language={[LaTeX]TeX},
numbers=left,numberstyle=\tiny,
basicstyle=\ttfamily,columns=fullflexible,
caption={My normal listing},
captionpos=b,
label=mynormal]
This is a normal listing
\end{lstlisting}
\lipsum[2]
\textcolor{red}{\Cref{mynormal} is a normal listing and \Cref{mypreamble} is a listing
with a preamble.}
\begin{preamblelisting}[label=mypreamble]{My preamble listing}%
{This is the preamble. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.}
\begin{test}{Example}
This is an example
\end{test}
\end{preamblelisting}
\begin{preamblelisting}[float=b,label=floatingpreamble]{My floating preamble listing}%
{This is the preamble. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.}
\begin{test}{Example}
This is an example
This is an example
This is an example
\end{test}
\end{preamblelisting}
\lipsum
\end{document}