% -*- coding: iso-latin-1; -*-
%<*ins>
\def\pkgname{etoc}
\def\pkgdate{2012/12/07}
\def\pkgversion{v1.06}
\def\pkgdescription{easily customizable TOCs (jfB)}
%</ins>
%%
%% Package `etoc' by Jean-Francois Burnol
%% Copyright (C) 2012 by Jean-Francois Burnol
%%
%<*none>
\def\lasttimestamp{Time-stamp: <25-01-2013 12:29:15 CET jfb>}
\def\docdate{2013/01/24}
\def\striptimestamp#1 <#2 #3 #4 #5>{#2 at #3 #4}
\edef\dtxtimestamp{\expandafter\striptimestamp\lasttimestamp}
\ProvidesFile{\pkgname.dtx}
  [`\pkgname' source and documentation (\dtxtimestamp)]
%
% The copyright statement at the top of this file applies to `etoc.dtx'
% and to its derived files.
% 
%    They may be distributed and/or modified under the
%    conditions of the LaTeX Project Public License,
%    either version 1.3 of this license or (at your
%    option) any later version.  The latest version of
%    this license is in:
%
%    http://www.latex-project.org/lppl.txt 
%
%    and version 1.3 or later is part of all distributions of
%    LaTeX version 2003/12/01 or later.  
% 
%  Installation:
%  ============
%
% `latex etoc.dtx'
% `latex etoc.dtx' (again) finishes producing the documentation
%
%  The file `etoc.sty' was generated in the first latex run.
%
%  Put `etoc.sty' in a suitable location within the TeX installation:
%          etoc.sty -> ..path..to../tex/latex/etoc/
%
%  The generated auxiliary files may be discarded.
% 
\begingroup
\input docstrip.tex
\askforoverwritefalse
\def\pkgpreamble{\defaultpreamble^^J\MetaPrefix^^J%
\string\ProvidesPackage{\pkgname}^^J%
\space[\pkgdate\space\pkgversion\space\pkgdescription]}
\generate{\nopreamble
\file{\pkgname.ins}{\from{\pkgname.dtx}{ins}}
\usepreamble\pkgpreamble
\file{\pkgname.sty}{\from{\pkgname.dtx}{package}}}
\endgroup
\iffalse
%</none>
%<*ins>
%-------------------------------------------------------------------------------
%% This file `etoc.ins' is provided for compatibility with TeX distributions
%% expecting to find it for installation of `etoc.sty'.
%%
%% As usual `latex etoc.ins' produces `etoc.sty' from the source `etoc.dtx'
%% (an already existing etoc.sty in the same repertory will be overwritten)
%% 
%% Put the `etoc.sty' file in a suitable location within the TeX
%% installation:    etoc.sty -> ................/tex/latex/etoc/
%%
%% The generated auxiliary files may be discarded.
%%
%% See `etoc.dtx' for the statements of copyright and conditions of
%% distribution or modification.
%%
\input docstrip.tex
\askforoverwritefalse
\def\pkgpreamble{\defaultpreamble^^J\MetaPrefix^^J%
\string\ProvidesPackage{\pkgname}^^J%
\space[\pkgdate\space\pkgversion\space\pkgdescription]}
\generate{\usepreamble\pkgpreamble
\file{\pkgname.sty}{\from{\pkgname.dtx}{package}}}
\endbatchfile
%-------------------------------------------------------------------------------
%</ins>
%<*none>
\fi
\documentclass[a4paper,11pt,abstract]{scrdoc}
\pagestyle{headings}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[hscale=0.66,vscale=0.75]{geometry}
\usepackage{verbatim}
\usepackage{txfonts}
%\usepackage{xspace}
%\usepackage{multicol}
\usepackage{enumitem}
%\usepackage{color}
\usepackage{xcolor}
\usepackage{graphicx}

\definecolor{joli}{RGB}{225,95,0}
\definecolor{JOLI}{RGB}{225,95,0}
\definecolor{BLUE}{RGB}{0,0,255}
\colorlet{niceone}{green!35!blue!75}

\usepackage[english]{babel}
\AtBeginDocument{
  \renewcommand\partname{Part}
  \addto\captionsenglish{\renewcommand\partname{Part}}
}

\usepackage[%dvipdfmx,
pdfencoding=pdfdoc,bookmarks=true]{hyperref}

\hypersetup{%
linktoc=all,%
breaklinks=true,%
hidelinks,%
pdfauthor={Jean-Fran\c cois Burnol},%
pdftitle={The etoc package},%
pdfsubject={LaTeX, table of contents},%
pdfkeywords={LaTeX, table of contents},%
pdfstartview=FitH,%
pdfpagemode=UseOutlines}

% for use when \usepackage{hyperref} is commented out
% \makeatletter
% \let\texorpdfstring\@firstoftwo
% \def\hyperref[#1]#2{{#2}}
% \def\href#1#2{{#2}}
% \makeatother

\MakeShortVerb{\|}

\DeclareRobustCommand\cs[1]{{\ttfamily\hyphenchar\font45\char`\\#1}}
\DeclareRobustCommand\csb[1]{{%
\color{blue}\ttfamily\hyphenchar\font45\char`\\#1}}

\newcommand\cshyp[1]{\texorpdfstring{\cs{#1}}{\textbackslash #1}}
\newcommand\csbhyp[1]{\texorpdfstring{\csb{#1}}{\textbackslash #1}}
\newcommand\lowast{\raisebox{-.25\height}{*}}
\newcommand\starit[1]{\cs{#1\lowast}}
\newcommand\staritb[1]{\csb{#1\lowast}}


\usepackage{etoc}  % loads xspace and multicol
%--------
% add-ons for the new section `surprising uses of etoc' (2013/01/24)
\newcounter{visibletoc}
\makeatletter
\renewcommand{\etocaftertitlehook}{\stepcounter{visibletoc}%
    \addtocontents {toc}{\protect\contentsline 
          {visibletoc}{\thevisibletoc }{\thepage }{\@currentHref }}}
\makeatother
\etocsetlevel{visibletoc}{5}
%--------
% add-ons for the `fancy TOC' (2013/01/25)
    \newcounter{dummypart}
    \newcounter{dummychapter}
    \newcounter{dummysection}
    \etocsetlevel{dummypart}{5}
    \etocsetlevel{dummychapter}{5}
    \etocsetlevel{dummysection}{5}


\newcommand\toc{\cs{table\-of\-contents}\xspace}
\newcommand\localtoc{\cs{local\-table\-of\-contents}\xspace}
\newcommand\tocb{\csb{table\-of\-contents}\xspace}
\newcommand\localtocb{\csb{local\-table\-of\-contents}\xspace}
\newcommand\etoc{%
\texorpdfstring{{\color{joli}\ttfamily\bfseries etoc}}{etoc}\xspace}

\hyphenation{toc-depth sec-num-depth etoc-framed-style etoc-ruled-style}
\hyphenation{etoc-toc-style-with-marks etoc-ruled etoc-framed}
\hyphenation{etoc-standard-display-style}

\frenchspacing

\renewcommand\familydefault\sfdefault

\setcounter{secnumdepth}{3}

% ATTENTION HYPERREF TIENT COMPTE DE TOCDEPTH POUR LES BOOKMARKS!
\setcounter{tocdepth}{3}
% IL FAUT DONC TOUJOURS REMETTRE À 3 AVANT CHAQUE \section ou \subsection !!!

\makeatletter
\def\verbatim@font{\ttfamily\small\baselineskip12pt }%
\makeatother

\begin{document}

\rmfamily
\thispagestyle{empty}


\begin{center}
  {\normalfont\Large The \etoc package}\\
\textsc{Jean-François Burnol}\par
  \footnotesize \ttfamily 
  jfbu (at) free (dot) fr\\
  Package version: \pkgversion\ (\pkgdate)\\
  Documentation generated from the source file\\
  with timestamp ``\dtxtimestamp''\par
\end{center}

\begin{abstract}
  The \etoc package gives to the user complete control on how
  the entries of the table of contents should be constituted
  from the \emph{name}, \emph{number}, and \emph{page number}
  of each sectioning unit. This goes via the definition of
  \emph{line styles} for each sectioning level used in the
  document. The package provides its own custom line styles as
  an example. The simplest usage though will be to delegate the
  details of the layout to packages dealing with list
  environments. 

  The \cs{tableofcontents} command can be used arbitrarily many
  times and has a variant \cs{localtableofcontents} which prints
  tables of contents `local' to the current surrounding
  document unit. An extension of the \cs{label}/\cs{ref} syntax
  allows to reproduce at another location (and with another
  layout) a TOC defined somewhere else in the document.

  The formatting inherited (possibly customized by other packages)
  from the document class can also be used. Regarding the
  \emph{global toc display}, \etoc provides styles based on a
  multi-column format, optionally with a frame or a ruled
  title.

  As the assignment of levels to the sectioning units can be
  changed at any time, \etoc can be used to create custom ``lists
  of'' in a very general manner. It does not create auxiliary
  files other than the usual |.toc| file.
\end{abstract}


\section*{Foreword}

Popular packages dealing with TOCs include |tocloft|,
|titletoc| and |minitoc|. Why another one? well, I started
\etoc for my own use, and only later found out about the above
mentioned packages . . . 

As is well explained in the |tocloft| package documentation,
the standard \LaTeX{} layout for the Table of Contents is
buried in the class definitions. In particular, most of the
lengths therein are hardcoded, and the only way for the user
to change them is to recopy the class definitions into the
document and then change them to obtain what is desired
(within suitable \cs{makeatletter} and \cs{makeatother}).
The more reasonable alternative is to use a dedicated package such as
|tocloft| or to use another flexible document class.

However, although now things are hopefully not hard-coded, one
still has to go through the package or class interface. This
means one has to memorize a (possibly large) number of macros
which will serve only to this task, and one will always be
constrained to customizing one initially given layout.

The spirit of \etoc is something else. The user will deal with
the \emph{name}, the \emph{number} and the \emph{page number}
corresponding to each document sectional division (and
found in a line of the |.toc| file) in a completely arbitrary
manner: they are made available via the \csb{etocname},
\csb{etocnumber}, and \csb{etocpage} commands. 

It is hoped that \etoc will turn out to be essentially
compatible with the |article|, |book|, |report|, |scrartcl|,
|scrbook|, |scrreprt| and |memoir| classes. 

% Preliminary
% testing is positive, but the author not being familiar with the
% latter classes,\footnote{latter=all but |article|...} he does
% not have a stock of documents which could have been used to
% test all their bells and whistles in the presence of
% \etoc. \par

\clearpage

\setcounter{tocdepth}{3}

\etoctoclines
\etocmarkboth\contentsname
\etocmulticolstyle{}
\tableofcontents \label{toc:main}


\part{Overview}

\setcounter{tocdepth}{-3}
\localtableofcontents \label{toc:partone}
\setcounter{tocdepth}{3}

\section{Initial motivation: nested lists}

The initial impetus  was to feed nested
list environments with the data consisting of the \emph{name},
\emph{number}, and \emph{page number} as recorded in the
|.toc| file.\footnote{I got
  started about this through reading this question on the
  \TeX{} Stackexchange site:\\ 
\href{http://tex.stackexchange.com/questions/79682/can-i-get-a-list-of-all-sections-as-a-simple-enumerate-list}{tex.stackexchange.com/questions/79682}}
For example, typesetting
the line corresponding to the first sub-section in a given
section would open a list environment which would be closed only when a
section, chapter, or part line entry in the |.toc| file would
be encountered. \etoc allows to do this very easily
and the opening and closing may be for example
|\begin{enumerate}| and |\end{enumerate}| pairs, will all the
customizing allowed by packages such as |enumitem|. 

\subsection{Limitations intrinsic to the use of environments}

There is a first limitation to this method: the |.toc| file
may contain other commands, such as language changing
commands, which do not expect to see their scope limited in
this way inside a group (\LaTeX's environments create groups).
Therefore the package own line styles
(illustrated by the main table of contents in this document)
do not make use of environments to avoid that problem.

A second limitation is that one may nest at most 4 levels of
enumerate environments, and 4 levels of itemize environment. I
tried alternating them and did succeed to nest 6 levels (and
not 8 alas . . . \footnote{this is
  surely a well-known issue which I did not at all
  investigate any further.}). With
\csb{etocnumber} as the optional parameter to \cs{item}:
\cs{item}|[\etocnumber]| one may transform the itemize into an
enumerated list... anyway, 4 levels of sectional divisions in
a TOC are generally sufficient, and again using |enumerate|
environments is only a possibility provided by \etoc, it is by
no means mandatory to use them in the line styles
specifications.

We will give in this manual a simple-minded example of nested
use of |enumerate| environments. More sophisticated examples
would use more sophisticated |enumitem| options. One may say
then that again the user has to memorize some customizing!
indeed, but the syntax and option names to memorize are in no
way related only to matters of tables of contents, hence an
economy of use of the poor brain.

Still a different sort of example,
less elementary and not making use at all of list-making
environments, is built-in in \etoc own ``line styles''. They
are activated by the command \csb{etoctoclines}... and do
come with their own set of customizing macros!

\section{Line styles and global style}

I should have distinguished between the \emph{line styles}
(the way the name, number and page numbers are used at each
level) which I was alluding to until now and the \emph{toc
  display style} (I am using this for lack of a better name)
which tells how the title should be set, whether an entry in
the |.toc| file should be made, whether the contents should be
typeset with multiple columns, etc...

\subsection{\csbhyp{etocsettocstyle} for the toc display}

The low-level \csb{etocsettocstyle} command allows to decide
what should be done before and after the line entries of the
TOC are typeset, and in particular how the title should be
printed.

\subsubsection{Multiple columns, ruled title, framed contents}

\etoc provides four (customizable...) toc styles which are
variants on the use of the |multicol| package:
\csb{etocmulticolstyle}, \csb{etoctocstyle},
\csb{etocruledstyle}, and \csb{etocframed\-style}. All use the
|multicol| package with a default of two columns
(single-column mode is of course allowed).

These commands must be followed either by \toc or \localtoc,
and dropping the `style' in the command name gives shortcuts
(except for \cs{etoctoc} which does not exist)
for doing the whole thing in one go (and inside a group).

\subsection{\csbhyp{etocsetstyle} for the line styles}

The command to inform \etoc of what to do with \cs{etocname},
\cs{etocnumber}, and \cs{etocpage} is called
\csb{etocsetstyle}. It has five mandatory arguments. The first
one is the name of the sectional unit: a priori known names
are |book|, |part|, |chapter|, |section|, |subsection|,
|subsubsection|, |paragraph|, and |subparagraph|. The four
other arguments say: 1) what to do when this level is first
encountered, down from a more general one, then 2) \& 3) (two
arguments, a `prefix' and a `contents') what to do when a new
entry of that type is found, and 4) the last argument is the
code to execute when a division unit of higher importance is
again hit upon.

\subsubsection{Instructing \etoc about the document sectional division names}

Other names (\emph{atom}, \emph{particle} . . .) for sectional
divisions may be made known to \etoc with the
command \csb{etocsetlevel}.\footnote{however, no
  more than the eight distinct levels from -2 to +5 may be used.} 

\subsection{Compatibility mode}

Both for the ``line styles'' and the ``global style'', it is
possible to switch into a compatibility mode which uses the
document class default style.\footnote{for the ``global style''
  \etoc checks if it knows the class, and if not defaults to
  the |article| class layout.} This is activated by:
\begin{verbatim}
\etocstandardlines        % `line entries' as without \usepackage{etoc}
\etocstandarddisplaystyle % `toc display'  as without \usepackage{etoc}
\end{verbatim}
Actually, starting with version |1.05|, this is the default at
the start of the document, and just adding
\cs{usepackage\{etoc\}} should hopefully not change anything
to the look of a document, at least for the |article|, |book|,
|report|, |scrartcl|, |scrbook|, |scrreprt| and |memoir|
classes.

After declaring a document division name to \etoc with
\cs{etocsetlevel}, the \cs{etocstandardlines} compatibility switch
should be functional under the condition that the document class does
things in a manner analogous to the standard classes. But at any rate
the \etoc default line styles and the ones defined via \cs{etocsetstyle}
will work. Except that maybe \etoc will fail to separate the number from
the name.

\section{Arbitrarily many TOCs, and local ones too}

\etoc allows arbitrarily many \cs{tableofcontents} commands in
your document. The line styles and the global style may of
course be changed in-between. Furthermore 
\cs{localtableofcontents} will print local tables of
contents\footnote{Up to version |1.04| we called this
  \starit{tableofcontents}, but for reasons of compatibility
  with the |memoir| class, we have decided to drop this
  usage.}: \emph{i.e.} all sections and sub-units inside a
given chapter, or all subsubsections and lower inside a given
subsection, etc . . .

\subsection{On manually adding layout commands to the
  \texorpdfstring{\ttfamily .toc}{.toc} file}

When using in that way many tables of contents in the same
document one should of course beware of adding manually things
to the |.toc| file. For example the technique of inserting\\
\hbox to \linewidth{\hss
  \cs{addtocontents}|{toc}{\string\clearpage}|\hss} just
before a \cs{part} to fix the problem when some part entry (in
the table of contents) is isolated at the bottom of one page
will cause problems with multiple TOCs: this \cs{clearpage}
will be executed by \etoc each time a \toc or \localtoc
command is encountered! The more prudent thing is to have
issued rather:\\
\hbox to
\linewidth{\hss\cs{addtocontents}|{toc}{\string\myclearpage}|\hss}
and to use where needed something like:
\begin{verbatim}
\let\myclearpage\clearpage
\tableofcontents
\let\myclearpage\relax
\end{verbatim}
The |memoir| class has the command \cs{settocdepth} which
writes a \cs{changetoc\-depth} command inside the |.toc| file.
This will impact the typesetting by \etoc of \emph{all} tables
of contents, with possibly unexpected results: imagine the
document has \cs{settocdepth}|{chapter}| at some point to
avoid having the sections from subsequent chapters be listed
in the main table of contents. Then a local table of contents
in one of these chapters will print a title but will be
without any entry. A solution is to do \cs{begingroup}
|\renewcommand*\changetocdepth[1]{}| \cs{localtableofcontents}
\cs{endgroup}, and to set the desired level for the local
table of contents with the other |memoir| command \cs{maxtocdepth}.
\footnote{The |memoir| class allows multiple calls to the \toc
  command, so these issues already arise there,
  independently of \etoc, see page 170 of the |memoir|
  manual.}


\subsection{Labeling and reusing elsewhere}

\etoc allows the labeling of a TOC with \cs{label\{toc:A\}}
and will redisplay it elsewhere when told
\cs{tableofcontents}\cs{ref\{toc:A\}}. Again, the actual
layout (title inclusive) is decided locally. The
line styles and global style (including the title) will be the
current ones and the current value of the |tocdepth| counter is
obeyed. As an example here is the table of contents of Part
\ref{part:custom}:

\begingroup

\etocstandardlines
\renewcommand{\etocbkgcolorcmd}{\color{green!5}}
\fboxsep1ex

\etocframed[1]{\fbox{\makebox[.5\linewidth]{\etocfontminusone I am from
    \hyperref[toc:c]{far away}}}}
\label{toc:d}
\ref{toc:c}

\endgroup

We actually did something like:\\
\hbox to \linewidth{\hss
  \toc\space\cs{label}|{toc:d}|\cs{ref}|{toc:c}|\hss} Hence
\hyperref[toc:d]{\color{niceone}the present location}
can itself now be referred to via |\ref{toc:d}|: it gives
the id of this TOC\footnote{\emph{i.e}
{\color{niceone}\ref{toc:d}}, there was an invisible
TOC with id 2 at the beginning of this part.} in the sequence of
document TOCs, and will be a link if package |hyperref| is
used.

However one should not use elsewhere \toc|\ref{toc:d}|. Due to the way
\etoc implements the cloning,\footnote{The double cloning had a
  different (but also wrong) effect in |v1.05| compared to |v1.06|.} 
  the doubly cloned TOC will be typeset as a full table of contents. So
  to clone again, one should use the original: \toc|\ref{toc:c}|.

The \cs{label} command should follow \toc  before the reference via
\cs{ref} to the other TOC. Indeed a \cs{label} following it is
detected by \toc (skipping spaces and an optional \texttt\lowast). A
\cs{label} after the \cs{ref} will not be treated by \etoc
itself and will refer to the latest reference point, possibly
to some counter updated in the title of the TOC or, although
this would require the user to have done a suitable set-up in the
line styles, even some counter modified inside the TOC.

As another example let us  display a table of contents for
this first part of the document. Indeed, immediately after the
line in the source file starting with the first
\cs{part} we wrote:

\vspace{-.25cm}
\begin{verbatim}
\setcounter{tocdepth}{-3}
\localtableofcontents \label{toc:partone}
\setcounter{tocdepth}{3}
\end{verbatim}
The |tocdepth| being set to -3, nothing at all was inserted in the page
(\etoc 
cancels printing even the heading of the TOC if the |tocdepth| is -2 or
less, except for the |memoir| class for which it needs to be -3 or less).
We can then display the TOC with:
\begin{verbatim}
\subsection{Local table of contents for this part}
\tableofcontents \ref{toc:partone}
\end{verbatim}

\subsection{Local table of contents for this part}

\setcounter{tocdepth}{3}

\begingroup
\colorlet{subsecnum}{black}
\colorlet{secbackground}{green!30}
\colorlet{tocbackground}{red!20!green!20}

\renewcommand{\etocbkgcolorcmd}{\color{tocbackground}}
\renewcommand{\etocleftrulecolorcmd}{\color{tocbackground}}
\renewcommand{\etocrightrulecolorcmd}{\color{tocbackground}}
\renewcommand{\etocbottomrulecolorcmd}{\color{tocbackground}}
\renewcommand{\etoctoprulecolorcmd}{\color{tocbackground}}

\renewcommand{\etocleftrule}{\vrule width 3cm}
\renewcommand{\etocrightrule}{\vrule width 1cm}
\renewcommand{\etocbottomrule}{\hrule height 12pt}
\renewcommand{\etoctoprule}{\hrule height 12pt}

\renewcommand{\etocinnertopsep}{0pt}
\renewcommand{\etocinnerbottomsep}{0pt}
\renewcommand{\etocinnerleftsep}{0pt}
\renewcommand{\etocinnerrightsep}{0pt}

\newcommand\shiftedwhiterule[2]{%
    \hbox to \linewidth{\color{white}%
    \hskip#1\leaders\vrule height1pt\hfil}\nointerlineskip
\vskip#2}

\etocsetstyle{subsubsection}{\etocskipfirstprefix}
{\shiftedwhiterule{\leftskip}{6pt}}
{\sffamily\footnotesize
\leftskip2.3cm\hangindent1cm\rightskip.5cm\noindent
\hbox to 1cm{\color{subsecnum}\etocnumber\hss}%
\color{black}\etocname\leaders\hbox to .2cm{\hss.}\hfill
\rlap{\hbox to .5cm{\hss\etocpage\hskip.1cm}}\par
\nointerlineskip\vskip3pt}
{}

\etocsetstyle{subsection}{\etocskipfirstprefix}
{\shiftedwhiterule{1.5cm}{6pt}}
{\sffamily\small
\leftskip1.5cm\hangindent.8cm\rightskip.5cm\noindent
\hbox to .75cm{\color{subsecnum}\etocnumber\hss}%
\color{black}\etocname\leaders\hbox to .2cm{\hss.}\hfill
\rlap{\hbox to .5cm{\hss\etocpage\hskip.1cm}}\par
\nointerlineskip\vskip3pt}
{}

\newcommand{\coloredstuff}[2]{%
            \leftskip0pt\rightskip0pt\parskip0pt
            \fboxsep0pt % \colorbox uses \fboxsep also when no frame!
       \noindent\colorbox{secbackground}
               {\parbox{\linewidth}{%
                    \vskip5pt
                    {\noindent\color{#1}#2\par\nointerlineskip}
                    \vskip3pt}}%
       \par\nointerlineskip}

\etocsetstyle{section}{\coloredstuff{blue}
     {\hfil \bfseries\large Contents of Part One\hfil}}
{\vskip3pt\sffamily\small}
{\coloredstuff{blue}{\kern1cm\hbox to .5cm{\etocnumber\hss}%
    \etocname\hfill
{\hbox to .5cm{\hss\etocpage\hskip.1cm}}}%
 \vskip6pt}
{}

\etocframedstyle[1]{}

\tableofcontents* \ref{toc:partone}

\endgroup

The actual design is not pre-built in \etoc; it uses the \etoc
`framed' style with a background color and a dummy empty
title, the actual title having been put inside the frame as
part of the \meta{start} code of the section `line style'. The
frame borders have been set to have the same color as the one
which serves as background for the entire thing. This design
(with other colors) is in use also for
\hyperref[toc:clone]{\color{niceone}this other toc},
and the coding used is to be found at its location.

\section{Surprising uses of \etoc}
\label{sec:surprising}

\subsection{The TOC of TOCs}

\begingroup
\makeatletter
% to get rid of the \par...
\def\table@fcontents{%\par
    \refstepcounter{etoc@tocid}% 
    \Etoc@tocwithidfalse
    \futurelet\Etoc@nexttoken\Etoc@t@bleofcontents}
\makeatother

\etocsetlevel{part}{1}
\etocsetlevel{chapter}{1}
\etocsetlevel{visibletoc}{0}
\etocsetstyle{visibletoc}{\etocskipfirstprefix}{, }{{\color{niceone}\etocname}}{}
\etocnakedtocdisplaystyle
\setcounter{tocdepth}{0}


Here is the numbered and linked list of all tables of contents which are
displayed within this document: 
\tableofcontents\setcounter{tocdepth}{3}\endgroup. And to obtain
it here we just wrote:\footnote{click the `3' from the list to confirm 
that this is indeed the third visible table of contents of this
document...}\par\smallskip
{\leftskip3cm\rightskip\leftskip
    \ttfamily\small\baselineskip11pt \noindent Here is the numbered and
    linked list of all tables of contents which are displayed
    within this document:~\string\tableofcontents.\par}

  The preparatory work was the following. First, we defined a
  counter |visibletoc| whose vocation is to get incremented at
  each displayed toc. \etoc has its own private counter but it
  counts all TOCs, even those not displayed because the |tocdepth|
  value was |-2| or |-3|.

We could have added manually |\refstepcounter{visibletoc}| and
|\label| commands at
all suitable locations in the document source, and use here |\ref|
commands, but this requires quite some manual editing of the
source.

There is a much better way: there is a hook
\cs{etocaftertitlehook} and we told it to increment the
|visibletoc| counter and to write a line to the |.toc| file,
exactly as sectioning commands such as |chapter|, |section|, or
|subsection| do. The name of the fictitious unit was chosen to be
again |visibletoc|.

In the preamble of the document we did
|\etocsetlevel{visibletoc}{5}| and in this subsection
|\etocsetlevel{visibletoc}{0}|, |\setcounter{tocdepth}{0}|, and we
also assigned to |part| and |chapter| level |1|. So the printed
table of contents uses \emph{only} the `visibletoc' entries from
the |.toc| file. The other entries are just discarded. We also
defined a simple style with \cs{etocsetstyle} for inline display.

To tell the entire truth the current version |1.06| of \etoc
issues |\par| automatically before typesetting a table of
contents, so we had to copy the suitable inner macro and remove
from it the |\par|. Next release of \etoc will have a public
command to tell the package not to do this |\par| (today I don't
want to modify the code source, only enhance a bit this
documentation).

Here are the implementation details:

\begingroup
\begin{verbatim}
... in the preamble:
\newcounter{visibletoc}
\makeatletter
\renewcommand{\etocaftertitlehook}{\stepcounter{visibletoc}%
    % no \refstepcounter needed nor useful as etoc's inner counter
    % for TOCs got incremented just before
    \addtocontents {toc}{\protect\contentsline 
          {visibletoc}{\thevisibletoc }{\thepage }{\@currentHref }}}
    % we do not use \addcontentsline which is patched by hyperref
    % In this way no bookmark creation, only hyperlinks.
\makeatother

% Only displayed TOCs will execute this hook. Invisible TOCs defined
% for display elsewhere do not. The package has a private counter
% which we didn't use as it counts all TOCs. The contentsline data
% defines an `unnumbered' entity (there is no \numberline macro
% in the last parameter), whose name is the current value of the
% counter visibletoc. So we will use \etocname.

\etocsetlevel{visibletoc}{5} % hence the added data will remain
                             % invisible as long as tocdepth < 5

\begin{document}
... document body
\subsection{Surprising uses of etoc}
\begingroup
\etocsetlevel{part}{1}
\etocsetlevel{chapter}{1}
\etocsetlevel{visibletoc}{0}
\etocsetstyle{visibletoc}
    {\etocskipfirstprefix}{, }{{\color{niceone}\etocname}}{}
\etocsettocstyle{}{}      % don't set any title, rules or frame or multicol!
\setcounter{tocdepth}{0}  % display only the `visibletoc' entries from .toc

Here is the numbered and linked list of all tables of contents which are
displayed within this document: \tableofcontents.
\endgroup
\end{verbatim}
\endgroup

Subsection \ref{subsec:interverting} gives another use of the
shuffling of levels. 

% ATTENTION HYPERREF TIENT COMPTE DE TOCDEPTH 
% MAIS C'EST DEBILE ÇA ! IL DEVRAIT Y AVOIR UN
% SETTING GLOBAL
% \setcounter{tocdepth}{3} % fait plus haut

\subsection{Arbitrary ``List Of...''}

This idea of interverting the levels is very powerful and allows
to let \etoc display lists of arbitrary things contained in the
document. All of that still
using nothing else than the |.toc| file! 
Example: imagine a document with dozens of
exercises, perhaps defined as |\newtheorem{exercise}{}[section]|. Let
us explain how to instruct \etoc to display an hyperlinked list
of all these exercises. For this we put in the preamble:
\begingroup
\makeatletter
\def\verbatim@font{\ttfamily\small\baselineskip12pt }%
\makeatother
\begin{verbatim}
 \newtheorem{exerci}{}[section]
 \newcommand{\exercisetotoc}{\addcontentsline{toc}{exercise}{\theexerci}}
 \newenvironment{exercise}{\begin{exerci}\exercisetotoc}{\end{exerci}}
 \etocsetlevel{exercise}{5}
\end{verbatim}
Then, the list of exercises can be typeset in a four-column format
with the following code:
\begin{verbatim}
\begingroup
  \etocsetlevel{exercise}{2}
  \etocsetlevel{chapter}{3} \etocsetlevel{subsection}{3} \etocsetlevel{part}{3}
  \setcounter{tocdeph}{2}
  \etocsetstyle{exercise}{}{}
    {\noindent\etocname\strut\leaders\etoctoclineleaders\hfill\etocpage\par}
    {\pagebreak[2]\vskip\baselineskip}
  \etocsetstyle{section}{}{}
    {\noindent\strut{\bfseries\large\etocnumber\hskip.5em\etocname}\par
     \nopagebreak[3]}{}
  \etocruledstyle[4]{\Large\bfseries List of the exercises}
  \setlength{\columnseprule}{.4pt}
  \tableofcontents
  \setcounter{tocdepth}{3}
\endgroup
\end{verbatim}
The above was motivated by question |94766| on the
\TeX-StackExchange site. 

When package |hyperref| is used, all
these exercise numbers will be hyperlinks. But |hyperref| will
also add bookmarks to be used if the output format is PDF. One
needs to inform it of a suitable level to be used for these
bookmarks, so here for example this could be |2|, so that for
bookmark purposes the exercises will be considered as sub-units of
sections. This goes in the preamble of the document:
\begin{verbatim}
\makeatletter
\providecommand*{\toclevel@exercise}{2}
\makeatother
\end{verbatim}
It is also possible to prevent |hyperref| from creating any bookmark
for the exercices. This does it:
\begin{verbatim}
\makeatletter
 \newcommand{\exercisetotoc}{%
   \addtocontents {toc}{\protect\contentsline 
          {exercise}{\theexerci }{\thepage }{\@currentHref }}}
\makeatother
\end{verbatim}
\endgroup

\subsection{A TOC with a fancy layout}

Another question (numbered |61297|) on the \TeX{} StackExchange
site was about using \LaTeX{} to obtain a table of contents where
the sections from a given chapter would be represented by a number
range (like 18--22 for a given chapter, 42--49 for another one ...
of course to be inserted automatically in the TOC). The next page
is the result of my effort at using \etoc for this problem. How
this was done will be found on the above cited site.

This is not an image inclusion, the TOC was recreated entirely
within this document via the creation of dummy part, chapter and
section units as a well as a dummy page count. The dummy data was
created using loops and found its way into the |.toc| file. 
And the table of
contents was then displayed after issuing:
\begin{verbatim}
    \etocsetlevel{dummypart}{-1}
    \etocsetlevel{dummychapter}{0}
    \etocsetlevel{dummysection}{1}
    \etocsetlevel{part}{2}
    \etocsetlevel{chapter}{2}
    \etocsetlevel{section}{2}
    \setcounter{tocdepth}{1}
\end{verbatim}
In this way the styles used to typeset originally the TOC from a
dedicated |.tex| file could be
inserted in the present documentation (as the current version of
\etoc does not have user commands to import a |.toc| file from
another document, we had to create as mentioned above the
apparatus for the generation of these dummy parts, chapters, sections).

\clearpage

\begingroup

    \etocsetlevel{dummypart}{-1}
    \etocsetlevel{dummychapter}{0}
    \etocsetlevel{dummysection}{1}
    \etocsetlevel{part}{2}
    \etocsetlevel{chapter}{2}
    \etocsetlevel{section}{2}

    \newif\ifextraidone
    \newif\ifextraiidone

    \etocsetstyle{dummypart}
    {}
    {}
    {\begin{center}\Large\bfseries PART \etocnumber
    \ifextraiidone\\\etocname.\fi\end{center}
    \ifextraidone\else
    \noindent\hskip.7\linewidth
        \hbox to .2\linewidth
%
% je modifie ici en ajoutant un \hss, pour compenser la plus petite
% taille de txrm. Ok.
        {\hss\small\textsc{\bfseries Sections.\hss Page.}}\par\fi
    \extraidonetrue
    }
    {}

    \newcommand\mytocleaders{\hbox to .125\linewidth{...\hss}}
    \etocsetstyle{dummychapter}
    {}
    {\par\noindent\etocifnumbered
        {\makebox[.15\linewidth][r]{\bfseries\etocnumber.}}
        {\hspace*{.15\linewidth}}}
    {\hbox to .65\linewidth
        {\hspace{1em}\etocname\unskip\leaders\mytocleaders\hss}%
     \hskip-.1\linewidth
     \hbox to .2\linewidth{\hspace{1.5em}...\hss
                    \etocifnumbered
                        {\etocpage}
                        {\ifextraiidone\else\etocpage\fi}}%
     \hskip-.2\linewidth
    }
    {}

    \newbox\forsectionnumbers
    \makeatletter
    \etocsetstyle{dummysection}
    {\setbox\forsectionnumbers=\hbox to .1\linewidth
        \bgroup\hss\etocskipfirstprefix}
    {\@gobble}
    {\etocnumber---}
    {\etocnumber\egroup
    % for reasons I do not quite understand, in some pdf viewers the dots
    % do not completely disappear if here \fboxsep0pt is used.
        \fboxsep=.5pt\colorbox{white}{\box\forsectionnumbers}\par
        \ifextraiidone\else
            \begin{center}\bfseries Concord.\end{center}
            \noindent\makebox[.15\linewidth][r]{\textsc{\bfseries Lesson.}}\par
        \fi\extraiidonetrue
    }
    \makeatother

    
    \renewcommand{\etocinnertopsep}{0pt}

    \etocruledstyle[1]{%
        \parbox{\linewidth}{%
            \centering
            \textsc{\bfseries\LARGE\MakeUppercase{Table of Contents}}\\
            \rule{.2\linewidth}{2pt}}%
    }

    \setcounter{tocdepth}{1}
    \leavevmode\vfill
    \tableofcontents
    \vfill\clearpage
    \setcounter{tocdepth}{3}

\makeatletter
    \def\adddummysection {\stepcounter{dummysection}%
      \addtocontents {toc}{\protect\contentsline 
          {dummysection}{\protect\numberline{\thedummysection}}
                   {\the\dummypage }{\@currentHref }}}

    \def\adddummychapter #1{\stepcounter{dummychapter}%
      \advance\dummypage2
      \addtocontents {toc}{\protect\contentsline 
          {dummychapter}{\protect\numberline{\thedummychapter}#1}
                   {\the\dummypage }{\@currentHref }}}

    \def\adddummychapno #1{\advance\dummypage2
      \addtocontents {toc}{\protect\contentsline 
          {dummychapter}{#1}{\the\dummypage }{\@currentHref }}}

    \def\adddummypart #1{\stepcounter{dummypart}%
       \advance\dummypage4
       \addtocontents {toc}{\protect\contentsline 
          {dummypart}{\protect\numberline{\Roman{dummypart}}#1}
                     {\the\dummypage }{\@currentHref }}}
     
\makeatother
    
    \newcount\dummypage \dummypage-1

    \adddummypart {}
    

    \adddummychapno {Introductory}

    \newcount\tempcount

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<8
    \repeat

    \adddummychapter {Concord of Subject and Verb}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<17
    \repeat

    \adddummychapter {Concord of Substantive and Adjective}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<22
    \repeat

    \adddummychapno {Concord of Relative and its Antecedent}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<25
    \repeat

    \adddummypart {Government}

    \adddummychapter {The Accusative Case}

    \adddummychapno {General uses}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<30
    \repeat

    \adddummychapno {Particular uses}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<37
    \repeat

    \adddummychapter {Verbs governing two Accusatives}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<41
    \repeat

    \adddummychapter {The Causal}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<49
    \repeat

    \adddummychapter {The Instrumental Case}

    \adddummychapno {General uses}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<54
    \repeat

    \adddummychapno {Particular uses}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<59
    \repeat

    \adddummychapter {The Dative Case}

    \adddummychapno {General uses}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<65
    \repeat

    \adddummychapno {Particular uses}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<71
    \repeat

    \adddummychapter {The Ablative Case}

    \adddummychapno {General uses}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<75
    \repeat

    \adddummychapno {Particular uses}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<86
    \repeat

    \adddummychapter {The Locative Case}

    \adddummychapno {General uses}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<92
    \repeat

    \adddummychapno {Particular uses}

    \loop\advance\tempcount by 1
    \adddummysection
    \ifnum\tempcount<100
    \repeat
\endgroup

\part{Package commands for line styles}

\setcounter{tocdepth}{3}

\etocsetstyle{section}
{\begin{enumerate}[leftmargin=.75cm, label=\etocifnumbered
      {{\fboxrule1pt\fcolorbox{green}{white}{\etocnumber}}}{}]}
% Dec 7, 2012. I hit upon a problem whose origin I found was
% with xcolor, as xcolor modifies \fbox
% (try \section{\fbox{A}} with and without xcolor). The fix
% was to \protect the \fbox used in the |enumitem| label.
% Strangely enough, with |hyperref| active, the problem did
% not show up. Anyhow I now use \fcolorbox reather than \fbox
% and there is no problem anymore. Also I don't have now to
% use \normalcolor which already needed protection.
{\normalsize\bfseries\rmfamily\item}
{\etocname (page \etocpage)}
{\end{enumerate}}

\etocsetstyle{subsection}
{\begin{enumerate}[leftmargin=0cm, label=\etocnumber]}
{\normalfont \item}
{\etocname (p.~\etocpage)}
{\end{enumerate}}

\etocsetstyle{subsubsection}
{\par\nobreak\begingroup\normalfont\footnotesize\itshape\etocskipfirstprefix}
{\allowbreak\,--\,}
{\etocname\unskip}
{.\hfil\par\endgroup\pagebreak[3]}

\etocruledstyle[1]{\etocfontminusone\color{green}
     \fboxrule1pt\fboxsep1ex
     \framebox[\linewidth]
              {\normalcolor\hss Contents of this second part\hss}}
\localtableofcontents \label{toc:a}

\section{The \csbhyp{etocsetstyle} command}


\subsection{An example}

Let us explain how \etoc was used to produce the table of
contents displayed at the beginning of this second part.
This
is a local table of contents, and we used the command \localtoc.


We shall distinguish between the \emph{line styles} and the
\emph{toc display style}. The line styles were (essentially)
obtained in the following manner:%
\footnote{the present document has
  {\ttfamily\string\renewcommand\string{%
      \string\familydefault\string}\string{\string\sfdefault\string}}
  in its preamble, hence \cs{normalfont} switches to the
  |sans| typeface; so in the section line-style, I wrote
  \cs{rmfamily} instead.}

\begingroup\small
\begin{verbatim}
\setcounter{tocdepth}{3}
\etocsetstyle{section}
{\begin{enumerate}}
{\normalsize\bfseries\rmfamily\item}
{\etocname (page \etocpage)}
{\end{enumerate}}

\etocsetstyle{subsection}
{\begin{enumerate}}
{\normalfont\item}
{\etocname (p.~\etocpage)}
{\end{enumerate}}

\etocsetstyle{subsubsection}
{\par\nobreak\begingroup\normalfont
        \footnotesize\itshape\etocskipfirstprefix}
{\allowbreak\,--\,}
{\etocname\unskip}
{.\hfil\par\endgroup\pagebreak[3]}
\end{verbatim}
\endgroup

Each \csb{etocsetstyle} command has five mandatory arguments:\\
\hbox to \linewidth
{\hfil\csb{etocsetstyle}\color{blue}\marg{levelname}%
  \marg{start}\marg{prefix}\marg{contents}\marg{finish}\hfil}
The initially recognized \meta{levelname}'s are the sectioning
levels of the standard document classes: from \emph{part} (or
\emph{book} which is used by the |memoir| class) down to
\emph{subparagraph}.

The \meta{start} code is executed when a toc entry of that level is
encountered and the previous one was at a higher level. The
\meta{finish} code is executed when one again encounters a higher
level toc entry. In the mean-time all entries for that level are
typeset by executing first the \meta{prefix} code and then the
\meta{contents} code. 

\subsubsection{\csbhyp{etocname} and \csbhyp{etocpage}}

The line styles specifications use the commands \csb{etocname}
and \csb{etocpage} as synonyms for the name, respectively the
page number of the current entry. \footnote{\etoc uses the |xspace|
  package, and there is no need to write
  \cs{etocname\string{\string} text} rather than just
  \cs{etocname text} for example. However, this forced us to
  add a mysterious \cs{unskip} in the
  subsubsection style.} Temporarily, we relied on
the automatic numbering of the |enumerate| environments but
there is also a command \csb{etocnumber} to get the real thing
from the |.toc| file.


\subsubsection{The \csbhyp{etocskipfirstprefix} command}

The chosen |subsubsection| style specifies to list the names of
the subsubsections from a given subsection in an in-line
manner, with -- as a separator. The command
\csb{etocskipfirstprefix}, which, if present, \emph{must} be
the very last one in the \emph{start} code, instructs to not
use that separator of the first item. 

With this style, one would have 
to be imaginative to design something then for paragraph and
subparagraph entries! perhaps as superscripts? Well, usually
one does not need paragraphs and subparagraphs numbered and
listed in the TOC, so our putative user here chose a design
where no provision is made for them and added the definitive:
\begin{verbatim}
\etocsetstyle{paragraph}{}{}{}{}
\etocsetstyle{subparagraph}{}{}{}{}
\end{verbatim}
This is also the situation with the default package line styles!

\subsubsection{secnum\-depth and tocdepth}

The \LaTeX{} counter |secnumdepth| decides down to which level division
headings are numbered. Numbered or not they are (at least in the standard
classes) written to the |.toc| file. The counter |tocdepth| decides then the
finest level which will be displayed in the table of contents: its local
value is obeyed by \etoc, and as the \toc command and its variants may now
be used multiple times, this is an important customization lever.


\subsection{The \csbhyp{etocsetlevel} command}

If your document uses other sectioning names, such as, for
example \emph{molecule}, or \emph{atom}, it should inform \etoc of
this in the following way:
\begin{verbatim}
\etocsetlevel{cell}{0}
\etocsetlevel{molecule}{1}
\etocsetlevel{atom}{2}
\etocsetlevel{nucleus}{3}
\end{verbatim}
The accepted numbers range from -2 to 5 inclusive. The package code
does:
\begin{verbatim}
\etocsetlevel{book}{-2}
\etocsetlevel{part}{-1}
\etocsetlevel{chapter}{0}
\etocsetlevel{section}{1}
\etocsetlevel{subsection}{2}
\etocsetlevel{subsubsection}{3}
\etocsetlevel{paragraph}{4}
\etocsetlevel{subparagraph}{5}
\end{verbatim}

\subsection{Further informations and let's make this a very long title to see how it will look like}


\subsubsection{The \csbhyp{etocnumber} command}
So far, our specifications would use the numbering generated
by the |enumerate| environments, but it would be nicer to use
the actual numbers as found in the |.toc| file. This is
available via the \csb{etocnumber} command. To get the labels
in the |enumerate| list to use it we can proceed with the
syntax {\ttfamily label=\char32} from the package |enumitem|:
\begin{verbatim}
\etocsetstyle{section}
{\begin{enumerate}[label=\etocnumber]}
{\normalsize\bfseries\rmfamily\item}
{\etocname (page \etocpage)}
{\end{enumerate}}
\end{verbatim}
Rather than just \cs{etocnumber} we then used something like
|\fbox{\etocnumber}|.


\subsubsection{The \csbhyp{etocifnumbered} switch}

The \cs{fbox} would give an unaesthetic result in the case of
an unnumbered section (which ended up in the table of
contents via an \cs{addcontentsline} command).\footnote{as
  seen below we use \cs{fcolorbox} rather than \cs{fbox}. Due
  to some redefinition made by package |xcolor|, had we used
  \cs{fbox} (and not used |hyperref|) we would have needed
  \cs{protect}\cs{fbox}.}

The \cs{etocifnumbered}\marg{A}\marg{B} command executes
\meta{A} if the number exists, and \meta{B} if not. So we use
it in the code which was finally chosen for the |section| level:
\begin{verbatim}
\etocsetstyle{section}
{\begin{enumerate}[leftmargin=.75cm, label=\etocifnumbered
      {{\fboxrule1pt\fcolorbox{green}{white}{\etocnumber}}}{}]}
{\normalsize\bfseries\rmfamily\item}
{\etocname (page \etocpage)}
{\end{enumerate}}

\etocsetstyle{subsection}
{\begin{enumerate}[leftmargin=0cm, label=\etocnumber]}
{\normalfont \item}
{\etocname (p.~\etocpage)}
{\end{enumerate}}
\end{verbatim}

If we had changed only the |section| level, and not the
|subsection| level, an error on compilation would have occurred
because the package style for subsections expects to start `in
vertical mode'. An additional \cs{par} token in the
\meta{contents} part of the |section| level would have fixed
this: |{...(page \etocpage)\par}|.


\subsection{The \csbhyp{etocfontxxx...} commands}

The package predefines for use in its default line styles:
\begin{verbatim}
\newcommand\etocfontminustwo{\normalfont \LARGE \bfseries}
\newcommand\etocfontminusone{\normalfont \large \bfseries}
\newcommand\etocfontzero{\normalfont \large \bfseries}
\newcommand\etocfontone{\normalfont \normalsize \bfseries}
\newcommand\etocfonttwo{\normalfont \normalsize}
\newcommand\etocfontthree{\normalfont \footnotesize}
\end{verbatim}
The same font is used for
the name, number, and page number, at each level (up to subsubsection).

In document classes where there is a \cs{chapter} command, the
default font for the level 1 (`section') does not use the bold
series. 

These font commands only impact the package default line
styles and do nothing under the \cs{etocstandardlines} regime.
And, let us recall, levels four (paragraph) and five
(subparagraphs) are actually not displayed by the package
default line styles.



\color{red!50}
\addtocontents{toc}{\string\color{red!50}}

\subsubsection{This is a (pale) red subsubsection for illustrative purposes}

Just before this subsubsection we inserted in the |.tex| file:
\begin{verbatim}
\color{red!50}
\addtocontents{toc}{\string\color{red!50}}
\end{verbatim}
This entire subsubsection and its title is printed in red, as
well as the title of the next section (we cancel the color
change only after it). But what about its entry in the table
of contents? well it is red in the main table of contents at
the beginning of this document, which uses the package line
styles, and it is not red in the table of contents at the
beginning of this second document part. The reason is that the
\meta{finish} code for the subsubsection level closed a group,
whereas the \meta{start} and \meta{finish} code of the package
line styles do not contain group opening and closing
instructions. So here the group was closed while reading the
|.toc| file and this explains why the next entry in the local
TOC was not typeset in red.

In the present case the group at the subsubsection level is
there to confine the font changes, but this could be made
superfluous via adding a \cs{normalsize} to the
|subsection| specification. I take this opportunity to mention
that \etoc always typesets the entire table of contents inside
a group, hence no font change therein can propagate to the outside.


\section{Am I also red?}

Well, actually no in this |scrartcl| class but I assure you I was in the
|article| class!

\begin{verbatim}
\normalcolor
\addtocontents{toc}{\string\normalcolor}
\end{verbatim}

\normalcolor
\addtocontents{toc}{\string\normalcolor}

Back to black. Note that this scope problem arises in real
life in a multi-lingual document, as the |babel| package
writes to the |.toc| file the language changes occurring in
the document. There is no easy general solution\footnote{one
  can insert by hand the language changes in each concerned
  sectioning command.} to this, but it will probably 
concern only a minority of cases. Again, the line styles
defined by \etoc do not use groups.

\section*{An unnumbered section}
\addcontentsline{toc}{section}{An unnumbered section}

I don't have much to say here. I just added an unnumbered
section, and made it appear in the |.toc| file.

\begin{verbatim}
\section*{An unnumbered section}
\addcontentsline{toc}{section}{An unnumbered section}
\end{verbatim}



\section{A comparison}

The \hyperref[toc:main]{main table of contents} was done in
the \etoc style. Let us issue here again two \toc commands,
one with the document class style (with some special
centered title in place of the standard Contents heading) and
the other one with the style we defined in the previous part
on the basis of enumerate environments.

\begin{verbatim}
\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{4}
\end{verbatim}

\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{4}

Let us  use the standard formatting of entries, as is
provided by the document class, and a two-column layout:
\begin{verbatim}
\etocstandardlines
\etocruledstyle{% 2 is the default number of columns
    \renewcommand{\etoctoprule}{\hrule height0pt}%
    \normalfont\normalsize\rmfamily\itshape 
    \parbox{.8\linewidth}{\centering 
  This is the global table of contents with standard
   (to the {\ttfamily scrartcl} class) entries 
  (and on two columns). Contrarily to
  \hyperref[toc:test]{\color{niceone}the next TOC}
  it contains paragraph entries.}}
\tableofcontents
\end{verbatim}

\etocstandardlines

\etocruledstyle{%
\renewcommand{\etoctoprule}{\hrule height0pt}%
\normalfont\normalsize\rmfamily\itshape 
\parbox{.8\linewidth}{\centering 
  This is the global table of contents with standard
   (to the {\ttfamily scrartcl} class) entries 
  (and on two columns). Contrarily to
  \hyperref[toc:test]{\color{niceone}the next TOC} it
  contains paragraph entries.}}

\tableofcontents

We then switch to our set-up from the previous part and add to it:
\begin{verbatim}
\etocsetstyle{part}
{\begin{enumerate}[label=\etocnumber,leftmargin=1.5em]}
{\etocfontminusone\item}
{\etocname}
{\end{enumerate}}
\tableofcontents \label{toc:test} \ref{toc:main}
\end{verbatim}


\etocruledstyle[2]{\normalfont\normalsize\rmfamily\itshape
  \fbox{\parbox{.8\linewidth}{This is the global table of
      contents using quickly defined (and easily improvable)
      enumerate environments for part, section and subsection,
      and an ad hoc in-line italic style for subsubsection.}}}

\etocsetstyle{part}
{\begin{enumerate}[label=\etocnumber,leftmargin=1.5em]}
{\etocfontminusone\item}
{\etocname}
{\end{enumerate}}

\tableofcontents \label{toc:test} \ref{toc:main}

\setcounter{tocdepth}{3}

\part{Package commands for global styles}
\label{part:globalcmds}

\begin{verbatim}
\setcounter{tocdepth}{-3}
\localtableofcontents \label{toc:globalcmds}
\setcounter{tocdepth}{3}
\end{verbatim}

\setcounter{tocdepth}{-3}
\localtableofcontents \label{toc:globalcmds}
\setcounter{tocdepth}{3}

\section{Specifying the toc display style}


The \emph{toc display} style says whether the TOC appears with
multiple columns or just one, whether the title is typeset as
in the |article| or |book| class, or should be centered above
the entries, with rules on its sides, or if the entire TOC
should be put in a frame. For example, to opt for a ruled
heading and single column layout, one issues commands of the
following type:
\begin{verbatim}
\etocruledstyle[1]{Title}
    \tableofcontents (or \localtableofcontents)
shortcuts:
    \etocruled[1]{Title}  (or  \etoclocalruled[1]{Title})
\end{verbatim}

\subsection{The command \csbhyp{etocruledstyle}}

The general format of \cs{etocruledstyle} is:\\
\centerline{\color{blue}\cs{etocruledstyle}%
\oarg{number of columns}\marg{title of the toc}}
\noindent Note that the title is horizontal material, if it
does not fit on one line it should be put in a \cs{parbox} of
a given width. We did this and even enclosed the parboxes in
\cs{fbox}es to get frames around them. For the example with
the standard formatting we did not use an \cs{fbox} and got
rid of the horizontal rules via:
\begin{verbatim}
\renewcommand{\etoctoprule}{\hrule height 0pt}
\end{verbatim}
The green frame for the heading of the table of contents at
the \hyperref[toc:a]{%
  \color{niceone} start of the second part of this
  document} was obtained with:
\begin{verbatim}
\etocruledstyle[1]{\etocfontminusone\color{green}
     \fboxrule1pt\fboxsep1ex
     \framebox[\linewidth]
              {\normalcolor\hss Contents of this second part\hss}}
\end{verbatim}


\subsection{The command \csbhyp{etocmulticolstyle}}

This is also a command with one optional and one mandatory argument:\\
\centerline{\color{blue}\cs{etocmulticolstyle}%
\oarg{number\_of\_columns}\marg{heading}}
The \meta{number\_of\_columns} can go from 1 to 10 (it
defaults to 2, and from 2 on is passed to a |multicols|
environment). The \meta{heading} should be some `vertical' material like:
\begin{center}
  \meta{heading} = {\ttfamily\string\section\lowast}\marg{title}
\end{center}
After such a command, future \cs{tableofcontents} will use the
specified style. A shortcut for just one table of contents and
not affecting the styles of later TOCs is:\\
\centerline{\cs{etocmulticol}\oarg{number\_of\_columns}\marg{heading}}
And there is also
\cs{etoclocalmulticol}\oarg{number\_of\_columns}\marg{heading}.


\subsubsection{The command \csbhyp{etoctocstyle}}

\centerline{\color{blue}\cs{etoctocstyle}\oarg{kind}%
\marg{number\_of\_columns}\marg{title}}
\hbox to \linewidth{\hss=
\cs{etocmulticolstyle}|[|{\itshape number\_of\_columns}|]|%
|{\kind*{|\itshape title\upshape|}}|\hss}
where |kind| is one of |chapter|, |section|, . . . and defaults
to |chapter| or |section| depending on the document class.

\paragraph{\texorpdfstring{\csb{etoc\-toc\-style\-with\-marks}}{etoctocstylewithmarks}}%
\leavevmode\unskip{\color{blue}\oarg{kind}\marg{number\_of\_columns}\marg{title}\marg{mark}}\\
\hbox to \linewidth
{\hss=\cs{etocmulticolstyle}|[|{\itshape number\_of\_columns}|]|%
|{\kind*{|\itshape title \ttfamily\upshape\string\markboth%
|{\MakeUppercase{|{\rmfamily\itshape mark}|}}}}|\hss} 
where |kind| is one of |chapter|, |section|, ... The
actual display of the marks depends on the settings of the
page style. There is variant \csb{etoctocstylewithmarksnouc}
which does not uppercase.

\subsection{A point of some importance}

There is an important difference between the
\cs{etocmulticolstyle} and \cs{etocruledstyle} commands. The
mandatory argument of the former can not be just some
\emph{naked text}. One can use things such as\\
\hbox to \linewidth{\hss \starit{section}|{|\emph{some not so naked text}|}|\hss} 
but \meta{heading}=\emph{naked text} gives an error:\\
\hbox to \linewidth{\hss 
|ERROR: LaTeX Error: Something's wrong--perhaps a missing \item.|\hss}
In the special case where the command was
also given the optional argument |[1]|,
it is possible to feed it with `|naked text\par|'.


On the other hand, it is exactly the \emph{opposite} for
\cs{etocruledstyle} (and \cs{etocframedstyle}) which expects
things which fit inside a horizontal box, hence it will gladly
accepts a \emph{naked text} as argument, but will choke on
\starit{section} or things ending with a \cs{par}.

\paragraph{Do we really want paragraph entries in the TOC?}

\paragraph{really?}

\subsection{The command  \csbhyp{etocruled}}

As a shortcut to set the style and issue a
\cs{tableofcontents}, all inside a group
so that  future table of contents will not be
affected, there is:\\
\centerline{\csb{etocruled}\oarg{number\_of\_columns}\marg{title}}
And the local form will be \csb{etoclocalruled}.

\subsection{The commands \csbhyp{etocframedstyle} and \csbhyp{etocframed}}

Same mechanism:\\
\centerline{\color{blue}\cs{etocframedstyle}%
  \oarg{number\_of\_columns}\marg{title}}
and the accompanying shortcut:\\
\centerline{\csb{etocframed}\oarg{number\_of\_columns}\marg{title}}
Here the entire table of contents is framed, hence this can
only work if it fits on a page. Note that the title itself is
not framed, if one wants a frame one should set it up
inside the \meta{title} argument to \cs{etocframedstyle} or
\cs{etocframed}. There is also \cs{etoc\-localframedstyle} and
\cs{etoclocalframed}.

\subsection{The command \csbhyp{etocsettocstyle}}

This is a command with two mandatory arguments:\\
\centerline{\color{blue}\cs{etocsettocstyle}\marg{before\_toc}%
\marg{after\_toc}}
The \marg{before\_toc} part is responsible for typesetting the
heading, for example it can be something like
\starit{section}|{\contentsname}|. It can also contain
instructions to mark the page headings.  Or
it could check (book class) to see if two-column mode is on,
and switch to one-column style, and the \meta{after\_toc} part
would then reenact the two-column mode.

The previously described commands \cs{etocmulticolstyle},
\cs{etocruledstyle}, and \cs{etocframedstyle} actually call
\cs{etocsettocstyle} as a lower-level routine.

\subsection{The compatibility mode \csbhyp{etocstandarddisplaystyle}}

\etoc will then emulate what the document class would have
done regarding the global display style of the table of
contents, in its absence. All customizing from inside the
class should be obeyed, too.

\section{The starred variants of the \csbhyp{tableofcontents}
  etc... commands}

The \toc, \localtoc, \cs{etocmulticol}, etc... have starred
variants (the star must be before the other arguments). For
all but the |memoir| class, they are like the original. For
the |memoir| class, the original prints an entry in the |.toc|
file, as is the usage for the original \toc command in that
class, whereas the starred variants do not, as is the habit in
that class.

As soon as one starts using local table of contents one
discovers that the default |memoir| thing which is to create a
|chapter| entry for each TOC is not convenient. The command
\csb{etocmemoirtoctotocfmt}\marg{kind}\marg{name} will change
the format (\meta{kind} is |chapter|, |section|, |subsection|... and
\meta{name} can be for example \cs{contentsname}.) The initial set-up
is with |chapter| and |\contentsname|. 

The format of the actual heading of the TOC should also be set
appropriately (for example with \cs{etoctocstyle}), to use the
identical division unit as in the first argument to
\cs{etocmemoirtoctotocfmt}. 

A weird situation arises when one has two successive
\cs{localtableofcontents} (obviously this is not a truly real
life situation), just after a \cs{part} for example. The first
one creates (if the default has not been modified as indicated
above) a Chapter heading which is
written to the |.toc|. Then the second one thinks to be local
to this chapter . . . and as a result it displays nothing. The
fix is to define the second one to be a clone of the first
one.

Independently of the situation with the |memoir| class there is
generally speaking a hook macro called
\csb{etocaftertitlehook} which is inhibited by using the
starred variants of the displaying commands. Except for the
|memoir| class, this hook is initially defined to do nothing.

\section{Table of contents for part \ref{part:globalcmds}}

As a third example we now print the local table of contents
for this part. We opt for a
``framed'' style, and specify some colors (including a
background color). We modify various things from the package
defaults. The original was invisibly defined with a label at the
beginning of this part \ref{part:globalcmds}.

\begingroup
\baselineskip10pt
\footnotesize
\begin{verbatim}
\etoctoclines
\begingroup % we use a group to limit the scope of the next commands
\renewcommand{\etoccolumnsep}{3em}
\renewcommand{\etocinnerleftsep}{2em}
\renewcommand{\etocinnerrightsep}{2em}
% specify a background color for the toc contents
\renewcommand{\etocbkgcolorcmd}{\color{yellow!10}}
%\renewcommand{\etocbkgcolorcmd}{\relax}
% set up the top and bottom rules 
\renewcommand{\etoctoprule}{\hrule height 1pt}
\renewcommand{\etoctoprulecolorcmd}{\color{red!25}}
\renewcommand{\etocbottomrule}{\hrule height 1pt}
\renewcommand{\etocbottomrulecolorcmd}{\color{red!25}}
% set up the left and right rules
\renewcommand{\etocleftrule}{\vrule width 5pt}
\renewcommand{\etocrightrule}{\vrule width 5pt}
\renewcommand{\etocleftrulecolorcmd}{\color{red!25}}
\renewcommand{\etocrightrulecolorcmd}{\color{red!25}}
% use \fcolorbox to set up a colored frame for the title
\fboxrule1pt
\etocframedstyle{\normalsize\rmfamily\itshape
  \fcolorbox{red}{white}{\parbox{.8\linewidth}{\centering
      This is a table of contents \`a la \etoc, but just for
      the sections and subsections in this part. As it is put
      in a frame, it has to be small enough to fit on the 
      current page. It has the label |toc:b|.}}}
% set up a label for future (or earlier...) reference
\tableofcontents \label{toc:b} \ref{toc:globalcmds}
\endgroup
\end{verbatim}
\endgroup

\etoctoclines
\begingroup 
\renewcommand{\etoccolumnsep}{3em}
\renewcommand{\etocinnerleftsep}{2em}
\renewcommand{\etocinnerrightsep}{2em}
% specify a background color for the toc contents
\renewcommand{\etocbkgcolorcmd}{\color{yellow!10}}
% set up the top and bottom rules 
\renewcommand{\etoctoprule}{\hrule height 1pt}
\renewcommand{\etoctoprulecolorcmd}{\color{red!25}}
\renewcommand{\etocbottomrule}{\hrule height 1pt}
\renewcommand{\etocbottomrulecolorcmd}{\color{red!25}}
% set up the left and right rules
\renewcommand{\etocleftrule}{\vrule width 5pt}
\renewcommand{\etocrightrule}{\vrule width 5pt}
\renewcommand{\etocleftrulecolorcmd}{\color{red!25}}
\renewcommand{\etocrightrulecolorcmd}{\color{red!25}}
% use \fcolorbox to set up a colored frame for the title
\fboxrule1pt
\etocframedstyle{\normalsize\rmfamily\itshape
  \fcolorbox{red}{white}{\parbox{.8\linewidth}{\centering
      This is a table of contents \`a la \etoc, but just for
      the sections and subsections in this part. As it is put
      in a frame, it has to be small enough to fit on the 
      current page. It has the label |toc:b|.}}}
% set up a label for future (or earlier...) reference
\tableofcontents \label{toc:b} \ref{toc:globalcmds}
\endgroup

We can also display it in the document standard manner:\footnote{the
present document uses the |scrartcl| class, and we check here that
the \etoc compatibility mode does respect the customizing done via the
class commands.} 
\begin{verbatim}
\KOMAoptions{toc=left}
\etocstandarddisplaystyle % necessary for the display to obey toc=left
\etocstandardlines
\tableofcontents \ref{toc:globalcmds}
\end{verbatim}
\KOMAoptions{toc=left}
\etocstandarddisplaystyle
\etocstandardlines
\tableofcontents \ref{toc:globalcmds}

\part{Using and customizing \etoc}
\label{part:custom}


\etoctoclines
\renewcommand{\etoctoprule}{\hrule height2pt depth0pt}
\renewcommand{\etoctoprulecolorcmd}{\color{red}}

\etocruledstyle{\normalfont\normalsize\rmfamily\fboxrule1pt\color{red}%
  \fbox{\parbox{.8\linewidth}{\centering\normalcolor This is a table of
      contents for the sections and
      subsections in this part. It carries the label |toc:c|}}} 

\localtableofcontents* \label{toc:c}


\section{Summary of the main style commands}

\subsection{Setting up local styles}

\hbox{\color{green}\fboxrule1pt\fboxsep1em
\setbox0\hbox{\cs{etocname}, \cs{etocnumber}, \cs{etocpage},
\cs{etocifnumbered}\marg{A}\marg{B}}%
\framebox[\linewidth][c]
{\vbox{\hsize\wd0\normalcolor\noindent
\cs{etocsetstyle}\marg{levelname}%
  \marg{start}\marg{prefix}\marg{contents}\marg{finish}\\
\cs{etocname}, \cs{etocnumber}, \cs{etocpage},
\cs{etocifnumbered}\marg{A}\marg{B}%
}}}

\subsection{Setting up global styles}

\medskip
\hbox{\color{green}\fboxrule1pt\fboxsep1em
\setbox0\hbox{\cs{etoctocstylewithmarksnouc}\oarg{kind}%
\marg{number\_of\_columns}\marg{title}\marg{mark}}%
\framebox[\linewidth][c]
{\vbox{\hsize\wd0
\normalcolor\noindent
\cs{etocmulticolstyle}\oarg{number\_of\_columns}\marg{heading}\\
\cs{etoctocstyle}\oarg{kind}\marg{number\_of\_columns}\marg{title}\\
\cs{etoctocstylewithmarks}\oarg{kind}\marg{number\_of\_columns}%
\marg{title}\marg{mark}\\
\cs{etoctocstylewithmarksnouc}\oarg{kind}\marg{number\_of\_columns}%
\marg{title}\marg{mark}\\
\cs{etocruledstyle}\oarg{number\_of\_columns}\marg{title}\\
\cs{etocframedstyle}\oarg{number\_of\_columns}\marg{title}\\
\cs{etocsettocstyle}\marg{before\_toc}\marg{after\_toc}}}}

\subsection{Displaying Tables of Contents}


\medskip \hbox{\color{green}\fboxrule1pt\fboxsep1em
  \setbox0\hbox{\cs{etocname}, \cs{etocnumber},
    \cs{etocpage}, \cs{etocifnumbered}\marg{A}\marg{B}}%
  \framebox[\linewidth][c]
  {\vbox{\hsize\wd0\normalcolor\noindent
      \toc\\
      \localtoc\\
      \cs{etocmulticol}\oarg{number\_of\_columns}\marg{heading}\\
      \cs{etoclocalmulticol}\oarg{number\_of\_columns}\marg{heading}\\
      \cs{etocruled}\oarg{number\_of\_columns}\marg{title}\\
      \cs{etoclocalruled}\oarg{number\_of\_columns}\marg{title}\\
      \cs{etocframed}\oarg{number\_of\_columns}\marg{title}\\
      \cs{etoclocalframed}\oarg{number\_of\_columns}\marg{title}\\
      \hbox{}{\itshape\ttfamily\ \ \ \  and their starred variants}
   }}}

\subsection{Labels and references}

The commands (starred or not) to actually display the table of contents
can be followed with optional labels or references:\par

\medskip \hbox{\color{green}\fboxrule1pt\fboxsep1em
  \setbox0\hbox{\cs{etocname}, \cs{etocnumber},
    \cs{etocpage}, \cs{etocifnumbered}\marg{A}\marg{B}}%
  \framebox[\linewidth][c]
  {\vbox{\hsize\wd0\normalcolor\noindent
      \toc \cs{label}|\{toc:here\}|\\ 
      \toc \cs{ref}|\{toc:far\}| \\
      \toc \cs{label}|\{toc:here\}| \cs{ref}|\{toc:far\}| \\
      \localtoc \cs{label}|\{toc:here\}|\\
      \hbox{}{\itshape\ttfamily\ \ \ \ similarly with\ }%
      \cs{etocmulticol}{\itshape\ttfamily\ etc . . . }
   }}}


\medskip The commands for local tables of
contents do \emph{not} react to a \cs{ref} following them. 

When
re-displaying another toc, only its contents are transferred:
both the line styles and the global style are the ones
currently defined, not the ones from the cloned toc.

\subsection{An example}


I got motivated by a
question\footnote{\href{http://tex.stackexchange.com/questions/83184/how-to-change-style-and-color-of-table-of-content}{tex.stackexchange.com/questions/83184}}
I saw on the \TeX{} StackExchange site. I copied the color RGB
specifications from an answer which had been provided to the
question. The \cs{etocframedstyle} puts the title on the top
rule in a centered position. This is not very convenient for
this example so we included the title as part of the
\meta{start} code at section level, to get it \emph{inside}
the frame.

\begingroup
\baselineskip10pt
\footnotesize
\begin{verbatim}
\setcounter{tocdepth}{3}
\begingroup
\definecolor{subsecnum}{RGB}{13,151,225}
\definecolor{secbackground}{RGB}{0,177,235}
\definecolor{tocbackground}{RGB}{212,237,252}

\renewcommand{\etocbkgcolorcmd}{\color{tocbackground}}
\renewcommand{\etocleftrulecolorcmd}{\color{tocbackground}}
\renewcommand{\etocrightrulecolorcmd}{\color{tocbackground}}
\renewcommand{\etocbottomrulecolorcmd}{\color{tocbackground}}
\renewcommand{\etoctoprulecolorcmd}{\color{tocbackground}}

\renewcommand{\etocleftrule}{\vrule width 1cm}
\renewcommand{\etocrightrule}{\vrule width .5cm}
\renewcommand{\etocbottomrule}{\hrule height 12pt}
\renewcommand{\etoctoprule}{\hrule height 12pt}

\renewcommand{\etocinnertopsep}{0pt}
\renewcommand{\etocinnerbottomsep}{0pt}
\renewcommand{\etocinnerleftsep}{0pt}
\renewcommand{\etocinnerrightsep}{0pt}

\newcommand\shiftedwhiterule[2]{%
    \hbox to \linewidth{\color{white}%
    \hskip#1\leaders\vrule height1pt\hfil}\nointerlineskip
\vskip#2}

\etocsetstyle{subsubsection}{\etocskipfirstprefix}
{\shiftedwhiterule{\leftskip}{6pt}}
{\sffamily\footnotesize
\leftskip2.5cm\hangindent1cm\rightskip1cm\noindent
\hbox to 1cm{\color{subsecnum}\etocnumber\hss}%
\color{black}\etocname\leaders\hbox to .2cm{\hss.}\hfill
\rlap{\hbox to 1cm{\hss\etocpage\hskip.2cm}}\par
\nointerlineskip\vskip3pt}
{}

\etocsetstyle{subsection}{\etocskipfirstprefix}
{\shiftedwhiterule{1.5cm}{6pt}}
{\sffamily\small
\leftskip1.5cm\hangindent1cm\rightskip1cm\noindent
\hbox to 1cm{\color{subsecnum}\etocnumber\hss}%
\color{black}\etocname\leaders\hbox to .2cm{\hss.}\hfill
\rlap{\hbox to 1cm{\hss\etocpage\hskip.2cm}}\par
\nointerlineskip\vskip6pt}
{}

\newcommand{\coloredstuff}[2]{%
            \leftskip0pt\rightskip0pt\parskip0pt
            \fboxsep0pt % \colorbox uses \fboxsep also when no frame!
       \noindent\colorbox{secbackground}
               {\parbox{\linewidth}{%
                    \vskip5pt
                    {\noindent\color{#1}#2\par\nointerlineskip}
                    \vskip3pt}}%
       \par\nointerlineskip}

\etocsetstyle{section}{\coloredstuff{white}
     {\hfil \hyperref[toc:b]{\bfseries\large I am a twin of  
     that other TOC (click me!)}\hfil}}
{\vskip3pt\sffamily\small}
{\coloredstuff{white}{\hbox to 1.5cm{\hss\etocnumber\hskip.2cm}%
    \etocname\hfill
\hbox{\etocpage\hskip.2cm}}%
 \vskip6pt}
{}

\etocframedstyle[1]{}
\tableofcontents \label{toc:clone} \ref{toc:globalcmds}
\endgroup
\end{verbatim}
\endgroup


The coding is a bit involved as it does not use any additional
package. A better solution would be to use some package to set
up a background color possibly extending accross pages, as the
framed style (which we used to get this background
color) can only deal with material short enough to fit on one
page.

Regarding colors, generally speaking all color commands inside
\etoc are initially defined to do nothing, and the choice to
use or not colors is left to the user.

\setcounter{tocdepth}{3}

\begingroup
\definecolor{subsecnum}{RGB}{13,151,225}
\definecolor{secbackground}{RGB}{0,177,235}
\definecolor{tocbackground}{RGB}{212,237,252}

\renewcommand{\etocbkgcolorcmd}{\color{tocbackground}}
\renewcommand{\etocleftrulecolorcmd}{\color{tocbackground}}
\renewcommand{\etocrightrulecolorcmd}{\color{tocbackground}}
\renewcommand{\etocbottomrulecolorcmd}{\color{tocbackground}}
\renewcommand{\etoctoprulecolorcmd}{\color{tocbackground}}

\renewcommand{\etocleftrule}{\vrule width 1cm}
\renewcommand{\etocrightrule}{\vrule width .5cm}
\renewcommand{\etocbottomrule}{\hrule height 12pt}
\renewcommand{\etoctoprule}{\hrule height 12pt}

\renewcommand{\etocinnertopsep}{0pt}
\renewcommand{\etocinnerbottomsep}{0pt}
\renewcommand{\etocinnerleftsep}{0pt}
\renewcommand{\etocinnerrightsep}{0pt}

\newcommand\shiftedwhiterule[2]{%
    \hbox to \linewidth{\color{white}%
    \hskip#1\leaders\vrule height1pt\hfil}\nointerlineskip
\vskip#2}

\etocsetstyle{subsubsection}{\etocskipfirstprefix}
{\shiftedwhiterule{\leftskip}{6pt}}
{\sffamily\footnotesize
\leftskip2.5cm\hangindent1cm\rightskip1cm\noindent
\hbox to 1cm{\color{subsecnum}\etocnumber\hss}%
\color{black}\etocname\leaders\hbox to .2cm{\hss.}\hfill
\rlap{\hbox to 1cm{\hss\etocpage\hskip.2cm}}\par
\nointerlineskip\vskip3pt}
{}

\etocsetstyle{subsection}{\etocskipfirstprefix}
{\shiftedwhiterule{1.5cm}{6pt}}
{\sffamily\small
\leftskip1.5cm\hangindent1cm\rightskip1cm\noindent
\hbox to 1cm{\color{subsecnum}\etocnumber\hss}%
\color{black}\etocname\leaders\hbox to .2cm{\hss.}\hfill
\rlap{\hbox to 1cm{\hss\etocpage\hskip.2cm}}\par
\nointerlineskip\vskip6pt}
{}

\newcommand{\coloredstuff}[2]{%
            \leftskip0pt\rightskip0pt\parskip0pt
            \fboxsep0pt % \colorbox uses \fboxsep also when no frame!
       \noindent\colorbox{secbackground}
               {\parbox{\linewidth}{%
                    \vskip5pt
                    {\noindent\color{#1}#2\par\nointerlineskip}
                    \vskip3pt}}%
       \par\nointerlineskip}

\etocsetstyle{section}{\coloredstuff{white}
     {\hfil \hyperref[toc:b]{\bfseries\large I am a twin of  
     that other TOC (click me!)}\hfil}}
{\vskip3pt\sffamily\small}
{\coloredstuff{white}{\hbox to 1.5cm{\hss\etocnumber\hskip.2cm}%
    \etocname\hfill
\hbox{\etocpage\hskip.2cm}}%
 \vskip6pt}
{}

\etocframedstyle[1]{}

\tableofcontents \label{toc:clone} \ref{toc:globalcmds}

\endgroup


\section{Customizing \etoc}



\subsection{Customizing  the \etoc pre-defined line styles}


We will simply list the relevant commands as defined in the
package. Customizing them goes through suitable
\cs{renewcommand}s:

\begin{verbatim}
\newcommand\etocfontminustwo{\normalfont \LARGE \bfseries}
\newcommand\etocfontminusone{\normalfont \large \bfseries}
\newcommand\etocfontzero{\normalfont \large \bfseries}
\newcommand\etocfontone{\normalfont \normalsize \bfseries}
\newcommand\etocfonttwo{\normalfont \normalsize}
\newcommand\etocfontthree{\normalfont \footnotesize}

\newcommand\etocsepminustwo{4ex plus .5ex minus .5ex}
\newcommand\etocsepminusone{4ex plus .5ex minus .5ex}
\newcommand\etocsepzero{2.5ex plus .4ex minus .4ex}
\newcommand\etocsepone{1.5ex plus .3ex minus .3ex}
\newcommand\etocseptwo{1ex plus .15ex minus .15ex}
\newcommand\etocsepthree{.25ex plus .05ex minus .05ex}

\newcommand\etocminustwoleftmargin{1.5em plus 0.5fil}
\newcommand\etocminustworightmargin{1.5em plus -0.5fil}
\newcommand\etocminusoneleftmargin{1em}
\newcommand\etocminusonerightmargin{1em}

\newcommand\etocbaselinespreadminustwo{1}
\newcommand\etocbaselinespreadminusone{1}
\newcommand\etocbaselinespreadzero{1}
\newcommand\etocbaselinespreadone{1}
\newcommand\etocbaselinespreadtwo{1}
\newcommand\etocbaselinespreadthree{.9}

\newcommand\etoctoclineleaders
{\hbox{\normalfont\normalsize\hbox to 2ex {\hss.\hss}}}

\newcommand\etocabbrevpagename{p.~}
\newcommand\etocpartname{\partname}
\newcommand\etocbookname{Book}
\end{verbatim}

No customizing of the standard line styles is possible from
within \etoc. As already explained, when
\cs{etocstandardlines} has been issued, the package just makes
itself very discrete and acts only at the global level, and
the TOC entries are (hopefully) formatted as would have
happened in the absence of \etoc.\footnote{with the
  KOMA-script classes, we noticed that
  \cs{etocstandarddisplaystyle} was apparently needed for the
  KOMA options |toc=left| to be active at the level of the line entries.}  


The \cs{etocstandardlines} mechanism will work also with
sectioning commands made known to \etoc via \cs{etocsetlevel},
under the condition of course that these sectioning commands
are accompanied with all the relevant definitions for
typesetting toc entries in the \LaTeX{} default manner
(existence of the macros \cs{l@something} . . .).

Using the \cs{etoctoclines} (obviously) or even just one
\cs{etocsetstyle} command turns off the compatibility mode
(all of this obeys the scoping mecanism of \TeX/\LaTeX{} groups).

\subsection{Customizing the global styles}


Again we list the relevant macros, what they do should be
legible from their names. Note that \cs{renewcommand}'s and
not \cs{setlength}'s have to be used for what appear to be
lengths, and that color commands are not just color
specifications, they must include \cs{color}, and are canceled
by re-defining them to do \cs{relax}.
\begin{verbatim}
\newcommand\etocabovetocskip{3.5ex plus 1ex minus .2ex} 
\newcommand\etocbelowtocskip{3.5ex plus 1ex minus .2ex}

\newcommand\etoccolumnsep{2em}
\newcommand\etocmulticolsep{0ex}
\newcommand\etocmulticolpretolerance{-1}
\newcommand\etocmulticoltolerance{200}
\newcommand\etocdefaultnbcol{2}
\newcommand\etocinnertopsep{2ex}
\newcommand\etoctoprule{\hrule}
\newcommand\etoctoprulecolorcmd{\relax}

% for the framed style only:
\newcommand\etocinnerleftsep{2em}
\newcommand\etocinnerrightsep{2em}
\newcommand\etocinnerbottomsep{3.5ex}

\newcommand\etocleftrule{\vrule}
\newcommand\etocrightrule{\vrule}
\newcommand\etocbottomrule{\hrule}
\newcommand\etocleftrulecolorcmd{\relax}
\newcommand\etocrightrulecolorcmd{\relax}
\newcommand\etocbottomrulecolorcmd{\relax}

\newcommand\etocbkgcolorcmd{\relax}

% hooks
\newcommand\etocframedmphook{\relax}
\end{verbatim}

The \cs{etocframedmphook} is positioned immediately
after the beginning of a minipage environment where the
contents of the framed TOC are typeset. 

The \cs{...colorcmd} things are initially set to be
\cs{relax}, so there is no need to do \cs{usepackage}|{color}|
if the document does not use colors. If the scope of a change
to a color command such as \cs{etocbkgcolorcmd} has not
been limited to a group and one then wishes to let it again be
\cs{relax} one must use a \cs{renewcommand} and not
\cs{let}\cs{etocbkgcolorcmd}\cs{relax}.

Regarding the dimensions of the top rule they can be specified
in |ex|'s or |em|'s as in this example:\\
\hbox to \linewidth{\hss 
|\renewcommand{\etoctoprule}{\hrule height 1ex}|\hss} 
The package code is done in such a manner
that it is the font size in instance at the end of typesetting
the title argument to \cs{etocruledtoc} or
\cs{etocframedtoc} which will be used for the meaning of the
`1ex'. Of course also the other rule commands can have their
dimensions in font relative units, but their values are
decided on the basis of the font in effect just before the
table of contents.

The top and bottom rules do not have to be rules and can be
horizontal \emph{leaders} (of a specified height) in the general
\TeX{} sense. However the left and right rules are not
used as (horizontal) leaders but as objects of a given specified
width. Note that \emph{only} the Plain \TeX{} syntax for rules is
accepted here. 

\part{Tips}

\section{... and tricks}

\subsection{Hacking  framed parboxes}


\begin{verbatim}
\renewcommand\etoctoprule{\hrule height 2pt depth 2pt}
\etocruled{\color{green}\fboxrule2pt\fboxsep1ex
               \fbox{\raisebox{-\fontdimen22\textfont2}
                    {\color{blue}\parbox{.5\linewidth}
                       {\normalfont This text is perfectly centered
                        vertically with respect to the
                        surrounding horizontal rules.}}}}
\ref{toc:globalcmds}
\end{verbatim}

\renewcommand\etoctoprule{\hrule height 2pt depth 2pt}

\etocruled{\color{green}\fboxrule2pt\fboxsep1ex
              \fbox{\raisebox{-\fontdimen22\textfont2}
                    {\color{blue}\parbox{.5\linewidth}
                       {\normalfont This text is perfectly centered
                        vertically with respect to the
                        surrounding horizontal rules.}}}}
              \ref{toc:globalcmds}


\subsection{Interverting the levels}
\label{subsec:interverting}

Let us display and count all subsections occurring in this document (see
section \ref{sec:surprising} for other uses of this technique): 
\begin{verbatim}
\setcounter{tocdepth}{2}
\etocsetlevel{part}{3}
\etocsetlevel{section}{3}
\etocsetstyle{subsection}{\begin{enumerate}[itemsep=0pt,%
label=,leftmargin=0pt]}
{\normalfont\bfseries\item}
{\roman{enumi}. \normalfont\etocname (\etocnumber, p.~\etocpage)}
{\end{enumerate}}
\renewcommand{\etoccolumnsep}{2.75em}
\renewcommand{\columnseprule}{1pt}
\etocmarkbothnouc{List of all subsections}
\etocmulticol[3]{\subsection{All subsections of this document}}
\setcounter{tocdepth}{3}
\end{verbatim}

\setcounter{tocdepth}{2}

\begingroup
\etoctoclines
\etocstandarddisplaystyle
\etocsetlevel{book}{3}
\etocsetlevel{part}{3}
\etocsetlevel{chapter}{3}
\etocsetlevel{section}{3}
\etocsetstyle{subsection}{%
\begin{enumerate}[itemsep=0pt,label=,leftmargin=0pt]}
{\normalfont\bfseries\item}
{\roman{enumi}. \normalfont\etocname (\etocnumber, p.~\etocpage)}
{\end{enumerate}}

\renewcommand{\etoccolumnsep}{2.75em}
\renewcommand{\columnseprule}{1pt}

\etocmarkbothnouc{List of all subsections}
\etocmulticol[3]{\subsection{All subsections of this document}}
\endgroup

\setcounter{tocdepth}{3}

\subsection{Compatibility with other packages}

\etoc uses the packages |multicol| and |xspace|. It is
|hyperref| aware and hopefully |hyperref| compatible! It
doesn't matter whether \etoc or |hyperref| is loaded first.

It seems that package |hyperref| takes into account the current
value of the |tocdepth| counter to decide whether to create a
bookmark for the next sectioning unit. So it is recommended if
this value is modified for the needs of displaying a table of
contents to immediately reset it to the global document value, as
this will decide the depth of the bookmarks added in the PDF
output (\emph{e.g.} if one displays inside a section a table of contents with
|tocdepth| set to zero, the next section will not make it to the
bookmarks if |tocdepth| is not reset to at least one). Also,
recall that \LaTeX{} counters are global: a
change to |tocdepth| in a group or environment does modify its
(global) value.

The macros \cs{etocname}, \cs{etocnumber}, and \cs{etocpage}
contain the |hyperref| links,\footnote{a future version of the
package will provide additionally macros without the
\texttt{hyperref} or \texttt{xspace} `decoration', as this can be
useful in certain circumstances.} if present (note that the
|linktoc=all| option of |hyperref| tells it to put a link also in
the page number corresponding to a given toc entry). The tables of
contents of the present document are fully linked.

The contents of the |.toc| file (if it exists) are read into
memory by \etoc once, at the time of \cs{usepackage\{etoc\}}.
The |.toc| file will be opened for write operations only at
the time of the first TOC displaying command. \footnote{up to
  version |v1.05| both things were done by \etoc at
  \cs{begin\{document\}}.} \footnote{As soon as the |.toc|
  file is opened for write, it becomes empty, so its contents
  should be read by other packages or user defined macros
  before the first TOC displaying command (this is the same
  situation in the standard classes with the standard \toc
  command.)}

When a \localtoc is inserted by the user in the document, a
line containing an \etoc inner command and an identification
number is added to the |.toc| file. The correct local table of
contents will be displayed only on the next |latex| run.

This is the only kind of data written by \etoc to the |.toc|
file, but of course it does mean that if you remove usage of
\etoc from the document, you should trash the |.toc| file.
The commands written by \etoc to the |.toc| file do nothing
when not used by \etoc itself, so other packages may
also read the |.toc| file and execute its
contents.\footnote{see previous footnote.}

If the document class does not use the extension |.toc| for
the file where the TOC data is preserved, then \etoc will
fail. If the data written in the |.toc| file does not contain
the name, number and page numbers in \cs{contentsline} lines,
then \etoc will fail. If the typesetting of entries of type
\emph{kind} is not done by a macro with name \cs{l@kind}, then
\etoc will fail. 

The macros \cs{l@section}, \cs{l@subsection} . . ., more
generally \cs{l@kind} where \emph{kind} has been declared to
\etoc with \cs{etocsetlevel} may be customized throughout the
document and it is their current definitions which will be
used in compatibility mode.\footnote{in versions up to
  |v1.05|, the default definitions of \cs{l@section} etc . .
  . were re-established (to their values at the
  \cs{begin\{document\}}, or at the latest \cs{etocsetlevel})
  only temporarily by \etoc at the time of displaying a TOC
  in compatibility mode. This made it impossible to customize
  them by trying to add for example some font changing prefix
  to the pre-existing macro (but customization via commands
  of packages like |tocloft| which re-define \cs{l@section}
  to have customizing hooks did work as advertised). The new
  implementation in |v1.06| changes the meaning of
  \cs{l@section} and similar macros only at the time of
  actually typesetting the tables of contents.}

\subsection{\TeX nical matters}

The \cs{etocname}, \cs{etocnumber}, \cs{etocpage} and
\cs{etocifnumbered} macros are protected against premature
expansion. They end with an \cs{xspace}. The style customizing
commands (local and global) such as \cs{etocsetstyle},
\cs{etocsetlevel}, \cs{etocsettocstyle},
\cs{etocmulticolstyle}, \cs{etocruledstyle},
\cs{etocframedstyle} do obey \LaTeX{}'s groups. All TOCs are
typeset inside groups.

When using multiple \toc commands in a document, one should
beware from adding typesetting instructions directly in the
|.toc| file, as they will be executed by \etoc for all TOCs:
even for a \localtoc it doesn't matter if that instruction
seems to concern material outside of its scope, it will get
executed nevertheless. If absolutely necessary these
instructions should be done in such a way that they can
be activated or deactivated easily from the document source,
as need be.

\subsection{Errors and catastrophes}

After using \cs{etocsetstyle} for one level, the remaining
uncustomized levels use the \etoc's default styles. One has to
make sure that all levels needed for the next table of
contents are mutually compatible.

Do not clone a clone but produce rather twins by cloning the
same original local table of contents.

As is usual with toc and labels, after each change, one has to
run latex a certain number of times to let the produced
document get its final appearance (at least twice). 

In certain extreme cases (\emph{e.g.} if one removes the
|hyperref| package from a previously hyperlinked document),
one must trash the previous |.aux| and |.toc| files. But
adding \cs{usepackage\{hyperref\}} to a non-hyperlinked
source should cause no trouble at all (the old |.toc| file
will not be used, the new one, whose format differs, will be
used on the next run).\footnote{Note though that an error
  results when one adds \cs{usepackage\{hyperref\}} to a
  document having some \cs{label}'s and \cs{ref}'s;
  one must run |latex| another time or trash the |.aux| file to
  get rid of the error. This is related neither with \etoc nor with the
|.toc| file.}


\part{The code}

This source file |etoc.dtx| produces 
|etoc.sty| when one does |latex etoc.dtx| or |pdflatex etoc.dtx| 
(an |etoc.ins| file is also produced, for
distributions expecting it for installation). Two more runs
are necessary to finish producing the documentation. The
|etoc.sty| file should be moved to a suitable location within
the \TeX{} installation.

\section{Implementation}

\makeatletter
\StopEventually{\check@checksum\end{document}}
\makeatother

Writing-up source code comments is hopefully for a future
release. 

% I don't want to have to type at this location (far from the top of the
% file) explicitly the package version or version date, as it is
% inconvenient to have to remember to do this when updating the package.
% Also, I prefer not to add macros to the |.sty| file giveing the package
% date, name, or version. So I cut out the following from the real macrocode
% environment (leaving out the \init@crossref.)
\makeatletter
\begingroup
\topsep\MacrocodeTopsep
\trivlist\parskip\z@\item[]
\macro@font
\leftskip\@totalleftmargin  \advance\leftskip\MacroIndent
\rightskip\z@  \parindent\z@  \parfillskip\@flushglue
\global\@newlistfalse \global\@minipagefalse
\ifcodeline@index
  \everypar{\global\advance\c@CodelineNo\@ne
  \llap{\theCodelineNo\ \hskip\@totalleftmargin}}%
\fi
\string\ProvidesPackage\string{\pkgname\string}\par
\noindent\space [\pkgdate\space\pkgversion\space\pkgdescription]\par
\nointerlineskip
\global\@inlabelfalse
\endtrivlist
\endgroup
\makeatother

% The catcode hackery next is to avoid to have <*package> to be listed
% in the commented source code...
% (c) 2012/11/19 jf burnol ;-)

\MakePercentIgnore

%
% \catcode`\<=0 \catcode`\>=11 \catcode`\*=11 \catcode`\/=11
% \let</none>\relax
% \def<*package>{\catcode`\<=12 \catcode`\>=12 \catcode`\*=12 \catcode`\/=12}
%
%</none>
%<*package>
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{multicol}
\RequirePackage{xspace}
\DeclareOption*{\PackageWarning{etoc}{Option `\CurrentOption' is unknown.}}
\ProcessOptions\relax
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newtoks\Etoc@toctoks
\newif\ifEtoc@jj  % book
\newif\ifEtoc@j   % part  
\newif\ifEtoc@    % chapter
\newif\ifEtoc@i   % section
\newif\ifEtoc@ii  % subsection
\newif\ifEtoc@iii % subsubsection
\newif\ifEtoc@iv  % paragraph
\newif\ifEtoc@v   % subparagraph
\newif\ifEtoc@number
\newif\ifEtoc@hyperref
\newif\ifEtoc@tocwithid
\newif\ifEtoc@standard
\newif\ifEtoc@part 
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newif\ifEtoc@localtoc
\newif\ifEtoc@skipthisone
\newif\ifEtoc@stoptoc
\newif\ifEtoc@notactive
\newcounter{etoc@tocid}
\newif\ifEtoc@mustclosegroup
\newif\ifEtoc@tocopenedforwrite
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\@ifclassloaded{memoir}{\def\Etoc@minf{-\thr@@}}{\def\Etoc@minf{-\tw@}}
\def\Etoc@@minustwo@@{-\tw@}
\let\Etoc@@minusone@@\m@ne
\chardef\Etoc@@zero@@\z@
\chardef\Etoc@@one@@\@ne
\chardef\Etoc@@two@@\tw@
\chardef\Etoc@@three@@\thr@@
\chardef\Etoc@@four@@ 4
\chardef\Etoc@@five@@ 5
\chardef\Etoc@@six@@ 6
\let\Etoc@localtop\Etoc@@minustwo@@
\def\Etoc@@minustwo@{minustwo}
\def\Etoc@@minusone@{minusone}
\def\Etoc@@zero@{zero}
\def\Etoc@@one@{one}
\def\Etoc@@two@{two}
\def\Etoc@@three@{three}
\def\Etoc@@four@{four}
\def\Etoc@@five@{five}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@levellist{}
\def\Etoc@newlevel#1{% 
    \def\Etoc@levellist@elt{\noexpand\Etoc@levellist@elt\noexpand}%
    \edef\Etoc@levellist{\Etoc@levellist\Etoc@levellist@elt#1}}
\def\etocsetlevel#1#2{%
 \let\Etoc@next\@firstofone
 \ifcase#2\or\or\or\or\or
       \else\ifnum#2=\m@ne\else\ifnum#2=-\tw@\else
    \PackageWarning{etoc}
    {unexpected value `#2' in \string\etocsetlevel.^^J%
    Should be -2,-1, 0, 1, 2, 3, 4 or 5. Set to -1}%
     \expandafter\def\csname Etoc@#1@\endcsname{minusone}%
     \expandafter\let\csname Etoc@#1@@\endcsname\m@ne
 \let\Etoc@next\@gobble\fi\fi\fi
 \expandafter\Etoc@newlevel\csname l@#1\endcsname
 \Etoc@next 
 {\ifcase#2\relax
      \expandafter\let \csname Etoc@#1@@\endcsname\Etoc@@zero@@
      \expandafter\def \csname Etoc@#1@\endcsname{zero}%
   \or
      \expandafter\let \csname Etoc@#1@@\endcsname\Etoc@@one@@
      \expandafter\def \csname Etoc@#1@\endcsname{one}%
   \or
      \expandafter\let \csname Etoc@#1@@\endcsname\Etoc@@two@@
      \expandafter\def \csname Etoc@#1@\endcsname{two}%
   \or
      \expandafter\let \csname Etoc@#1@@\endcsname\Etoc@@three@@
      \expandafter\def \csname Etoc@#1@\endcsname{three}%
   \or
      \expandafter\let \csname Etoc@#1@@\endcsname\Etoc@@four@@
      \expandafter\def \csname Etoc@#1@\endcsname{four}%
   \or
      \expandafter\let \csname Etoc@#1@@\endcsname\Etoc@@five@@
      \expandafter\def \csname Etoc@#1@\endcsname{five}%
   \else
      \ifnum#2=\m@ne
      \expandafter\let \csname Etoc@#1@@\endcsname\Etoc@@minusone@@
      \expandafter\def \csname Etoc@#1@\endcsname{minusone}%
      \else
      \expandafter\let \csname Etoc@#1@@\endcsname\Etoc@@minustwo@@
      \expandafter\def \csname Etoc@#1@\endcsname{minustwo}%
      \fi
   \fi}}
\etocsetlevel{book}{-2}
\etocsetlevel{part}{-1}
\etocsetlevel{chapter}{0}
\etocsetlevel{section}{1}
\etocsetlevel{subsection}{2}
\etocsetlevel{subsubsection}{3}
\etocsetlevel{paragraph}{4}
\etocsetlevel{subparagraph}{5}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\AtBeginDocument{%
\@ifpackageloaded{hyperref}
{\Etoc@hyperreftrue
\def\Etoc@et@hop#1#2#3#4#5{#1{#3}{#4}{#5}#2}}
{\Etoc@hyperreffalse
\def\Etoc@et@hop#1#2#3#4{#1{#3}{#4}#2}}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
  \def\Etoc@swa#1{%
        \Etoc@et@hop 
          {\Etoc@savedcontentsline{#1}}
          {\Etoc@prefix\Etoc@contents}}
  \def\Etoc@swb#1{%
        \Etoc@et@hop 
          {\Etoc@savedcontentsline{#1}}
          {\Etoc@contents}}
\let\etocskipfirstprefix\@thirdofthree 
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@setflags #1{%
  \ifcase #1\relax
      \global\Etoc@vfalse
      \global\Etoc@ivfalse
      \global\Etoc@iiifalse
      \global\Etoc@iifalse
      \global\Etoc@ifalse
      \global\Etoc@true
  \or
      \global\Etoc@vfalse
      \global\Etoc@ivfalse
      \global\Etoc@iiifalse
      \global\Etoc@iifalse
      \global\Etoc@itrue
  \or
      \global\Etoc@vfalse
      \global\Etoc@ivfalse
      \global\Etoc@iiifalse
      \global\Etoc@iitrue
  \or
      \global\Etoc@vfalse
      \global\Etoc@ivfalse
      \global\Etoc@iiitrue
  \or
      \global\Etoc@vfalse
      \global\Etoc@ivtrue
  \or
      \global\Etoc@vtrue
  \else
    \ifnum#1=\m@ne
      \global\Etoc@vfalse
      \global\Etoc@ivfalse
      \global\Etoc@iiifalse
      \global\Etoc@iifalse
      \global\Etoc@ifalse
      \global\Etoc@false
      \global\Etoc@jtrue
    \else
      \global\Etoc@vfalse
      \global\Etoc@ivfalse
      \global\Etoc@iiifalse
      \global\Etoc@iifalse
      \global\Etoc@ifalse
      \global\Etoc@false
      \global\Etoc@jfalse
      \global\Etoc@jjtrue
    \fi
  \fi}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@etoccontentsline#1{%
  \global\expandafter\let\expandafter\Etoc@tmp\csname Etoc@#1@@\endcsname
  \Etoc@skipthisonefalse
  \ifEtoc@hyperref
          \def\Etoc@next{\expandafter\@gobbletwo\@gobblefour}%
      \else
          \def\Etoc@next{\expandafter\@gobble\@gobblefour}%
  \fi
  \ifEtoc@localtoc
     \let\Etoc@prenext\relax
     \ifEtoc@stoptoc
        \Etoc@skipthisonetrue
     \fi
     \ifnum\Etoc@tmp<\Etoc@localtop 
        \def\Etoc@prenext{\global\Etoc@stoptoctrue}%
        \Etoc@skipthisonetrue
     \fi
     \ifEtoc@notactive
        \def\Etoc@prenext{\Etoc@setflags{\Etoc@tmp}}%
        \Etoc@skipthisonetrue
     \fi
     \Etoc@prenext
  \fi
  \ifnum\c@tocdepth<\Etoc@tmp\relax\else
  \ifEtoc@skipthisone\else
  \global\let\Etoc@next\relax  
  \ifcase\Etoc@tmp 
      \ifEtoc@v \Etoc@end@five\fi
      \ifEtoc@iv \Etoc@end@four\fi
      \ifEtoc@iii \Etoc@end@three\fi
      \ifEtoc@ii \Etoc@end@two\fi
      \ifEtoc@i \Etoc@end@one\fi
      \ifEtoc@ \else \def\Etoc@next{\Etoc@begin@zero}\fi
      \def\Etoc@contents{\Etoc@contents@zero}%
      \def\Etoc@prefix{\Etoc@prefix@zero}%
  \or
      \ifEtoc@v \Etoc@end@five\fi
      \ifEtoc@iv \Etoc@end@four\fi
      \ifEtoc@iii \Etoc@end@three\fi
      \ifEtoc@ii \Etoc@end@two\fi
      \ifEtoc@i \else \def\Etoc@next{\Etoc@begin@one}\fi
      \def\Etoc@contents{\Etoc@contents@one}%
      \def\Etoc@prefix{\Etoc@prefix@one}%
  \or
      \ifEtoc@v \Etoc@end@five\fi
      \ifEtoc@iv \Etoc@end@four\fi
      \ifEtoc@iii \Etoc@end@three\fi
      \ifEtoc@ii \else \def\Etoc@next{\Etoc@begin@two}\fi
      \def\Etoc@contents{\Etoc@contents@two}%
      \def\Etoc@prefix{\Etoc@prefix@two}%
  \or
      \ifEtoc@v \Etoc@end@five\fi
      \ifEtoc@iv \Etoc@end@four\fi
      \ifEtoc@iii \else \def\Etoc@next{\Etoc@begin@three}\fi
      \def\Etoc@contents{\Etoc@contents@three}%
      \def\Etoc@prefix{\Etoc@prefix@three}%
  \or 
      \ifEtoc@v \Etoc@end@five\fi 
      \ifEtoc@iv \else \def\Etoc@next{\Etoc@begin@four}\fi
      \def\Etoc@contents{\Etoc@contents@four}%
      \def\Etoc@prefix{\Etoc@prefix@four}%
  \or 
      \ifEtoc@v \else \def\Etoc@next{\Etoc@begin@five}\fi
      \def\Etoc@contents{\Etoc@contents@five}%
      \def\Etoc@prefix{\Etoc@prefix@five}%
  \else
    \ifnum\Etoc@tmp=\m@ne
      \ifEtoc@v \Etoc@end@five\fi
      \ifEtoc@iv \Etoc@end@four\fi
      \ifEtoc@iii \Etoc@end@three\fi
      \ifEtoc@ii \Etoc@end@two\fi
      \ifEtoc@i \Etoc@end@one\fi
      \ifEtoc@ \Etoc@end@zero\fi
      \ifEtoc@j \else \def\Etoc@next{\Etoc@begin@minusone}\fi
      \def\Etoc@contents{\Etoc@contents@minusone}%
      \def\Etoc@prefix{\Etoc@prefix@minusone}%
    \else
      \ifEtoc@v \Etoc@end@five\fi
      \ifEtoc@iv \Etoc@end@four\fi
      \ifEtoc@iii \Etoc@end@three\fi
      \ifEtoc@ii \Etoc@end@two\fi
      \ifEtoc@i \Etoc@end@one\fi
      \ifEtoc@ \Etoc@end@zero\fi
      \ifEtoc@j \Etoc@end@minusone\fi
      \ifEtoc@jj \else \def\Etoc@next{\Etoc@begin@minustwo}\fi
      \def\Etoc@contents{\Etoc@contents@minustwo}%
      \def\Etoc@prefix{\Etoc@prefix@minustwo}%
      \fi
  \fi
  \ifnum\Etoc@tmp=\m@ne\Etoc@parttrue\else\Etoc@partfalse\fi
  \Etoc@setflags{\Etoc@tmp}%
  \fi\fi
  \Etoc@next
  \@firstoftwo{\Etoc@swa{#1}}{\Etoc@swb{#1}}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@lxyz #1#2{\@namedef{etocname }{\leavevmode #1\xspace}%
                    \@namedef{etocpage }{\leavevmode #2\xspace}%
    \Etoc@getnb #1\relax\relax\etoc@
    \ifEtoc@number\else\ifEtoc@part 
    \Etoc@getit #1\hspace\relax\etoc@\fi\fi}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@getnb #1{\let\Etoc@next\Etoc@getnb@nohyp
\ifEtoc@hyperref\ifx #1\hyper@linkstart
    \let\Etoc@next\Etoc@getnb@hyp\fi\fi\Etoc@next #1}
\def\Etoc@getit #1{\let\Etoc@next\Etoc@getit@nohyp
\ifEtoc@hyperref\ifx #1\hyper@linkstart
    \let\Etoc@next\Etoc@getit@hyp\fi\fi\Etoc@next #1}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@getnb@nohyp #1#2#3\etoc@{%
\ifx #1\numberline
\@namedef{etocnumber }{\leavevmode #2\xspace}\Etoc@numbertrue
\else
\@namedef{etocnumber }{\leavevmode\xspace}\Etoc@numberfalse
\fi}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@getnb@hyp #1#2#3#4#5#6\etoc@{%
  \def\Etoc@getnbr ##1##2##3\etoc@{%
    \ifx ##1\numberline
       \@namedef{etocnumber }{\leavevmode #1{#2}{#3}{##2}#5\xspace}%
       \Etoc@numbertrue
    \else
       \@namedef{etocnumber }{\leavevmode\xspace}%
       \Etoc@numberfalse
    \fi}%
  \Etoc@getnbr #4\relax\relax\etoc@}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@getit@nohyp #1\hspace#2#3\etoc@{%
    \def\Etoc@getname ##1\hspace\relax\etoc@{%
       \@namedef{etocname }{\leavevmode ##1\xspace}}%
    \ifx \relax#2\else 
        \@namedef{etocnumber }{\leavevmode #1\xspace}%
        \Etoc@numbertrue
    \Etoc@getname #3\etoc@\fi}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@getit@hyp #1#2#3#4#5#6\etoc@{%
  \def\Etoc@getname ##1\hspace\relax\etoc@{%
       \@namedef{etocname }{\leavevmode #1{#2}{#3}{##1}#5\xspace}}%
  \def\Etoc@getnbr ##1\hspace##2##3\etoc@{%
   \ifx\relax##2\else
       \@namedef{etocnumber }{\leavevmode #1{#2}{#3}{##1}#5\xspace}%
       \Etoc@numbertrue
   \Etoc@getname ##3\etoc@\fi}%
  \Etoc@getnbr #4\hspace\relax\etoc@}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\edef\etocpage{\noexpand\protect\expandafter\noexpand
               \csname etocpage \endcsname}
\edef\etocname{\noexpand\protect\expandafter\noexpand
               \csname etocname \endcsname}
\edef\etocnumber{\noexpand\protect\expandafter\noexpand
                 \csname etocnumber \endcsname}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\@namedef{etocifnumbered }{\ifEtoc@number
\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\edef\etocifnumbered{\noexpand\protect\expandafter\noexpand
                     \csname etocifnumbered \endcsname}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@readtoc#1{%
  \ifeof #1
     \let\Etoc@nextread\@gobble
     \global\Etoc@toctoks=\expandafter{\the\Etoc@toctoks}%
  \else
     \let\Etoc@nextread\Etoc@readtoc
     \read #1 to \Etoc@buffer
     \Etoc@toctoks=\expandafter\expandafter\expandafter
       {\expandafter\the\expandafter\Etoc@toctoks\Etoc@buffer}%
  \fi
  \Etoc@nextread{#1}%
}
\IfFileExists{\jobname .toc}
    {{\endlinechar=-1 \makeatletter
        \newread\Etoc@tf
        \openin\Etoc@tf\@filef@und
           \Etoc@readtoc\Etoc@tf
        \closein\Etoc@tf}}{\typeout{No file \jobname .toc.}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@starttoc{%
\begingroup   
\makeatletter 
\ifEtoc@hyperref
    \ifx\hyper@last\@undefined
    \ltx@ifpackageloaded {parskip}{\parskip \z@ }{}%
    \IfFileExists {\jobname .toc}
    {\Hy@WarningNoLine 
    {old toc file detected, not used; run LaTeX again (cheers from etoc)}%
    \global\Etoc@toctoks={}}
{}\fi\fi
\if@filesw \newwrite \tf@toc 
\immediate \openout \tf@toc \jobname .toc\relax \fi \@nobreakfalse 
\endgroup}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@toctoc{%
   \global\Etoc@vfalse
   \global\Etoc@ivfalse
   \global\Etoc@iiifalse
   \global\Etoc@iifalse
   \global\Etoc@ifalse
   \global\Etoc@false
   \global\Etoc@jfalse
   \global\Etoc@jjfalse
\ifEtoc@standard
\etoc@setstyle{@minustwo}{}{}{}{}%
\etoc@setstyle{@minusone}{}{}{}{}%
\etoc@setstyle{@zero}{}{}{}{}%
\etoc@setstyle{@one}{}{}{}{}%
\etoc@setstyle{@two}{}{}{}{}%
\etoc@setstyle{@three}{}{}{}{}%
\etoc@setstyle{@four}{}{}{}{}%
\etoc@setstyle{@five}{}{}{}{}%
\else
\def\Etoc@levellist@elt##1{\let##1\Etoc@lxyz}%
\Etoc@levellist
\let\numberline\@gobble
\let\booknumberline\numberline
\let\partnumberline\numberline
\let\chapternumberline\numberline
\fi
\the\Etoc@toctoks
\ifEtoc@notactive\else 
  \ifEtoc@v   \Etoc@end@five\fi
  \ifEtoc@iv  \Etoc@end@four\fi
  \ifEtoc@iii \Etoc@end@three\fi
  \ifEtoc@ii  \Etoc@end@two\fi
  \ifEtoc@i   \Etoc@end@one\fi
  \ifEtoc@    \Etoc@end@zero\fi
  \ifEtoc@j   \Etoc@end@minusone\fi
  \ifEtoc@jj  \Etoc@end@minustwo\fi
\fi}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\etoc@@startlocaltoc#1#2{%
\let\Etoc@next\relax
    \ifEtoc@localtoc 
    \ifEtoc@notactive
        \ifnum #1=#2\relax
          \ifEtoc@jj  \let\Etoc@localtop\Etoc@@minusone@@ \fi
          \ifEtoc@j   \let\Etoc@localtop\Etoc@@zero@@  \fi
          \ifEtoc@    \let\Etoc@localtop\Etoc@@one@@   \fi
          \ifEtoc@i   \let\Etoc@localtop\Etoc@@two@@   \fi
          \ifEtoc@ii  \let\Etoc@localtop\Etoc@@three@@ \fi
          \ifEtoc@iii \let\Etoc@localtop\Etoc@@four@@  \fi
          \ifEtoc@iv  \let\Etoc@localtop\Etoc@@five@@  \fi
          \ifEtoc@v   \let\Etoc@localtop\Etoc@@six@@   \fi
          \def\Etoc@next{\global\Etoc@notactivefalse
                      \global\Etoc@vfalse
                      \global\Etoc@ivfalse
                      \global\Etoc@iiifalse
                      \global\Etoc@iifalse
                      \global\Etoc@ifalse
                      \global\Etoc@false
                      \global\Etoc@jfalse
                      \global\Etoc@jjfalse}%
        \fi
    \fi\fi
\Etoc@next}
\let\etoc@startlocaltoc\@gobble
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\long\def\Etoc@localtableofcontents#1{%
    \edef\Etoc@tmp{#1}%
    \ifnum\Etoc@tmp<\@ne
    \PackageWarning{etoc}
       {Unknown toc id: run LaTeX to get references right}% 
     \leavevmode --unknown etoc ref: run latex again--\par
    \let\Etoc@next\@gobble\else\let\Etoc@next\@firstofone\fi
    \Etoc@next
    {\edef\etoc@startlocaltoc##1{%
         \noexpand\etoc@@startlocaltoc{##1}{#1}}
    \Etoc@localtoctrue
    \let\Etoc@localtop\Etoc@@minustwo@@
    \global\Etoc@stoptocfalse
    \global\Etoc@notactivetrue
    \Etoc@tableofcontents}%
    \endgroup\ifEtoc@mustclosegroup\expandafter\endgroup\fi}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@getrefno #1#2\etoc@{#1}
\def\Etoc@getref #1{\@ifundefined{r@#1}{0}{\expandafter\expandafter\expandafter
          \Etoc@getrefno\csname r@#1\endcsname\relax\etoc@}}
\def\Etoc@ref#1{\Etoc@localtableofcontents{\Etoc@getref{#1}}}
\def\Etoc@label#1{\label{#1}%
       \futurelet\Etoc@nexttoken\Etoc@t@bleofcontents}
{\def\1{\Etoc@again}\expandafter
\gdef\1 {\futurelet\Etoc@nexttoken\Etoc@t@bleofcontents}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@t@bleofcontents{%
 \ifx\Etoc@nexttoken\label
    \def\Etoc@next{\expandafter\Etoc@label\@gobble}\else
 \ifx\Etoc@nexttoken\@sptoken
    \let\Etoc@next\Etoc@again\else
 \ifEtoc@tocwithid
    \def\Etoc@next{\Etoc@localtableofcontents{\c@etoc@tocid}}%
 \else
    \ifx\Etoc@nexttoken\ref
       \def\Etoc@next{\expandafter\Etoc@ref\@gobble}%
    \else
       \def\Etoc@next{\Etoc@localtocfalse
                      \global\Etoc@notactivefalse
                      \Etoc@tableofcontents
                      \endgroup
                      \ifEtoc@mustclosegroup\expandafter\endgroup\fi}%
    \fi
 \fi\fi\fi\Etoc@next}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\table@fcontents{\par
    \refstepcounter{etoc@tocid}% 
    \Etoc@tocwithidfalse
    \futurelet\Etoc@nexttoken\Etoc@t@bleofcontents}
\def\localtable@fcontents{\par
    \refstepcounter{etoc@tocid}%
    \addtocontents{toc}
       {\string\etoc@startlocaltoc\string{\arabic{etoc@tocid}\string}}%
    \Etoc@tocwithidtrue
    \futurelet\Etoc@nexttoken\Etoc@t@bleofcontents}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocaftertitlehook{}
\def\tableofcontents{\ifEtoc@tocopenedforwrite\else\Etoc@starttoc\fi
   \global\Etoc@tocopenedforwritetrue
   \begingroup
   \def\etoc@startlocaltoc##1{\etoc@@startlocaltoc{##1}{\c@etoc@tocid}}%
   \@ifstar
   {\let\Etoc@aftertitlehook\relax\table@fcontents}
   {\let\Etoc@aftertitlehook\etocaftertitlehook\table@fcontents}}
\def\localtableofcontents{\ifEtoc@tocopenedforwrite\else\Etoc@starttoc\fi
   \global\Etoc@tocopenedforwritetrue
   \begingroup\@ifstar
   {\let\Etoc@aftertitlehook\relax\localtable@fcontents}
   {\let\Etoc@aftertitlehook\etocaftertitlehook\localtable@fcontents}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocsettocstyle[2]{%
\long\def\Etoc@tableofcontents
{\ifnum\c@tocdepth>\Etoc@minf\let\Etoc@@next\@firstofone\else
\let\Etoc@@next\@gobble\fi
\Etoc@@next{#1\Etoc@aftertitlehook
              \let\Etoc@savedcontentsline\contentsline
              \let\contentsline\Etoc@etoccontentsline
              \Etoc@toctoc\let\Etoc@n@xt\relax
              \ifEtoc@tocwithid\else
              \ifEtoc@localtoc
              \ifEtoc@notactive
              \def\Etoc@n@xt{\Etoc@localtocfalse
                             \global\Etoc@notactivefalse
                             \Etoc@toctoc}%
              \fi\fi\fi\Etoc@n@xt
              #2}}}
\newcommand\etocextendtocstyle[2]{%
\toks0=\expandafter{\Etoc@tableofcontents #2}%
\toks2={#1}%
\long\edef\Etoc@tableofcontents{\the\toks2 \the\toks0}}
%                      % je me fais avoir ^ à chaque fois!
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocsetstyle{\Etoc@standardfalse\etoc@setstyle}
\long\def\etoc@setstyle#1#2#3#4#5{%
\long\expandafter\def
     \csname Etoc@begin@\csname Etoc@#1@\endcsname\endcsname {#2}%
\long\expandafter\def
     \csname Etoc@prefix@\csname Etoc@#1@\endcsname\endcsname {#3}%
\long\expandafter\def
     \csname Etoc@contents@\csname Etoc@#1@\endcsname\endcsname {#4}%
\long\expandafter\def
     \csname Etoc@end@\csname Etoc@#1@\endcsname\endcsname {#5}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocfontminustwo{\normalfont \LARGE \bfseries}
\newcommand\etocfontminusone{\normalfont \large \bfseries}
\newcommand\etocfontzero{\normalfont \large \bfseries}
\newcommand\etocfontone{\normalfont \normalsize \bfseries}
\newcommand\etocfonttwo{\normalfont \normalsize}
\newcommand\etocfontthree{\normalfont \footnotesize}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocsepminustwo{4ex \@plus .5ex \@minus .5ex}
\newcommand\etocsepminusone{4ex \@plus .5ex \@minus .5ex}
\newcommand\etocsepzero{2.5ex \@plus .4ex \@minus .4ex}
\newcommand\etocsepone{1.5ex \@plus .3ex \@minus .3ex}
\newcommand\etocseptwo{1ex \@plus .15ex \@minus .15ex}
\newcommand\etocsepthree{.25ex \@plus .05ex \@minus .05ex}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocbaselinespreadminustwo{1}
\newcommand\etocbaselinespreadminusone{1}
\newcommand\etocbaselinespreadzero{1}
\newcommand\etocbaselinespreadone{1}
\newcommand\etocbaselinespreadtwo{1}
\newcommand\etocbaselinespreadthree{.9}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocminustwoleftmargin{1.5em plus 0.5fil}
\newcommand\etocminustworightmargin{1.5em plus -0.5fil}
\newcommand\etocminusoneleftmargin{1em}
\newcommand\etocminusonerightmargin{1em}
\newcommand\etoctoclineleaders
    {\hbox{\normalfont\normalsize\hb@xt@2ex {\hss.\hss}}}
\newcommand\etocabbrevpagename{p.~}
\newcommand\etocpartname{\partname}
\newcommand\etocbookname{Book}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\etoctoclines{\Etoc@standardfalse
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\etoc@setstyle{@minustwo}{\etocskipfirstprefix}{\pagebreak[3]}
{\begingroup \etocfontminustwo
  \addvspace{\etocsepminustwo}%
  \parindent \z@ 
  \leftskip  \etocminustwoleftmargin
  \rightskip \etocminustworightmargin
  \parfillskip \@flushglue
  \vbox{\etocifnumbered{\etocbookname~\etocnumber:~}{}\etocname
  \baselineskip\etocbaselinespreadminustwo\baselineskip\par}%
  \addvspace{\etocsepminusone}\nobreak
\endgroup}
{}%
\etoc@setstyle{@minusone}{\etocskipfirstprefix}{\pagebreak[3]}
{\begingroup \etocfontminusone
  \addvspace{\etocsepminusone}%
  \parindent \z@ 
  \leftskip  \etocminusoneleftmargin
  \rightskip \etocminusonerightmargin
  \parfillskip \@flushglue
  \vbox{\etocpartname\etocifnumbered{ \etocnumber.~}{ }\etocname
  \baselineskip\etocbaselinespreadminusone\baselineskip\par}%
  \addvspace{\etocsepzero}\nobreak
\endgroup}
{}%
\etoc@setstyle{@zero}
{\addpenalty\@highpenalty\etocskipfirstprefix}{\pagebreak[2]}
{\begingroup 
   \etocfontzero
   \addvspace{\etocsepzero}%
   \parindent \z@ \parfillskip \@flushglue 
   \vbox{\etocifnumbered{\etocnumber\space}{}\etocname
   \baselineskip\etocbaselinespreadzero\baselineskip\par}%
   \addvspace{\etocsepone}\addpenalty\@itempenalty
\endgroup}
{\addpenalty\@secpenalty}%
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\etoc@setstyle{@one}
{\addpenalty\@medpenalty\etocskipfirstprefix}{\pagebreak[1]}
{\begingroup\etocfontone
\addvspace{\etocsepone}%
\parindent \z@ \parfillskip \z@ 
\setbox\z@\vbox{\parfillskip\@flushglue
\etocname\par\setbox\tw@\lastbox
\global\setbox\@ne\hbox{\unhbox\tw@}}%
\dimen\z@=\wd\@ne
\setbox\z@=\etoctoclineleaders
\advance\dimen\z@\wd\z@
\etocifnumbered
{\setbox\tw@\hbox{\etocnumber, \etocabbrevpagename\etocpage}}
{\setbox\tw@\hbox{\etocabbrevpagename\etocpage}}%
\advance\dimen\z@\wd\tw@
\ifdim\dimen\z@ < \linewidth
       \vbox{\etocname\nobreak
       \leaders\box\z@\hfil\box\tw@
       \baselineskip\etocbaselinespreadone\baselineskip\par}
   \else
       \vbox{\etocname\nobreak
       \ifdim\wd\@ne<\linewidth\leaders\copy\z@\hfil\break\fi
       \hbox{}\leaders\box\z@\hfil\box\tw@
       \baselineskip\etocbaselinespreadone\baselineskip\par}
\fi
\addvspace{\etocseptwo}\addpenalty\@itempenalty
\endgroup}
{\addpenalty\@secpenalty}%
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\etoc@setstyle{@two}
{\addpenalty\@medpenalty\etocskipfirstprefix} 
{\pagebreak[1]}
{\begingroup\etocfonttwo
\addvspace{\etocseptwo}%
\parindent \z@ \parfillskip \z@
\setbox\z@\vbox{\parfillskip\@flushglue
\etocname\par\setbox\tw@\lastbox
\global\setbox\@ne\hbox{\unhbox\tw@}}%
\dimen\z@=\wd\@ne
\setbox\z@=\etoctoclineleaders
\advance\dimen\z@\wd\z@
\etocifnumbered
{\setbox\tw@\hbox{\etocnumber, \etocabbrevpagename\etocpage}}
{\setbox\tw@\hbox{\etocabbrevpagename\etocpage}}%
\advance\dimen\z@\wd\tw@
\ifdim\dimen\z@ < \linewidth
       \vbox{\etocname\nobreak
       \leaders\box\z@\hfil\box\tw@
       \baselineskip\etocbaselinespreadtwo\baselineskip\par}
   \else
       \vbox{\etocname\nobreak
       \ifdim\wd\@ne<\linewidth\leaders\copy\z@\hfil\break\fi
       \hbox{}\leaders\box\z@\hfil\box\tw@
       \baselineskip\etocbaselinespreadtwo\baselineskip\par}
\fi
\addvspace{\etocsepthree}\addpenalty\@itempenalty
\endgroup}
{\addpenalty\@secpenalty}%
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\etoc@setstyle{@three}
{\nopagebreak
\etocfontthree
\addvspace{\etocsepthree}%
\nointerlineskip\noindent
\etocskipfirstprefix}
{\allowbreak\,--\,}
{\etocname\unskip}  % \xspace in \etocname
{.\hfil\begingroup
\baselineskip\etocbaselinespreadthree\baselineskip
\par\endgroup
\addpenalty{-\@highpenalty}}%
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\etoc@setstyle{@four}{}{}{}{}%
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\etoc@setstyle{@five}{}{}{}{}%
}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocabovetocskip{3.5ex \@plus 1ex \@minus .2ex} 
\newcommand\etocbelowtocskip{3.5ex \@plus 1ex \@minus .2ex}
\newcommand\etoccolumnsep{2em}
\newcommand\etocmulticolsep{0ex}
\newcommand\etocmulticolpretolerance{-1}
\newcommand\etocmulticoltolerance{200}
\newcommand\etocdefaultnbcol{2}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocmulticolstyle[2][\etocdefaultnbcol]{%
\etocsettocstyle
   {\addvspace{\etocabovetocskip}%
    \ifnum #1>\@ne\let\Etoc@next\@firstoftwo
         \else \let\Etoc@next\@secondoftwo\fi
    \Etoc@next{%
    \multicolpretolerance\etocmulticolpretolerance
    \multicoltolerance\etocmulticoltolerance
    \setlength{\columnsep}{\etoccolumnsep}%
    \setlength{\multicolsep}{\etocmulticolsep}%
    \begin{multicols}{#1}[#2\addvspace{\etocsepminusone}]}
    {#2\addvspace{\etocsepminusone}%
       \pretolerance\etocmulticolpretolerance
       \tolerance\etocmulticoltolerance}}
   {\ifnum #1>\@ne\let\Etoc@next\@firstofone
         \else \let\Etoc@next\@gobble\fi
    \Etoc@next{\end{multicols}}%
    \addvspace{\etocbelowtocskip}}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocinnertopsep{2ex}
\newcommand\etocinnerbottomsep{3.5ex}
\newcommand\etocinnerleftsep{2em}
\newcommand\etocinnerrightsep{2em}
\newcommand\etoctoprule{\hrule}
\newcommand\etocleftrule{\vrule}
\newcommand\etocrightrule{\vrule}
\newcommand\etocbottomrule{\hrule}
\newcommand\etoctoprulecolorcmd{\relax}
\newcommand\etocbottomrulecolorcmd{\relax}
\newcommand\etocleftrulecolorcmd{\relax}
\newcommand\etocrightrulecolorcmd{\relax}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etoc@ruledheading[1]{%
   \hb@xt@\linewidth{\color@begingroup
          \hss #1\hss\hskip-\linewidth
          \etoctoprulecolorcmd\leaders\etoctoprule\hss
          \phantom{#1}%
          \leaders\etoctoprule\hss\color@endgroup}%
         \nointerlineskip\vskip\etocinnertopsep}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocruledstyle[2][\etocdefaultnbcol]{%
\etocsettocstyle
   {\addvspace{\etocabovetocskip}%
    \ifnum #1>\@ne\let\Etoc@next\@firstoftwo
         \else \let\Etoc@next\@secondoftwo\fi
    \Etoc@next
       {\multicolpretolerance\etocmulticolpretolerance
        \multicoltolerance\etocmulticoltolerance
        \setlength{\columnsep}{\etoccolumnsep}%
        \setlength{\multicolsep}{\etocmulticolsep}%
        \begin{multicols}{#1}[\etoc@ruledheading{#2}]}
       {\etoc@ruledheading{#2}\nobreak
         \pretolerance\etocmulticolpretolerance
         \tolerance\etocmulticoltolerance}} 
   {\ifnum #1>\@ne\let\Etoc@next\@firstofone
         \else \let\Etoc@next\@gobble\fi
    \Etoc@next{\end{multicols}}%
    \addvspace{\etocbelowtocskip}}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocframedmphook{\relax}
\newcommand\etocbkgcolorcmd{\relax}
\newcommand\Etoc@relax{\relax}
\newbox\etoc@framed@titlebox
\newbox\etoc@framed@contentsbox
\newcommand\etocframedstyle[2][\etocdefaultnbcol]{%
\etocsettocstyle{%
    \addvspace{\etocabovetocskip}%
    \sbox\z@{#2}%
    \dimen\z@\dp\z@
        \ifdim\wd\z@<\linewidth \dp\z@\z@ \else \dimen\z@\z@ \fi
    \setbox\etoc@framed@titlebox=\hb@xt@\linewidth{\color@begingroup
        \hss 
        \ifx\etocbkgcolorcmd\Etoc@relax\else 
            \sbox\tw@{\color{white}%
            \vrule\@width\wd\z@\@height\ht\z@\@depth\dimen\z@}%
            \ifdim\wd\z@<\linewidth \dp\tw@\z@\fi
            \box\tw@
            \hskip-\wd\z@
        \fi
        \copy\z@
        \hss
        \hskip-\linewidth
        \etoctoprulecolorcmd\leaders\etoctoprule\hss%
        \hskip\wd\z@
        \etoctoprulecolorcmd\leaders\etoctoprule\hss\color@endgroup}%
    \setbox\z@\hbox{\etocleftrule\etocrightrule}%
    \dimen\tw@\linewidth\advance\dimen\tw@-\wd\z@
        \advance\dimen\tw@-\etocinnerleftsep
        \advance\dimen\tw@-\etocinnerrightsep
    \setbox\etoc@framed@contentsbox=\vbox\bgroup
        \hsize\dimen\tw@
        \kern\dimen\z@
        \vskip\etocinnertopsep
        \hbox\bgroup
        \begin{minipage}{\hsize}%
        \etocframedmphook
    \ifnum #1>\@ne\let\Etoc@next\@firstoftwo
    \else \let\Etoc@next\@secondoftwo\fi
        \Etoc@next
        {\multicolpretolerance\etocmulticolpretolerance
        \multicoltolerance\etocmulticoltolerance
        \setlength{\columnsep}{\etoccolumnsep}%
        \setlength{\multicolsep}{\etocmulticolsep}%
        \begin{multicols}{#1}}  
        {\pretolerance\etocmulticolpretolerance
         \tolerance\etocmulticoltolerance}}
     {\ifnum #1>\@ne\let\Etoc@next\@firstofone
         \else \let\Etoc@next\@gobble\fi
    \Etoc@next{\end{multicols}\unskip}%
    \end{minipage}%
    \egroup
    \vskip\etocinnerbottomsep
    \egroup
    \vbox{\hsize\linewidth
        \ifx\etocbkgcolorcmd\Etoc@relax\else
            \kern\ht\etoc@framed@titlebox
            \kern\dp\etoc@framed@titlebox
            \hb@xt@\linewidth{\color@begingroup
            \etocleftrulecolorcmd\etocleftrule
            \etocbkgcolorcmd
            \leaders\vrule 
                   \@height\ht\etoc@framed@contentsbox 
                   \@depth\dp\etoc@framed@contentsbox 
            \hss
            \etocrightrulecolorcmd\etocrightrule
            \color@endgroup}\nointerlineskip
            \vskip-\dp\etoc@framed@contentsbox
            \vskip-\ht\etoc@framed@contentsbox
            \vskip-\dp\etoc@framed@titlebox
            \vskip-\ht\etoc@framed@titlebox
        \fi
    \box\etoc@framed@titlebox\nointerlineskip
    \hb@xt@\linewidth{\color@begingroup
    {\etocleftrulecolorcmd\etocleftrule}%
    \hss\box\etoc@framed@contentsbox\hss
    \etocrightrulecolorcmd\etocrightrule\color@endgroup}
    \nointerlineskip
    \vskip\ht\etoc@framed@contentsbox
    \vskip\dp\etoc@framed@contentsbox
    \hb@xt@\linewidth{\color@begingroup\etocbottomrulecolorcmd
          \leaders\etocbottomrule\hss\color@endgroup}}
    \addvspace{\etocbelowtocskip}}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etoc@multicoltoc[2][\etocdefaultnbcol]{%
\etocmulticolstyle[#1]{#2}%
\tableofcontents}
\newcommand\etoc@multicoltoci[2][\etocdefaultnbcol]{%
\etocmulticolstyle[#1]{#2}%
\tableofcontents*}
\newcommand\etoc@local@multicoltoc[2][\etocdefaultnbcol]{%
\etocmulticolstyle[#1]{#2}%
\localtableofcontents}
\newcommand\etoc@local@multicoltoci[2][\etocdefaultnbcol]{%
\etocmulticolstyle[#1]{#2}%
\localtableofcontents*}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etoc@ruledtoc[2][\etocdefaultnbcol]{%
\etocruledstyle[#1]{#2}%
\tableofcontents}
\newcommand\etoc@ruledtoci[2][\etocdefaultnbcol]{%
\etocruledstyle[#1]{#2}%
\tableofcontents*}
\newcommand\etoc@local@ruledtoc[2][\etocdefaultnbcol]{%
\etocruledstyle[#1]{#2}%
\localtableofcontents}
\newcommand\etoc@local@ruledtoci[2][\etocdefaultnbcol]{%
\etocruledstyle[#1]{#2}%
\localtableofcontents*}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etoc@framedtoc[2][\etocdefaultnbcol]{%
\etocframedstyle[#1]{#2}%
\tableofcontents}
\newcommand\etoc@framedtoci[2][\etocdefaultnbcol]{%
\etocframedstyle[#1]{#2}%
\tableofcontents*}
\newcommand\etoc@local@framedtoc[2][\etocdefaultnbcol]{%
\etocframedstyle[#1]{#2}%
\localtableofcontents}
\newcommand\etoc@local@framedtoci[2][\etocdefaultnbcol]{%
\etocframedstyle[#1]{#2}%
\localtableofcontents*}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocmulticol{\begingroup
    \Etoc@mustclosegrouptrue
    \@ifstar
    {\etoc@multicoltoci}
    {\etoc@multicoltoc}}
\newcommand\etocruled{\begingroup
    \Etoc@mustclosegrouptrue
    \@ifstar
    {\etoc@ruledtoci}
    {\etoc@ruledtoc}}
\newcommand\etocframed{\begingroup
    \Etoc@mustclosegrouptrue
    \@ifstar
    {\etoc@framedtoci}
    {\etoc@framedtoc}}
\newcommand\etoclocalmulticol{\begingroup
    \Etoc@mustclosegrouptrue
    \@ifstar
    {\etoc@local@multicoltoci}
    {\etoc@local@multicoltoc}}
\newcommand\etoclocalruled{\begingroup
    \Etoc@mustclosegrouptrue
    \@ifstar
    {\etoc@local@ruledtoci}
    {\etoc@local@ruledtoc}}
\newcommand\etoclocalframed{\begingroup
    \Etoc@mustclosegrouptrue
    \@ifstar
    {\etoc@local@framedtoci}
    {\etoc@local@framedtoc}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocarticlestyle{%
    \etocsettocstyle
    {\section *{\contentsname 
                \@mkboth {\MakeUppercase \contentsname}
                         {\MakeUppercase \contentsname}}}
    {}}
\newcommand\etocarticlestylenomarks{%
    \etocsettocstyle
    {\section *{\contentsname}}
    {}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocbookstyle{%
    \etocsettocstyle
    {\if@twocolumn \@restonecoltrue \onecolumn \else \@restonecolfalse \fi 
     \chapter *{\contentsname 
                \@mkboth {\MakeUppercase \contentsname}
                         {\MakeUppercase \contentsname}}}
    {\if@restonecol \twocolumn \fi}}
\newcommand\etocbookstylenomarks{%
    \etocsettocstyle
    {\if@twocolumn \@restonecoltrue \onecolumn \else \@restonecolfalse \fi 
     \chapter *{\contentsname}}
    {\if@restonecol \twocolumn \fi}}
\let\etocreportstyle\etocbookstyle
\let\etocreportstylenomarks\etocbookstylenomarks
%    \end{macrocode}
% placeholder for comments. December 1, 11:33 Very weird
% problem with the two successive \cs{addtocontents} (one for
% \cs{etoc@startlocaltoc} and the next one here for the memoir
% class and unstarred \cs{tableofcontents}). They are not put
% in the correct order into the aux file hence into the toc
% file. It is very weird that if the aux file already exists
% due to a previous latex run where the user had put * after
% the TOC displaying command, the things end up in the correct
% order in the new aux file, hence things work correctly.
% Anyway, \cs{etocaftertitlehook} is defined here in a strange
% way which empirically seems to solve the problem. v1.05a
%    \begin{macrocode}
\newcommand\Etoc@addcontentsline{}
\newcommand\etocmemoirtoctotocfmt[2]{%
    \renewcommand\Etoc@addcontentsline{\addcontentsline {toc}{#1}{#2}}%
    \renewcommand\etocaftertitlehook{%
      \ifmem@em@starred@listof
      \else\phantomsection\aftergroup\Etoc@addcontentsline\fi}}
\newcommand\etocmemoirstyle{%
    \etocsettocstyle
        {\ensureonecol \par \begingroup \@nameuse {@tocmaketitle}
         \Etoc@aftertitlehook\let\Etoc@aftertitlehook\relax
         \parskip \cftparskip \@nameuse {cfttocbeforelisthook}}
        {\@nameuse {cfttocafterlisthook}\endgroup\restorefromonecol}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocscrartclstyle{%
    \etocsettocstyle
        {\let\if@dynlist\if@tocleft
         \iftocfeature {toc}{onecolumn}
             {\iftocfeature {toc}{leveldown}
              {}
              {\if@twocolumn \aftergroup \twocolumn \onecolumn \fi }}
             {}%
         \tocbasic@listhead {\listoftocname}%
         \begingroup \expandafter \expandafter \expandafter 
         \endgroup \expandafter 
         \ifx 
             \csname microtypesetup\endcsname \relax 
         \else 
             \iftocfeature {toc}{noprotrusion}{}
                 {\microtypesetup {protrusion=false}%
                  \PackageInfo {tocbasic}%
                  {character protrusion at toc deactivated}}%
         \fi
         \setlength {\parskip }{\z@ }%
         \setlength {\parindent }{\z@ }%
         \setlength {\parfillskip }{\z@ \@plus 1fil}% 
         \csname tocbasic@@before@hook\endcsname
         \csname tb@toc@before@hook\endcsname}
        {\csname tb@toc@after@hook\endcsname 
         \csname tocbasic@@after@hook\endcsname}}
\let\etocscrbookstyle\etocscrartclstyle
\let\etocscrreprtstyle\etocscrartclstyle
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocnakedtocdisplaystyle{\etocsettocstyle{}{}}
\newcommand\etocstandarddisplaystyle{\etocarticlestyle}
\newcommand\etocmarkboth[1]{%
    \@mkboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}}
\newcommand\etocmarkbothnouc[1]{\@mkboth{#1}{#1}}
\newcommand\etoctocstyle[3][section]{\etocmulticolstyle[#2]%
    {\csname #1\endcsname *{#3}}}
\newcommand\etoctocstylewithmarks[4][section]{\etocmulticolstyle[#2]%
    {\csname #1\endcsname *{#3\etocmarkboth{#4}}}}
\newcommand\etoctocstylewithmarksnouc[4][section]{\etocmulticolstyle[#2]%
    {\csname #1\endcsname *{#3\etocmarkbothnouc{#4}}}}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\def\Etoc@redefetocstyle#1{%
    \renewcommand\etoctocstylewithmarks[4][#1]
    {\etocmulticolstyle[##2]%
          {\csname ##1\endcsname *{##3\etocmarkboth{##4}}}}
    \renewcommand\etoctocstylewithmarksnouc[4][#1]
    {\etocmulticolstyle[##2]%
          {\csname ##1\endcsname *{##3\etocmarkbothnouc{##4}}}}
    \renewcommand\etoctocstyle[3][#1]{%
     \etocmulticolstyle[##2]{\csname ##1\endcsname *{##3}}}}
\@ifclassloaded{scrartcl}
    {\renewcommand\etocstandarddisplaystyle{\etocscrartclstyle}}{}
\@ifclassloaded{book}
    {\renewcommand\etocfontone{\normalfont\normalsize}
     \renewcommand\etocstandarddisplaystyle{\etocbookstyle}
     \Etoc@redefetocstyle{chapter}}{}
\@ifclassloaded{report}
    {\renewcommand\etocfontone{\normalfont\normalsize}
     \renewcommand\etocstandarddisplaystyle{\etocreportstyle}
     \Etoc@redefetocstyle{chapter}}{}
\@ifclassloaded{scrbook}
    {\renewcommand\etocfontone{\normalfont\normalsize}
     \renewcommand\etocstandarddisplaystyle{\etocscrbookstyle}
     \Etoc@redefetocstyle{chapter}}{}
\@ifclassloaded{scrreprt}
    {\renewcommand\etocfontone{\normalfont\normalsize}
     \renewcommand\etocstandarddisplaystyle{\etocscrreprtstyle}
     \Etoc@redefetocstyle{chapter}}{}
\@ifclassloaded{memoir}
    {\renewcommand\etocfontone{\normalfont\normalsize}
     \etocmemoirtoctotocfmt{chapter}{\contentsname}%
     \renewcommand\etocstandarddisplaystyle{\etocmemoirstyle}
     \Etoc@redefetocstyle{chapter}}{}
%    \end{macrocode}
% placeholder for comments
%    \begin{macrocode}
\newcommand\etocstandardlines{\Etoc@standardtrue}
\etoctoclines
\etocstandardlines
\etocstandarddisplaystyle
\endinput
%    \end{macrocode}
% \MakePercentComment
\CharacterTable
 {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
  Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
  Digits        \0\1\2\3\4\5\6\7\8\9
  Exclamation   \!     Double quote  \"     Hash (number) \#
  Dollar        \$     Percent       \%     Ampersand     \&
  Acute accent  \'     Left paren    \(     Right paren   \)
  Asterisk      \*     Plus          \+     Comma         \,
  Minus         \-     Point         \.     Solidus       \/
  Colon         \:     Semicolon     \;     Less than     \<
  Equals        \=     Greater than  \>     Question mark \?
  Commercial at \@     Left bracket  \[     Backslash     \\
  Right bracket \]     Circumflex    \^     Underscore    \_
  Grave accent  \`     Left brace    \{     Vertical bar  \|
  Right brace   \}     Tilde         \~}

\CheckSum{2484}

\Finale
%%
%% End of file `etoc.dtx'.

