Here is a solution via filecontents package. Features:
- the collected code by the
coll environment is pure (La)TeX (no |\par| like in Gonzalo Medina's answer).
- can use several named collections (created by
\newcoll).
- use an external file (
\jobname-<collname>-<n>.tex) to store each block of collected code.
Example:

The preamble (with commented macros):
\documentclass[twocolumn]{article}
\makeatletter
% some useful packages
\usepackage{filecontents}
\usepackage{listings}
% utilities
\gdef\coll@currcoll{}
\def\coll@stepcurrcollcount{\stepcounter{coll@count\coll@currcoll}}
\def\coll@currcollcount{\csname thecoll@count\coll@currcoll\endcsname}
\def\coll@currcollshow{\csname thecoll@show\coll@currcoll\endcsname}
\def\coll@currexternfilename{\jobname-\coll@currcoll-\coll@currcollcount}
\def\coll@currlegend{\csname coll@legend\coll@currcoll\coll@currcollcount\endcsname}
\def\coll@setlegend#1{%
\expandafter\gdef\csname coll@legend\coll@currcoll\coll@currcollcount\endcsname{#1}%
}
\def\coll@getlegend#1{\csname coll@legend\coll@currcoll#1\endcsname}
\def\coll@setref#1{%
\expandafter\gdef\csname coll@ref\coll@currcoll\coll@currcollcount\endcsname{#1}%
}
\def\coll@getref#1{\csname coll@ref\coll@currcoll#1\endcsname}
% macro to expand collected code into a figure
\newcommand{\coll@execcoll}{%
\begin{figure}[h!]
\centering
\@input{\coll@currexternfilename}%
\caption{\coll@getlegend{\coll@currcollcount}}
\label{\coll@getref{\coll@currcollcount}}
\end{figure}
}
% macro to create a new collection
\newcommand\newcoll[1]{%
\newcounter{coll@count#1}
\setcounter{coll@count#1}{0}
\newcounter{coll@show#1}
\setcounter{coll@show#1}{0}
}
% environment to add an element to a collection
\newenvironment{coll}[3]{% collection, legend, ref
\gdef\coll@currcoll{#1}%
\coll@stepcurrcollcount%
\coll@setlegend{#2}%
\coll@setref{#3}%
\@tempswafalse\filec@ntents%
{\coll@currexternfilename}%
}
{\endfilecontents\aftergroup\coll@execcoll}
% macro to show one code from current collection
\newcommand{\coll@showonecode}[1]{
\lstinputlisting[title={Code of figure \ref{\coll@getref{#1}}: \coll@getlegend{#1}}]%
{\jobname-\coll@currcoll-#1.tex}%
}
% macro to show all collected codes from a collection
\newcommand{\showcoll}[1]{
\gdef\coll@currcoll{#1}%
\loop\ifnum\coll@currcollcount>\coll@currcollshow %
\stepcounter{coll@show#1}
\coll@showonecode{\coll@currcollshow}
\repeat
}
\makeatother
The document:
\usepackage{xcolor}
\lstset{
basicstyle=\small\ttfamily,
frame=rltb,
backgroundcolor=\color{yellow!30},
breaklines=true,
escapeinside=||
}
\usepackage{tikz}
\begin{document}
\newcoll{code-type-1}
\newcoll{code-type-2}
\section{First section}
\subsection{a subsection}
\begin{coll}{code-type-1}{first legend}{code:one}
\begin{tikzpicture}
\draw[fill=orange] circle(3mm);
\end{tikzpicture}
\end{coll}
\subsection{another subsection}
\begin{coll}{code-type-2}{another legend}{code:two}
\textbf{bar}
\end{coll}
\section{Second section}
\subsection{third subsection}
\begin{coll}{code-type-1}{third legend}{code:three}
\begin{tikzpicture}
\draw[fill=blue] circle(3mm) ++(1cm,0) circle(4mm);
\end{tikzpicture}
\end{coll}
\subsection{fourth subsection}
\begin{coll}{code-type-2}{fourth legend}{code:four}
\begin{tikzpicture}
\fill[black] circle(5mm and 3mm);
\end{tikzpicture}
\end{coll}
\section{The collections}
\subsection{Collected codes type 1}
\showcoll{code-type-1}
\subsection{Collected codes type 2}
\showcoll{code-type-2}
\end{document}
|\par|or if your codes are unaffected by extra spaces and missing linefeeds... – Paul Gaborit Oct 14 '12 at 20:02