How do I create custom “list of…”s on a section level?

I have a long document with frequent uses on an environment that needs to be indexed on a per-section basis. I know that the tocloft package allows creation of custom "list of..."s I also know that the minitoc package allows per-part, per-chapter, or per-section tables of contents.

But I need a custom "list of" that occurs on a per-section basis, that is, both functionalities. I think I have an idea how to implement this: Each instance of the environment writes something to an aux file that is unique to the section, and at the beginning of that section the custom aux file is read, writing that list. But before I start hacking I'm wondering if this wheel has been invented before and I don't know of it.

-

See "7.4. Partial lists" in titletoc package, it may be what you want.

And I guess minitoc may also solve this problem itself. I'm not sure.

For user-defined floats, I could only hack into titletoc. Luckily, it's not very difficult. And I think this feature should be added into titletoc.

\documentclass{article}
\usepackage{float,titletoc}
\newfloat{myflt}{htbp}{mine}
\makeatletter
\def\ttl@partialmine{pmine}
\def\ttl@writepartial#1#2{%
\ttl@topartial{toc}{#1}{#2}%
\ttl@topartial{lof}{#1}{#2}%
\ttl@topartial{lot}{#1}{#2}%
\ttl@topartial{mine}{#1}{#2}%
\ttl@writefile{#1}{#2}}
\makeatother

\newcommand{\listofmyflt}{\listof{myflt}{Full list}}

\newcommand{\partiallistofmyflt}{
\startlist{mine}%
\printlist{mine}{}{\subsection*{Partial list}}}

\begin{document}
\listofmyflt

\section{foo}
\partiallistofmyflt

\begin{myflt}\caption{foo}\end{myflt}

\section{bar}
\partiallistofmyflt

\begin{myflt}\caption{bar}\end{myflt}
\begin{myflt}\caption{baz}\end{myflt}

\stoplist{mine}
\end{document}

-
That almost seems like it could work. However, I think I might prefer my own-rolled solution. –  Matthew Leingang Mar 3 '11 at 5:07
@Matthew: Indeed. I updated the solution, no much hacks. –  Leo Liu Mar 3 '11 at 5:29
And I checked the code of minitoc, it's much harder to hack. –  Leo Liu Mar 3 '11 at 5:37
Leo, I should have trusted you back in March. After much work I tried your suggestion and it works like a charm. I did change your code sample to illustrate the method better, though. –  Matthew Leingang Sep 14 '11 at 1:39
Does it work for non-floating environments? –  cmhughes Sep 14 '11 at 4:40

I've put a version below of how I achieved this for non-floating environments. It writes to the file \jobname.prb; some may consider this a disadvantage as it uses one of TeX's write registers. It needs 2 compiles (as with any other toc-based command). I'd welcome any feedback.

\documentclass{article}
\usepackage{ifthen}
\usepackage{hyperref}
\usepackage{xstring}

\newcounter{probSectCounter}
\setcounter{probSectCounter}{-1}
\newcounter{echo}

% renew the section command so that it writes
% to the \jobname.prb file
\let\oldsection\section
\renewcommand\section{%
\stepcounter{probSectCounter}%
\oldsection}

% custom environment- whatever you want
\newcounter{problem}
\newenvironment{problem}{%
\refstepcounter{problem}%
\textbf{Problem \theproblem }\par%
}%
{%
\par\vspace{\belowdisplayskip}\noindent\ignorespacesafterend%
}

% define the \listproblems command
\def\listproblems{%
% open the appropriate file do everything in a
% new environment so that we can renew the
% \contentsline command locally
\begin{problemlist}
\openin\File=\jobname.prb%
\loop\unless\ifeof\File%
\fileline%
\repeat%
\closein\File%
\end{problemlist}
}%

\makeatletter
% need a command to bring the \contentsline
% into the problemlist environment
\let\orig@contents\contentsline%
\newenvironment{problemlist}%
{%
% renew the \contentsline command locally in this environment
%
% if we renewed the command outside of the environment, it would
% affect \tableofcontents, \minitoc, and perhaps more- bad!
\let\contentsline\orig@contents%
\let\Contentsline\contentsline%
\renewcommand\contentsline[4]{%
{%
{%
\setcounter{echo}{1}%
}%
{%
\setcounter{echo}{0}%
}%
% the command is empty
}%
{%
\ifthenelse{\equal{\theecho}{1}}%
{%
\Contentsline{##1}{##2}{##3}{##4}%
}%
{}%
}%
}%
}%
{}

% enable the \jobname.prb file
\def\prb@enablelistofproblems{%
\begingroup%
\makeatletter%
\if@filesw%
\expandafter\newwrite\csname tf@prb\endcsname%
\immediate\openout \csname tf@prb\endcsname \jobname.prb\relax%
\fi%
\@nobreakfalse%
\endgroup}%

% enable the \jobname.prb files at the end
% of the document
\AtEndDocument{\prb@enablelistofproblems}

\makeatother

\begin{document}

\section{intro}
Welcome to this section.
\listproblems

\begin{problem}
In environment
\end{problem}

\begin{problem}
In environment
\end{problem}

\section{another section}
\listproblems

\begin{problem}
In environment
\end{problem}

\begin{problem}
In environment
\end{problem}

\begin{problem}
In environment
\end{problem}

\end{document}

-