+%%% Table of contents %%%
+
+\newwrite\toc
+\immediate\openout\toc=\jobname.toc
+\newif\iftoc
+\tocfalse
+
+\def\writetoc#1#2{
+ \toctrue
+ \toks0={\the\pageno}
+ \edef\tocaux{\write\toc{\noexpand\expandafter\noexpand\string\noexpand\csname toc#1\noexpand\endcsname{\currentid}{#2}{\the\toks0}}}
+ \tocaux
+ \tocfalse
+}
+
+\newwrite\tok
+\def\includetoc{
+\immediate\openin\tok=\jobname.tok
+\ifeof\tok
+\else
+\input \jobname.tok
+\fi
+\immediate\closein\tok
+}
+
+\def\tocchap#1#2#3{\smallskip\line{\bo #1.~~#2 \dotfill ~#3}}
+\def\tocsec#1#2#3{\line{#1.~~#2 \dotfill ~#3}}
+\def\tocunchap#1#2#3{\smallskip\line{\bo #2 \dotfill ~#3}}
+