I'd like to have a list of my custom environments at the start of each section. I read this post, but wasn't able to get the titletoc to help me. The given response worked for floats, but I couldn't see how to adapt it to (non-floating) environments.
Here is a MWE of what I have so far; it works the way I want to, except for the spacing issues between the commas. It follows along the lines of the discussion in the above post, by creating a file for each section (hacked from the ntheorem package).
So, my questions are: how could I fix the spacing issues between the commas? Is it bad to create lots of auxiliary files (.prb1, .prb2, etc)? Have I re-invented a wheel? I'd also welcome any other feedback.
\documentclass{article}
\usepackage{ifthen}
\newcounter{probfilecounter}
\setcounter{probfilecounter}{0}
\newcounter{index}
\newcounter{linecount}
\newread\File
\let\stdsection\section
\renewcommand\section{\stepcounter{probfilecounter}\stdsection}
\makeatletter
\def\listproblems{%
\openin\File=\jobname.prb\thesection%
%\@input{\jobname .prb\thesection}%
\setcounter{linecount}{0}%
\loop\unless\ifeof\File%
\stepcounter{linecount}%
\read\File to\fileline %
\repeat%
\closein\File%
% re-open the file
\setcounter{index}{0}%
\openin\File=\jobname.prb\thesection%
\loop\unless\ifeof\File%
\stepcounter{index}%
\read\File to\fileline%
%\fileline
\ifthenelse{\theindex=1}%
{Problems in this section: \fileline}%
{%
\ifthenelse{\theindex<\thelinecount}%
{, \fileline}%
{}%
}%
\repeat%
\closein\File%
}%
\def\prb@enablelistofproblems{%
\begingroup%
\makeatletter%
\if@filesw%
\setcounter{index}{0}%
\whiledo{\value{index}<\theprobfilecounter}{%
\stepcounter{index}%
\expandafter\newwrite\csname tf@prb\theindex\endcsname%
\immediate\openout \csname tf@prb\theindex\endcsname \jobname.prb\theindex\relax%
}%
\fi%
\@nobreakfalse%
\endgroup}%
\AtEndDocument{\prb@enablelistofproblems}
\makeatother
% define the problem environment
\newcounter{problem}
\newenvironment{problem}{
\refstepcounter{problem}%
\textbf{Problem \theproblem} \par
\addtocontents{prb\theprobfilecounter}{\theproblem}
}{}
\begin{document}
\section{intro}
\listproblems
\begin{problem}
second problem
\end{problem}
\begin{problem}
another problem
\end{problem}
\section{another section}
\listproblems
\begin{problem}
another problem
\end{problem}
\begin{problem}
another problem
\end{problem}
\end{document}