Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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}
share|improve this answer
    
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}%
            \addcontentsline{prb}{section}{heading\theprobSectCounter}%
            \oldsection}

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

% define the \listproblems command
\newread\File
\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%
\read\File to\fileline%
\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]{%
    \StrCount{##2}{heading}[\heading]%
    \ifthenelse{\heading>0}%
    {%
        \StrCount{##2}{heading\theprobSectCounter}[\heading]%
        \ifthenelse{\heading>0}%
        {%
            \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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.