You can use caption=\relax
; however, then you'll get "Listing #:" with the colon. I don't know how to remove this.
EDIT: OK, here's a nasty hack that removes the colon if the caption is \relax
. It works with my version of listings
, but it might not work in future versions.
\documentclass{article}
\usepackage{listings}
\begin{document}
\makeatletter
\def\test@relax{\relax}
\let\save@fnum@lstlisting\fnum@lstlisting
\def\fnum@lstlisting{%
\save@fnum@lstlisting
\ifx\lst@caption\test@relax\expandafter\@gobble\fi
}
\makeatother
\begin{lstlisting}[caption=\relax]
a:=b
\end{lstlisting}
\end{document}
You could use the help of mdframed
and expecially its options singleextra
, firstextra
, secondextra
and middleextra
. The different options allow you to have different styles for a frame that's on a single page and a frame that is broken over two or more pages.
When mdframed
is used with framemethod=tikz
you have access to the corners of a frame. The node on the lower left corner is called (O)
and the one on the upper right (P)
. The lower right one can thus be accessed by (O -| P)
and the upper left one by (P -| O)
.
This can be used in combination with \lstnewenvironment
to get a customized listings environment that does what one wants.
Below I define a mdframed
style that adds some continuing information if a frame is split and a new listings
environment that uses this frame style. Here is how it looks:
And here is the code:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[framemethod=tikz]{mdframed}
% define the frame style for the listing:
\mdfdefinestyle{note}
{
hidealllines = true ,
skipabove = .5\baselineskip ,
skipbelow = .5\baselineskip ,
singleextra = {} ,
firstextra = {
\node[below right,overlay,align=left,font=\continuingfont]
at (O) {\continuingtext};
} ,
secondextra = {
\node[above right,overlay,align=left,font=\continuingfont]
at (O |- P) {\continuedtext};
} ,
middleextra = {
\node[below right,overlay,align=left,font=\continuingfont]
at (O) {\continuingtext};
\node[above right,overlay,align=left,font=\continuingfont]
at (O |- P) {\continuedtext};
}
}
% customize the appearance of the continuing notes:
\newcommand*\continuingfont{\footnotesize\itshape}
\newcommand*\continuingtext{Listing continues on next page}
\newcommand*\continuedtext{Continuing from last page}
\usepackage{listings}
% define the listings style:
\lstdefinestyle{code}{
language = [LaTeX]TeX,
basicstyle = \small\ttfamily ,
numbers = left,
numberstyle = \tiny,
numberblanklines = true,
breaklines = true,
keepspaces = true,
columns = fullflexible,
% whatever else you want ...
}
% define the environment:
\lstnewenvironment{listing}
{%
\lstset{style=code}%
\mdframed[style=note]%
}
{%
\endmdframed
}
\usepackage{lipsum}% dummy text
\begin{document}
\lipsum[1-2]
\begin{listing}
\usepackage[framemethod=tikz]{mdframed}
\mdfdefinestyle{note}
{
hidealllines = true ,
skipabove = .5\baselineskip ,
skipbelow = .5\baselineskip ,
singleextra = {} ,
firstextra = {
\node[below right,overlay,align=left,font=\continuingfont]
at (O) {\continuingtext};
} ,
secondextra = {
\node[above right,overlay,align=left,font=\continuingfont]
at (O |- P) {\continuedtext};
} ,
middleextra = {
\node[below right,overlay,align=left,font=\continuingfont]
at (O) {\continuingtext};
\node[above right,overlay,align=left,font=\continuingfont]
at (O |- P) {\continuedtext};
}
}
\newcommand*\continuingfont{\footnotesize\itshape}
\newcommand*\continuingtext{Listing continues on next page}
\newcommand*\continuedtext{Continuing from last page}
\usepackage{listings}
\lstdefinestyle{code}{
language = [LaTeX]TeX,
basicstyle = \small\ttfamily ,
numbers = left,
numberstyle = \tiny,
numberblanklines = true,
breaklines = true,
keepspaces = true,
columns = fullflexible,
% whatever else you want ...
}
\lstnewenvironment{listing}
{%
\lstset{style=code}%
\mdframed[style=note]%
}
{%
\endmdframed
}
\end{listing}
\lipsum[2]
\end{document}
Best Answer
You could also use something like this in your document preamble:
This would resemble the same behaviour as:
But I think the first variant is much cleaner and more flexible. You could specify
\AtBeginDocument{\counterwithin{lstlisting}{subsection}}
just as well.Or
\AtBeginDocument{\counterwithin{lstlisting}{chapter}}
if you like.I hope this helps.