% secttocforbook.sty defines \secttoc for sections
% in a book.  \secttoc then lists contents of a section, 
% one depth deeper than minitoc does for chapters.
%
% Assumes invoked with \usepackage{minitoc} \dominitoc
% and re-uses the *.mtc files to get the data.
% Suggest use by
% \usepackage{secttocforbook}
% \setcounter{tocdepth}{1}
% \setcounter{secnumdepth}{3}
%
% The option \usepackage[context]{secttocforbook} 
% additionally lists in the \secttoc all the sections 
% that are in the same chapter as the \secttoc so one 
% can also see the context of that particular section.
%
% A J Roberts, University of Adelaide, 13 Feb 2012
% http://www.maths.adelaide.edu.au/anthony.roberts
%
% process options
% 'context' lists all sections, but not contents, along with
% the contents of the current section.
\newif\ifajr@context \ajr@contextfalse 
\DeclareOption{context}{\ajr@contexttrue}
\ProcessOptions
% define a dummy and the original contentsline
% depending upon the section, link to one of these
\newcommand{\ajr@gobblefour}[4]{}
\AtBeginDocument{% link to original after all preamble
  \let\ajr@orgcontentsline\contentsline
}
% is the contentsline a section or not
\newif\ifitis@sect
\def\ajr@section{\itis@secttrue}
\def\ajr@subsection{\itis@sectfalse}
\def\ajr@subsubsection{\itis@sectfalse}
\def\ajr@paragraph{\itis@sectfalse}
\def\ajr@subparagraph{\itis@sectfalse}
% decide whether to print the line or not depending
\newcommand{\ajr@contentsline}[4]{%
  \csname ajr@#1\endcsname
  \ifitis@sect \stepcounter{mysecttocount} 
    \ifajr@context % list co-section headings as well
      \ifnum\c@section=\c@mysecttocount\else\mtcfont\fi%
      \ajr@orgcontentsline{#1}{#2}{#3}{#4}
    \fi
  \else % print contentsline if inside correct section
    \ifnum\c@section=\c@mysecttocount%
      \ajr@orgcontentsline{#1}{#2}{#3}{#4}
      \else %
      \ajr@gobblefour{#1}{#2}{#3}{#4}
    \fi
  \fi
}
% title for section contents
\def\stctitle{Section contents}
% a counter for the sections in a mtc file
\newcounter{mysecttocount}
% let secttoc be current minitoc but some lines missing
\def\secttoc{}
\renewcommand{\secttoc}{\begingroup
  \ifnum\c@section=0
    \typeout{**** Use secttoc after first section starts ****}
    \else
    \let\mtctitle\stctitle
    \stepcounter{minitocdepth}
    \setcounter{mysecttocount}{0}
    \let\contentsline\ajr@contentsline
    \minitoc
    \addtocounter{minitocdepth}{-1}
  \fi
  \endgroup
}
