# Combining subfloat and newfloat packages for sublistings environment

I am using the subfloat package to group related figures; they show up like 1a, 1b, etc. in the List of Figures.

Now, I want to define a new listing float using the newfloat package, which generates its own List of Listings.

Alas, I am lacking something like a sublistings environment, which causes the listings to be labelled 1a, 1b, etc. as well:

\documentclass{article}
\usepackage[countmax]{subfloat}
\usepackage{newfloat}
\DeclareFloatingEnvironment{listing}

\begin{document}
\listoflistings

\begin{sublistings}
\begin{listing}
\caption{First listing}
\end{listing}
\begin{listing}
\caption{Second listing}
\end{listing}
\end{sublistings}

\end{document}


As far as I can see, the subfloat package has support for subfigures and subtables only.

I wasn't able to copy & modify its code to produce a sublistings environment that works with the float defined by newfloat. Can anyone help me out?

-

It's cargo cult programming, but it works: Take the possibly relevant code of subfloat.sty, enclose it into \makeatletter/\makeatother and replace every instance of figure with listing.

\documentclass{article}
\usepackage[countmax]{subfloat}
\usepackage{newfloat}
\DeclareFloatingEnvironment{listing}

\makeatletter
\newcommand*\thesubfloatlisting{\themainlisting\alph{subfloatlisting}}
\newenvironment{sublistings}{%
\sublistingsbegin
}{%
\sublistingsend
\global\@ignoretrue
}
\let\c@subfloatlisting=\c@listing
\newif\ifinsubfloatlistings
\newcommand{\sublistingsbegin}{%
\ifinsubfloatlistings
\PackageError{subfloat}{Cannot start subfloatlistings inside
a\MessageBreak subfloatlisting area}{You probably have used
\string\sublistingsbegin\space or \string\begin{sublistings} inside
the^^Jsame environment or after \string\sublistingsbegin.}%
\fi
\global\insubfloatlistingstrue
\refstepcounter{listing}%
\protected@xdef\listing@value{\the\c@listing}%
\protected@xdef\themainlisting{\thelisting}%
\global\let\thelisting@original=\thelisting
\setcounter{listing}{0}%
\gdef\thelisting{\thesubfloatlisting}%
\ifsubfloat@countmax
{\def\@setref##1##2##3{%
\ifx##1\relax
\protect\G@refundefinedtrue
\setcounter{subfloatlistingmax}{0}%
\PackageWarningNoLine{subfloat}{Reference ##3' on page
\thepage \space undefined\MessageBreak
(count subfloatlistings)}%
\else
\setcounter{subfloatlistingmax}{\expandafter\@firstoftwo##1}%
\fi}%
\ref{subfloat@@listing\listing@value}}%
\fi
\let\p@sublisting=\thesubfloatlisting
%%  }{}%
\ignorespaces
}
\newcommand{\sublistingsend}{%
\ifinsubfloatlistings
\else
\PackageError{subfloat}{Cannot stop subfloatlistings outside
a\MessageBreak subfloatlisting area}{You probably have used
\string\sublistingsend\space or \string\end{sublistings} without
starting a^^Jsubfloatlisting area using the same environment or
\string\sublistingsbegin.}%
\fi
\global\insubfloatlistingsfalse
\ifsubfloat@countmax
\subfloat@listinglabel{subfloat@@listing\listing@value}%
\fi
\setcounter{listing}{\listing@value}%
\global\let\thelisting=\thelisting@original
\let\p@sublisting=\thelisting
%%  }{}%
\ignorespaces
}
%%
\ifsubfloat@countmax
\newcounter{subfloatlistingmax}
\else
\PackageWarningNoLine{subfloat}{Numbers of floats not
counted:\MessageBreak
If you use one of the counters subfloatlistingmax or\MessageBreak
subfloattablemax you will get strange error messages\MessageBreak
containing \string\c@subfloatlistingmax\space or\MessageBreak
\string\c@subfloattablemax:\MessageBreak
Please switch on countmax or
remove the code using\MessageBreak
the counter then.}
\fi
\ifsubfloat@countmax
\newcommand*\subfloat@listinglabel[1]{\@bsphack
\protected@write\@auxout{}%
{\string\newlabel{#1}{{\the\c@listing}{\thepage}}}%
\@esphack}
\fi
\makeatother

\begin{document}
\listoflistings

\begin{sublistings}
\begin{listing}
\caption{First listing}
\end{listing}
\begin{listing}
\caption{Second listing}
\end{listing}
\end{sublistings}

\end{document}
`

-
Awesome, thanks. Must have done something wrong with my own search-and-replace effort earlier, which catapulted me into the land of incomprehensible error messages. :-( –  Andreas Jul 19 '12 at 15:34