# Custom list of environments (per section)

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}

\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}%
\repeat%
\closein\File%
% re-open the file
\setcounter{index}{0}%
\openin\File=\jobname.prb\thesection%
\loop\unless\ifeof\File%
\stepcounter{index}%
%\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
}{}

\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}

-

It seems that the line endings in your .prb<number> files are translated into spaces by TeX. If I change the code line

    {, \fileline}%


to

    {\unskip, \fileline}%


the spaces before the commas disappear.

-
Thanks @lockstep! –  cmhughes Jul 16 '11 at 19:56
Thanks again @lockstep. I think I've come to an answer about my question of the .prb1, .prb2, ... files- if I have too many, there's, 'No room for a new \write'. Guess it's time to get more resourceful! –  cmhughes Jul 17 '11 at 20:01
@smr01cmh: You're welcome! BTW, don't forget to upvote my answer as soon as you have enough reputation to do so. ;-) –  lockstep Jul 17 '11 at 20:03
Will do! I'm currently only at 8! –  cmhughes Jul 17 '11 at 20:16

I had tried your approach but had two problems. One is that it's not really compatible with using \include and \includeonly to fragment the document and only compile parts of it at a time. If you have Section 1 and Section 2 in different files, and have problem files foo.prb1 and foo.prb2, then exclude the first section's file, you need to also tell the second section (which is now the first section read) to look in file foo.prb2. Or something like that. I ended up with lists in the wrong section.

The other thing is that TeX has a limit to the number of files it can have open at once. I believe that number is 16. So unless you close each of the .prb<n> files you will quickly run out (my document has 15 chapters and close to 100 sections).

\documentclass{article}
\usepackage{titletoc}

\newcounter{problem}
\newenvironment{problem}{
\refstepcounter{problem}%
\noindent\textbf{Problem \theproblem} \par
}{}

\makeatletter%\begin{hack}
\def\ttl@partialprb{ppr}
\def\ttl@writepartial#1#2{%
\ttl@topartial{toc}{#1}{#2}%
\ttl@topartial{lof}{#1}{#2}%
\ttl@topartial{lot}{#1}{#2}%
\ttl@topartial{prb}{#1}{#2}%
\ttl@writefile{#1}{#2}}
\makeatother%\end{hack}

\newcommand{\listproblems}{
\startlist{prb}%
\printlist{prb}{}{\subsection*{Problems in this section}}}

\begin{document}

\section{intro}
\listproblems

\begin{problem}
first problem
\end{problem}

\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}


It works for me.

-
Thanks for your answer. Yes, I had (since) realized about the write registers (see my comment above). Since posting I have modified my original code quite a lot, but I'm grateful for your answer, it looks very elegant. –  cmhughes Sep 15 '11 at 2:28
@MatthewLeningang I think the answer I posted on your original question is the modified code I was talking about. –  cmhughes Sep 15 '11 at 2:35