%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Jean-Pierre Drucbert
% ONERA/CERT/GPI
% Office National d'\'Etudes et de Recherches A\'eroSpatiales
% Centre d'\'Etudes et de Recherches de Toulouse
% Groupe de Prestations Informatiques
% Complexe Scientifique de Rangueil
% 2, Avenue \'Edouard Belin
% BP 4025
% F-31055 TOULOUSE CEDEX
% FRANCE
%
% Phone +33-61-55-70-59
% Fax +33-61-55-71-72
% Telex 521 596 F
%
% Email: drucbert@onecert.fr
%
% Please send me any (constructive) suggestions and comments.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% minitoc.sty --- redefines the \chapter command to display a
%   mini-table-of-contents at the beginning of every chapter.
%   Oct-90   Original version, by Nigel Ward.
%   Nov-91   Revised to reuse \chapter, \section, \subsection commands
%      transparently, generate toc-file-name automatically,
%      assorted other cleanup.  Dan Jurafsky
%   Jun/Jul-93  New design, to avoid allocating a newwrite for
%               each chapter (!!)
%      Added \chapterend to terminate the scope of a minitoc
%               Replaced ``minipage'' environnement by a ``verse''
%               environnement, to allow a minitoc split across pages. All
%               the layout of the minitoc is in the
%               \minitableofcontents command, so if someboby wants to redefine
%               that layout, he has just to rewrite it (and only it).
%
%      You can inhibit the minitoc for the next chapter by preceding
%               it with \minitocno.
%      (\minitocyes is useless for the user: it is implicit AFTER the
%               \chapter* pseudo-chapters).
%
%      Problems: you MUST have \chapterend to terminate each chapter
%               with a minitoc.
%      How about avoiding this constraint?
%
%      The depth of the minitoc is user-adjustable with the counter
%               `minitocdepth' (as `tocdepth' for the table of contents).
%               At least three passes (3!!!) of LaTeX are necessary to get
%               correct minitoc's (the first pass create the .mtcX files, the
%               second uses them but they may contain wrong page numbers and
%               recreates them, the third should be ok).
%               Works with \chapter[xxx]{yyy} and floating bodies.
%      Works with two columns (but the minitoc is in composed in one
%               column; how to make it to spread over the two column?)
%               Some mods added to work with xr.sty (external references).
%      Jean-Pierre Drucbert
%
\long\def\@writefilemtc#1#2{\@ifundefined{tf@mtc}{}{%
   \immediate\write\tf@mtc{#2}}
% % for the minitoc
\@ifundefined{tf@#1}{}{%
   \immediate\write\csname tf@#1\endcsname{#2}}%
}
\long\def\@writefileold#1#2{\@ifundefined{tf@#1}{}{% % for the toc
   \immediate\write\csname tf@#1\endcsname{#2}}}

%\let\@mycaption\caption % not needed

% preserved version of \@caption, to use \xaddcontentsline in place of
 \addcontentsline
\long\def\@caption#1[#2]#3{\par\xaddcontentsline{\csname
  ext@#1\endcsname}{#1}{\protect\numberline{\csname
  the#1\endcsname}{\ignorespaces #2}}\begingroup
    \@parboxrestore
    \normalsize
    \@makecaption{\csname fnum@#1\endcsname}{\ignorespaces #3}\par
  \endgroup}

% \xaddcontentsline is like \addcontentsline, but does not writes in
%    the minitoc
\def\xaddcontentsline#1#2#3{\if@filesw \begingroup
\let\label\@gobble \let\index\@gobble \let\glossary\@gobble
\def\protect##1{\string\string\string##1\string\space
   \space}\@temptokena{\thepage}%
\edef\@tempa{\write \@auxout{\string\@writefileold{#1}{\protect
     \contentsline{#2}{#3}{\the\@temptokena}}}}\@tempa
   \if@nobreak \ifvmode\nobreak\fi\fi\endgroup\fi}

\newwrite\tf@mtc              % only ONE newrite; wile be opened/closed as
                              % needed
\newdimen\mtc@indent          % to make the minitoc narrower than normal text
\mtc@indent=48pt              % default value (shoul it made public?)
                              % this command is copied from \narrower of plain
                              % TeX
\def\mtc@narrower{\advance\leftskip\mtc@indent \advance\rightskip\mtc@indent}
                              % \@BBR discourages page breaks
\def\@BBR{\unpenalty\nopagebreak[4]}
\newcounter{minitocdepth}     % minitocdepth is like tocdepth, but for the 
                              % minitoc
\setcounter{minitocdepth}{2}  % by default, subsections and sections.
                              % 1=sections, 3=subsubsections, 4=paragraphs, 
                              % 5=subparagraphs

% Grab the function bindings of these commands so we can redefine
%   them transparently later.
\let\@realchapter\@chapter

% This is so the \chapter* command, which is used for Bibliography's
%  and Tables of Contents, won't generate a mini-toc.
% Now, you MUST use \chapterend to terminate the scope of a minitoc
\def\@chapter[#1]#2{\renewcommand{\tocfile}{mtc\thechapter}\@ifnextchar
 *{\minitocno\@realchapter\minitocyes}{\@mychapter[#1]{#2}}}

% This generates a mini-toc by calling minitableofcontents,
%  (after calling \@realchapter which is bound to the real \chapter,
%  so it sets up the chapter heading, etc.)
%  Each chapter will have a mini-toc in a file named "doc.mtcN" where
%  N is the chapter number, and "doc" is the document (job) name.
\def\@mychapter[#1]#2{\@realchapter[#1]{#2}\minitableofcontents\minitocyes}

% This actually generates the mini-toc in a verse environnement
% in \small characters
\newcommand{\minitableofcontents}{\relax{%
   \parskip=12pt
   \noindent\small\mtc@narrower\parindent=0pt%
   \vspace*{-14mm}\null\hrule\null\vspace*{-20mm}\null~\\\@BBR
   \hspace*{\fill}\begin{verse}\c@tocdepth=\c@minitocdepth
   \itemindent=0pt \labelwidth=0pt \labelsep=\leftmargin \listparindent=0pt%
   ~\\\@BBR % this blanc line is necessary to avoid a wild negative indentation
   \mytableofcontents{\tocfile}~\end{verse}\vspace*{-14mm}\null~\\\@BBR
   \hspace*{\fill}\rule{\columnwidth}{.15mm}\null~\\}% %## the \\ is essential
   \par\vspace{1.5\baselineskip}}   % some space under the minitoc, please

\let\minitoc@\minitableofcontents
      % this flag can be turned of to avoid a minitoc for the
      % following chapter (put \minitocno just before \chapter
      % to suppress the next minitoc). It is turned back ON after.
\def\minitocyes{\global\let\minitableofcontents\minitoc@}
\def\minitocno{\global\let\minitableofcontents\relax}

% This command writes a deferred \closeout command for the minitoc in the
% .aux file
\long\def\addcloseout#1{\if@filesw \begingroup
\let\label\@gobble \let\index\@gobble \let\glossary\@gobble
\def\protect##1{\string\string\string##1\string\space\space}%
\edef\@tempa{\write \@auxout {%
\string\@closemtc#1}}\@tempa
  \if@nobreak \ifvmode\nobreak\fi\fi\endgroup\fi}

% The two following commands are written in the .aux file to perform deferred
% actions
\def\@openmtc#1#2{\let\@writefile\@writefilemtc\immediate\openout #1 =#2}
\def\@closemtc#1{\immediate\closeout#1 \let\@writefile\@writefileold}

% This command writes a deferred \openout command for the minitoc in the .aux
% file
\long\def\addopenout#1#2{\if@filesw \begingroup
\let\label\@gobble \let\index\@gobble \let\glossary\@gobble
\def\protect##1{\string\string\string##1\string\space\space}%
\edef\@tempa{\write \@auxout {\string\@openmtc{#1}{#2}}}\@tempa
  \if@nobreak \ifvmode\nobreak\fi\fi\endgroup\fi}

%   suppress the big bold label "contents," and just output the data itself

\def\my@starttoc#1{\begingroup
  \makeatletter
  \@input{\jobname.#1}\if@filesw  % add a deferred \openout for the minitoc
\addopenout{\tf@mtc}{\expandafter\jobname.#1}
  \fi
  \global\@nobreakfalse \endgroup}

\def\mytableofcontents#1{\my@starttoc{#1}}
\def\chapterend{\minitocyes\addcloseout{\tf@mtc}} % add a deferred closeout for
 the minitoc

\newcommand{\tocfile}{xxx}   % this MUST be reset to some
         % other filename-extension once per chapter.
         % It's only defined here so the renewcommands
         % will function right later.

\minitocyes

\endinput
% This style option should answer to a problem exposed by:
%___________________________________________________________________________
%|~~~\    University of Tokyo    | Jari Vaario, jari@ai.rcast.u-tokyo.ac.jp
%|   | /~~~\   /\   /~~~\~~~T~~~ | Research Center for
%|___/|       /  \  \___    |    |    Advanced Science and Technology/OHlab.
%|  \ |      /~~~~\     \   |    | Komaba 4-6-1, Meguro-ku, 153 Tokyo, Japan
%|   \ \___//      \\___/   |    | tel. +81-3-3481-4486/fax. +81-3-3481-4585
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%
%
