\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{nccsect}
         [2002/05/19 v1.03 Extended Section and TOC Handling (NCC)]
%
% \changes{v1.00}{2002/02/01}{This version is uploaded to CTAN}
% \changes{v1.01}{2002/02/08}{\NCC@thetocchapter hook added}
% \changes{v1.02}{2002/02/13}{Bug in using the \nosectiontag fixed}
% \changes{v1.02}{2002/03/19}{Test for caption with empty text added}
% \changes{v1.03}{2002/05/19}{Vskips in captions after style changes now}
%
% The standard section definition method is changed here. The following
% improvements are introduced:
%
%  1 The \DeclareSection command is used for defining or redefining
%    section commands. New section levels are easy produced now.
%    \DeclareSection{0} redefines \chapter command (in book and report) or
%    \part command (in article). \DeclareSection{-1} redefines caption for
%    float figures and \DeclareSection{-2} redefines caption for float tables.
%
%  2 An indentation of the first paragraph after a heading of display section
%    is controlled by \indentaftersection and \noindentaftersection commands
%    now.
%
%  3 The \DeclareSection* command declares display sections whose alignment is
%    controlled with the \sectionstyle command.
%
%  4 A generated section command could be called by
%
%    \startsection{level}[aux-title]{title} or
%    \startsection{level}*{title}
%
%    If level<=0, this command is equivalent to the \chapter or \part
%    depending on level and class used. For level=0, if \chapter command is
%    defined, the \chapter is called. In other cases the \part is called.
%
%    Standard section commands (\section, \subsection, etc.) are redefined as
%
%    \def\section{\startsection{1}}
%    \def\subsection{\startsection{2}}
%    etc.
%
%  5 The \norunninghead command supresses generation of running head in the
%    next non-starred section.
%
%  6 The \runninghead{running title} command overrides a text going to the
%    running head when a new non-starred section starts and an appropriate
%    \pagestyle is in use (the solution to make running head with
%    \sectionmark, \subsectionmark, ... right after a new section is bad
%    because it sometimes works incorrect). This command has higher priority
%    than \norunninghead.
%
%  7 It is not necessary to define mark commands for all section levels.
%    If corresponding section mark command does not exist, the updating of
%    running head is skipped.
%
%  8 The \noheadingtag command supresses a number tag in the next section,
%    but all other attendant actions are executed (writing to aux and
%    updating the running head).
%
%  9 The \headingtag{tag} command overrides a number tag in the next section.
%    It has higher priority than \noheadingtag. Overrided section tag can be
%    reffered with the \label command. All fragile commands in overriden
%    tag should be protected. 
%
% 10 The \headingtag*{tag} command prepares a number tag as is, ignoring
%    the tag style defined while the corresponding section command was
%    declared. The aux-file and running head are not updated in this case.
%    All fragile commands in overriden tag should be protected.
%
% 11 The \SectionTagSuffix and \CaptionTagSuffix allow customizing suffixes
%    inserted after section and caption tags.
%
% The TOC-like entries are also redefined in this packages.
%
%  1 The \DeclareTOCEntry command is used for defining or redefining
%    TOC-entries from level 0 and greater and \l@table and \l@figure also.
%
%  2 TOC-entries corresponding to sections of levels more than 5
%    are processed with \l@section@vi, \lsection@vii, etc, commands.
%
%  3 The \numberline command is redefined to remove overlapping of long
%    numbers in TOC-like entries on the text following after numbers.
%
%  4 The \NumberlineSuffix command allows customize a skip inserted after
%    numbers in TOC-like entries. The default is
%    \NumberlineSuffix{\quad}{\enskip}. The first paramater is a suffix
%    prototype for calculation of alignment and the last parameter is really
%    used.
%
%  5 The \PnumPrototype{number} command declares a prototype for page numbers
%    in TOC and redefines its right margin.
%
% The \skipwritingtoaux supresses writing to aux-file in the next section
% or caption command.
%
% The package handles new float types also. To do this, you need to register
% a name of a new float with the \RegisterFloatType command. After that
% you can declare a caption and toc-command for it using the negation
% of its registration number (\DeclareSection{-3}... and
% \DeclareTOCEntry{-3}... for a new float type).
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% These commands are identically defined twice: in nccthm and nccsect. 
%
% \NCC@secskip{VSPACE} adds vspace before a section
% \NCC@runskip is a skip before run-in sections
%
\def\NCC@secskip#1{%
  \if@noskipsec \leavevmode \fi \par
  \if@nobreak \everypar{}\else
    \addpenalty\@secpenalty
    \addvspace{#1}%
  \fi
}
\def\NCC@runskip{2.75ex \@plus 1ex \@minus .2ex}
%
% Useful skip command in TOC.
%
\newcommand{\runinsectionskip}{\NCC@secskip{\NCC@runskip}}
%
% Define \startsection command.
%
\newcommand*{\startsection}[1]{%
  \ifnum#1>\z@ 
    \def\next@{\csname NCC@section\romannumeral#1\endcsname}%
  \else
    \def\next@{\part}%
    \ifnum#1=\z@ \@ifundefined{chapter}{}{\def\next@{\chapter}}\fi
  \fi
  \next@
}
%
% \SectionTagSuffix{SUFFIX} customizes suffix after a section tag
%
\newcommand*{\SectionTagSuffix}[1]{\def\NCC@asecnum{#1}}
\@onlypreamble\SectionTagSuffix
%
% Declare section generation command for an ordinary case:
%
% \DeclareSection{LEVEL}{NAME}[INDENT]{PREFIX}{BEFORESKIP}{AFTERSKIP}{STYLE}
%
% LEVEL:      a number, denoting depth of section -- e.g., section = 1,
%             subsection = 2, etc.; LEVEL=0 means definition of
%             \chapter or \part depending on class used (the NAME
%             is ignored in this case); LEVEL<0 means caption in float
%             {figure, or table, or something else) depending on NAME
%             (INDENT is ignored).
% NAME:       a counter name -- e.g., section, subsection, etc. 
% INDENT:     indentation of heading from the left margin (zero is default).
% PREFIX:     is inserted before the section number tag (usually empty).
% BEFORESKIP: the skip to leave above the heading. The special case
%             -1sp means skip equal to \partopsep+\topsep. It works for
%             ordinary sections only
% AFTERSKIP:  if positive, then skip to leave below the heading, else
%             negative of skip to leave to right of run-in heading.
% STYLE:      commands to set a style. The last command in this argument
%             may be a command such as \MakeUppercase that takes an
%             argument. The section heading will be supplied as the
%             argument to this command. So setting it to, say,
%             \bfseries\MakeUppercase would produce bold,
%             uppercase headings.
%
\newcommand{\DeclareSection}{\@ifstar{\NCC@dsecx}{\NCC@dsec}}
\def\NCC@dsec#1#2{%
  \@ifnextchar[{\NCC@dsect{#1}{#2}}{\NCC@dsect{#1}{#2}[\z@skip]}%
}
\def\NCC@dsect#1#2[#3]#4#5#6#7{%
  \ifnum#1>\z@
    \expandafter\def\csname NCC@section\romannumeral#1\endcsname{%
      \def\NCC@makesectag####1{#4####1\NCC@asecnum}%
      \let\NCC@makesec\NCC@makesect
      \@startsection{#2}{#1}{#3}{#5}{#6}{#7}}%
  \else
    \ifnum#1=\z@
      \NCC@dsecm{\@hangfrom{\hskip #3}\rightskip\@flushglue}{#4}{#5}{#6}{#7}%
    \else
      \NCC@dsecf{#2}{#4}{#5}{#6}{#7}%
    \fi
  \fi
}
\@onlypreamble\DeclareSection
\@onlypreamble\NCC@dsec
\@onlypreamble\NCC@dsect
%
% \NCC@makesect{INDENT}{STYLE}{HEADING}{AFTERSKIP}{ACTION}
%
% Makes standard section. The \@svsec contains prepared section tag.
%
\def\NCC@makesect#1#2#3#4#5{%
  \@tempskipa #4\relax
  \ifdim \@tempskipa>\z@
    \begingroup \normalfont
      #2{\@hangfrom{\hskip #1\relax\@svsec}%
          \interlinepenalty \@M\ignorespaces #3\@@par}%
    \endgroup
    #5%
  \else
    \def\@svsechd{{\normalfont#2{\hskip #1\relax
                   \@svsec\ignorespaces #3}}#5}%
  \fi
  \@xsect{#4}%
}
%
% Now we declare display sections whose alignment is controlled with the
% \sectionstyle:
%
% \DeclareSection*{LEVEL}{NAME}{PREFIX}{BEFORESKIP}{AFTERSKIP}{STYLE}
%
% A negative AFTERSKIP has no meaning in this case.
%
\def\NCC@dsecx#1#2#3#4#5#6{%
  \ifnum#1>\z@
    \expandafter\def\csname NCC@section\romannumeral#1\endcsname{%
      \def\NCC@makesectag####1{#3####1\NCC@asecnum}%
      \let\NCC@makesec\NCC@makesecx
      \@startsection{#2}{#1}{\z@}{#4}{#5}{#6}}%
  \else
    \ifnum#1=\z@
      \NCC@dsecm{\NCC@sec{}\rightskip\@flushglue}{#3}{#4}{#5}{#6}%
    \else
      \NCC@dsecf{#2}{#3}{#4}{#5}{#6}%
    \fi
  \fi
}
\@onlypreamble\NCC@dsecx
%
% \NCC@makesecx{INDENT}{STYLE}{HEADING}{AFTERSKIP}{ACTION}
%
% Makes controlled section. The \@svsec contains prepared section tag.
% The INDENT is ignored in this case.
% 
\def\NCC@makesecx#1#2#3#4#5{%
  \begingroup\normalfont
    #2{\NCC@sec{\@svsec}\interlinepenalty \@M\ignorespaces #3\@@par}%
  \endgroup #5%
  \par \nobreak \vskip #4\relax \@afterheading \ignorespaces
}
%
% \sectionstyle{STYLE} changes display section style to specified one.
% Predefined section styles are:
%
% hangindent  -- standard LaTeX style (default).
% hangindent* -- the same as hangindent, but ragged right.
% parindent   -- sections are indented on \parindent without hange number.
% parindent*  -- the same as parindent, but ragged right.
% center      -- section headers are centered.
% 
\newcommand*{\sectionstyle}[1]{\@nameuse{NCC@sec@#1}}
\def\NCC@sec@hangindent{\def\NCC@sec##1{\@hangfrom{##1}}}
\@namedef{NCC@sec@hangindent*}{%
  \def\NCC@sec##1{\@hangfrom{##1}\rightskip\@flushglue}%
}
\def\NCC@sec@parindent{\def\NCC@sec##1{\@hangfrom\indent##1}}
\@namedef{NCC@sec@parindent*}{%
  \def\NCC@sec##1{\@hangfrom\indent\rightskip\@flushglue##1}%
}
\def\NCC@sec@center{\def\NCC@sec##1{\centering##1}}
\sectionstyle{hangindent} % Default style
%
% \indentaftersection -- produces indentations after display sections
% \noindentaftersection -- suppresses indentations after display sections
%
\newcommand{\indentaftersection}{\@afterindenttrue}
\newcommand{\noindentaftersection}{\@afterindentfalse}
%
% Section controls:
% NCC@nosectag is true if \noheadingtag is used
% NCC@startag is true if \headingtag*{tag} is used
% \NCC@sectag saves \headingtag parameter
% NCC@nosecmark is true if \norunninghead is used
% \NCC@secmark{mark command} saves \runninghead parameter
%
\newif\ifNCC@nosectag   \NCC@nosectagfalse
\newif\ifNCC@secstartag \NCC@secstartagfalse
\newif\ifNCC@nosecmark  \NCC@nosecmarkfalse
\newif\ifNCC@noaux      \NCC@noauxfalse
\let\NCC@sectag\relax
\let\NCC@secmark\relax
%
\newcommand{\skipwritingtoaux}{\NCC@noauxtrue}
\newcommand{\norunninghead}{\NCC@nosecmarktrue}
\newcommand*{\runninghead}[1]{\def\NCC@secmark##1{##1{#1}}}
\newcommand{\noheadingtag}{\NCC@nosectagtrue}
\newcommand{\headingtag}{%
  \@ifstar{\NCC@secstartagtrue\NCC@setsectag}{\NCC@setsectag}%
}
\def\NCC@setsectag#1{\def\NCC@sectag{#1}}
%
% Redefine \@startsection removing afterindent test.
%
\def\@startsection#1#2#3#4#5#6{%
  \NCC@secskip{#4}%
  \@ifstar{\@ssect{#3}{#4}{#5}{#6}}%
          {\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}%
}
%
% Redefine starred form of section
%
% \@ssect{INDENT}{BEFORESKIP}{AFTERSKIP}{STYLE}{HEADING}
%
\def\@ssect#1#2#3#4#5{%
  \let\@svsec\@empty \NCC@makesec{#1}{#4}{#5}{#3}{}%
}
%
% Redefine base form of section
%
% \@sect{NAME}{LEVEL}{INDENT}{BEFORESKIP}{AFTERSKIP}{STYLE}[ARG1]{ARG2}
%
\def\@sect#1#2#3#4#5#6[#7]#8{%
  \NCC@makesection{#1}{#2}{%
    \NCC@makesec{#3}{#6}{#8}{#5}{}%
  }{%
    \NCC@makesec{#3}{#6}{#8}{#5}{%
      \NCC@makesecmark{#1}{#7}{%
        \addcontentsline{toc}{\NCC@secname{#2}}{%
          \ifnum #2>\c@secnumdepth \else
            \protect\numberline{\csname the#1\endcsname}%
          \fi #7%
        }%
      }%
    }%
  }%
}
%
% \NCC@secname{LEVEL} generates section name (section, subsection, ...
% or section@vi, section@vii, ... for new section levels)
%
\def\NCC@secname#1{%
  \ifcase#1\relax \or section\or subsection\or subsubsection\or
    paragraph\or subparagraph\else section@\romannumeral#1\fi
}
%
% \NCC@makesection{NAME}{LEVEL}{STAR-TAG-ACTION}{ORDINARY-ACTION}
%
\def\NCC@makesection#1#2#3#4{%
  \ifNCC@secstartag \protected@edef\@svsec{\NCC@sectag}#3%
  \else
    % Select tag creation style 
    \ifx\NCC@sectag\relax
      \ifNCC@nosectag
        \edef\NCC@restsec{\noexpand\c@secnumdepth \the\c@secnumdepth\relax}%
        \c@secnumdepth -1000
      \else
        \let\NCC@restsec\relax
        \ifnum#2>\c@secnumdepth \else\refstepcounter{#1}\fi
      \fi
    \else
      \expandafter\let\expandafter\NCC@thesec\csname the#1\endcsname
      \def\NCC@restsec{\expandafter\let\csname the#1\endcsname\NCC@thesec}%
      \expandafter\let\csname the#1\endcsname\NCC@sectag
      \protected@edef\@currentlabel{\csname p@#1\endcsname\NCC@sectag}%
    \fi
    % Produce section tag
    \ifnum #2>\c@secnumdepth
      \let\@svsec\@empty
    \else
      \protected@edef\@svsec{\protect\NCC@makesectag{\csname
        the#1\endcsname}}%
    \fi
    \ifNCC@noaux
      \let\NCC@temp\addtocontents \let\addtocontents\@gobbletwo
      #4\let\addtocontents\NCC@temp
    \else
      #4%
    \fi
  \fi
  \let\NCC@sectag\relax
  \NCC@secstartagfalse
  \NCC@nosectagfalse
  \NCC@noauxfalse
}
%
% \NCC@makesecmark{NAME}{TOC-ARG}{ACTION}
%
\def\NCC@makesecmark#1#2#3{%
  \@ifundefined{#1mark}{}{%
    \ifx\NCC@secmark\relax
      \ifNCC@nosecmark \else
        \csname #1mark\endcsname{#2}%
      \fi
    \else
      \NCC@secmark{\csname #1mark\endcsname}%
    \fi
  }%
  \let\NCC@secmark\relax \NCC@nosecmarkfalse
  #3\NCC@restsec
}
\@ifundefined{chapter}{%
  %
  % \NCC@dsecm{ALIGNMENT}{PREFIX}{BEFORESKIP}{AFTERSKIP}{STYLE}
  %
  % Declares \part (section of zero level)
  %
  \def\NCC@dsecm#1#2#3#4#5{%
    \def\part{%
      \NCC@secskip{#3}%
      \def\NCC@makesectag####1{\leavevmode#2\partname\nobreakspace####1}%
      \secdef{\@part{#1}{#4}{#5}}{\@spart{#1}{#4}{#5}}%
    }%
  }
  %
  % \@part{ALIGNMENT}{AFTERSKIP}{STYLE}[ARG1]{ARG2}
  %
  \def\@part#1#2#3[#4]#5{%
    \NCC@makesection{part}{\z@}{%
      \NCC@makepart{#1}{#3}{#5}{#2}{}%
    }{%
      \NCC@makepart{#1}{#3}{#5}{#2}{%
        \NCC@makesecmark{part}{#4}{%
          \addcontentsline{toc}{part}{%
            \ifnum \c@secnumdepth>\m@ne \protect\numberline{\thepart}\fi
            #4%
          }%
        }%
      }%
    }%
  }
  %
  % \@spart{ALIGMNMENT}{AFTERSKIP}{STYLE}{HEADING}
  %
  \def\@spart#1#2#3#4{%
    \let\@svsec\@empty \NCC@makepart{#1}{#3}{#4}{#2}{}%
  }
  %
  % \NCC@makepart{ALIGNMENT}{STYLE}{HEADING}{AFTERSKIP}{ACTION}
  %
  % \@svsec is \@empty or contains prefix, part name and tag
  %
  \def\NCC@makepart#1#2#3#4#5{%
    \begingroup \normalfont
      \ifx\@svsec\@empty \else #1{\@svsec\@@par}\nobreak \fi
      \interlinepenalty \@M #1{#2{#3}\@@par}%
    \endgroup #5%
    \par\nobreak \vskip #4\relax \@afterheading \ignorespaces
  }
  \@ifundefined{partmark}{\def\partmark#1{\markboth{}{}}}{}
}{%
  %
  % \NCC@dsecm{ALIGNMENT}{PREFIX}{BEFORESKIP}{AFTERSKIP}{STYLE}
  %
  % Declares \chapter (section of zero level)
  %
  \def\NCC@dsecm#1#2#3#4#5{%
    \def\chapter{\NCC@startchap
      \def\NCC@makesectag####1{\leavevmode#2\@chapapp\nobreakspace####1}%
      \secdef{\@chapter{#1}{#3}{#4}{#5}}{\@schapter{#1}{#3}{#4}{#5}}%
    }%
  }
  %
  % \@chapter{ALIGNMENT}{BEFORESKIP}{AFTERSKIP}{STYLE}[ARG1]{ARG2}
  %
  \def\@chapter#1#2#3#4[#5]#6{%
    \@ifundefined{if@mainmatter}{}{\if@mainmatter\else\noheadingtag\fi}%
    \NCC@makesection{chapter}{\z@}{%
      \NCC@makechapter{#1}{#2}{#4}{#6}{#3}{}%
    }{%
      \NCC@makechapter{#1}{#2}{#4}{#6}{#3}{%
        \NCC@makesecmark{chapter}{#5}{%
          \typeout{\@chapapp\space\thechapter.}%
          \addcontentsline{toc}{chapter}{%
            \ifnum \c@secnumdepth>\m@ne 
              \protect\numberline{\NCC@thetocchapter}\fi
            #5%
          }%
          \NCC@infloats{\addtocontents{\@nameuse{ext@\@captype}}%
                          {\protect\runinsectionskip}}%
        }%
      }%
    }%
  }
  %
  % \@schapter{ALIGMNMENT}{BEFORESKIP}{AFTERSKIP}{STYLE}{HEADING}
  %
  \def\@schapter#1#2#3#4#5{%
    \let\@svsec\@empty \NCC@makechapter{#1}{#2}{#4}{#5}{#3}{}%
  }
  %
  % \NCC@makechapter{ALIGNMENT}{BEFORESKIP}{STYLE}{HEADING}{AFTERSKIP}{ACTION}
  %
  % \@svsec is \@empty or contains prefix, chapapp name and tag
  %
  \def\NCC@makechapter#1#2#3#4#5#6{%
    \if@twocolumn
      \@topnewpage[\NCC@makechaphead{#1}{#2}{#3}{#4}{#5}{#6}]%
    \else
      \NCC@makechaphead{#1}{#2}{#3}{#4}{#5}{#6}%
      \@afterheading
    \fi
    \ignorespaces
  }
  %
  % \NCC@makechaphead{ALIGNMENT}{BEFORESKIP}{STYLE}{HEADING}{AFTERSKIP}{ACTION}
  %
  \def\NCC@makechaphead#1#2#3#4#5#6{%
    \leavevmode\strut\par\removelastskip \vskip -\baselineskip
    \vspace{#2}%
    \begingroup \normalfont
      \ifx\@svsec\@empty \else #1{\@svsec\@@par}\fi
      \interlinepenalty \@M #1{#3{#4}\@@par}%
    \endgroup #6%
    \par\nobreak \vskip #5\relax
  }
  %
  % Starting chapter hook (can be redefined)
  %
  \def\NCC@startchap{%
    \if@openright\cleardoublepage\else\clearpage\fi
    \thispagestyle{plain}\global\@topnum\z@
  }
  %
  % The following hook allow redefining the appearance of chapter name
  % in toc.
  %
  \def\NCC@thetocchapter{\thechapter}
}
\@onlypreamble\NCC@dsecm
%
% \NCC@dsecf{NAME}{PREFIX}{BEFORESKIP}{AFTERSKIP}{STYLE}
%
\def\caption{\csname NCC@cap@\@captype\endcsname}
\def\NCC@dsecf#1#2#3#4#5{%
  \expandafter\def\csname NCC@cap@#1\endcsname{%
    \def\NCC@makesectag####1{#2\csname #1name\endcsname
      \nobreakspace####1\NCC@acapnum}%
    \NCC@startcaption{#3}{#4}{#5}%
  }%
}
%
% \NCC@startcaption{BEFORESKIP}{AFTERSKIP}{STYLE}
%
\def\NCC@startcaption#1#2#3{%
  \secdef{\NCC@caption{#1}{#2}{#3}}{\NCC@scaption{#1}{#2}{#3}}%
}
%
% \NCC@caption{BEFORESKIP}{AFTERSKIP}{STYLE}[ARG1]{ARG2}
%
\def\NCC@caption#1#2#3[#4]#5{%
  \NCC@makesection{\@captype}{\z@}{%
    \NCC@makecaption{#3}{#1}{#5}{#2}{}%
  }{%
    \NCC@makecaption{#3}{#1}{#5}{#2}{%
      \begingroup
        \let\centering\@empty
        \addcontentsline{\@nameuse{ext@\@captype}}{\@captype}{%
          \ifnum \c@secnumdepth>\z@
            \protect\numberline{\@nameuse{the\@captype}}\fi
          #4%
        }%
      \endgroup
      \NCC@restsec
    }%
  }%
}
%
% \NCC@scaption{BEFORESKIP}{AFTERSKIP}{STYLE}{TEXT}
%
\def\NCC@scaption#1#2#3#4{%
  \let\@svsec\@empty \NCC@makecaption{#3}{#1}{#4}{#2}{}%
}
%
% \NCC@makecaption{STYLE}{BEFORESKIP}{TEXT}{AFTERSKIP}{ACTION}
%
\def\NCC@makecaption#1#2#3#4#5{%
  \begingroup\par\normalfont #1{}\addvspace{#2}\noindent
    \def\@tempa{#3}\ifx\@tempa\@empty\let\NCC@acapnum\@empty\fi
    \setbox\@tempboxa\vtop{\hsize\linewidth\@parboxrestore
      {\@svsec}\ignorespaces#1{#3}\unskip\@@par}%
    \ifnum\dp\@tempboxa<\baselineskip
      \parbox[t]\linewidth{\centering{\@svsec}\ignorespaces#1{#3}\unskip}%
    \else 
      \box\@tempboxa
    \fi
    \par \ifdim #4=\z@ \else \vskip #4\fi
  \endgroup
  #5%
}
%
% Registration of floats
%
\newcommand*{\RegisterFloatType}[1]{%
  \edef\NCC@floatlist{\NCC@floatlist{#1}}%
}
\let\NCC@floatlist\@empty
\@onlypreamble\RegisterFloatType
%
% \NCC@infloats{ACTION} is cycle in registered float types: during
% execution the \@captype contains float name and \@tempcnta is equal to
% its registration number (1 for figure, 2 for table, and so on).
%
\def\NCC@infloats#1{\@tempcnta\z@
  \expandafter \@tfor \expandafter \@captype
   \expandafter :\expandafter =\NCC@floatlist \do
  {\advance\@tempcnta\@ne #1}\let\@captype\relax
}
%
% Customizing of text inserted after caption number.
%
\newcommand*{\CaptionTagSuffix}[1]{\def\NCC@acapnum{#1}}
\@onlypreamble\CaptionTagSuffix
%
% Generic command for customizing contents supporting commands
%
% \DeclareTOCEntry{LEVEL}{ACTION}{PREFIX}{PROTOTYPE}{STYLE}[AFTER]
%
% LEVEL       a number, denoting depth of section -- e.g., section = 1,
%             subsection = 2, etc. For zero and negative level the
%             following commands are created:
%               0 means \l@chapter or \l@part depending on class used.
%              -1 means \l@figure
%              -2 means \l@table
% ACTION      commands called before entry is produced (usually empty).
% PREFIX      text inserted before the section number (usually empty).
% PROTOTYPE   prototype of number for alignment.
% STYLE       commands to set style. The last command in this argument
%             may be a command such as \MakeUppercase that takes an
%             argument. The produced entry will be supplied as the
%             argument to this command. So setting it to, say,
%             \bfseries\MakeUppercase would produce bold,
%             uppercase entry. This style is applied to the number also.
% AFTER       prototype for left margin adjustment for an entry of the
%             next level. Default is PREFIX+PROTOTYPE+SUFFIX.
%
% An entry is produced in a group.
%
\newcommand*{\DeclareTOCEntry}[5]{%
  \@ifnextchar[{\NCC@dtoc{#1}{#2}{#3}{#4}{#5}}%
               {\NCC@dtoc{#1}{#2}{#3}{#4}{#5}%
                [{#3#4\let\do\@firstoftwo\NCC@atocnum}]}%
}
\def\NCC@dtoc#1#2#3#4#5[#6]{%
  \ifnum#1<\z@
    \NCC@infloats{%
      \ifnum#1=-\@tempcnta
        \expandafter\def\csname l@\@captype\endcsname
          {\NCC@tocentry\z@{#2}{#3}{#4}{#5}}%
      \fi
    }%
  \else
    \ifnum#1>\z@
      \edef\@tempa{\noexpand\def\expandafter\noexpand
                   \csname l@\NCC@secname{#1}\endcsname}%
    \else
      \@ifundefined{chapter}{\def\@tempa{\def\l@part}}%
                            {\def\@tempa{\def\l@chapter}}%
    \fi
    \@tempa{\NCC@tocentry{#1}{#2}{#3}{#4}{#5}}%
    \@tempcnta #1\relax \advance\@tempcnta\@ne
    \expandafter\def\csname l@tocskip@\romannumeral\@tempcnta
      \endcsname{\NCC@tocadj{#5{#6}}}%
  \fi
}
\@onlypreamble\DeclareTOCEntry
\@onlypreamble\NCC@dtoc
%
% \NCC@tocentry{LEVEL}{ACTION}{PREFIX}{PROTOTYPE}{STYLE}{ENTRY}{NUMBER}
%
\def\NCC@tocentry#1#2#3#4#5#6#7{%
  \ifnum #1>\c@tocdepth \else
    \par\begingroup\normalfont #2%
      \@tempdimb\z@ \@tempcnta #1\relax
      \@whilenum \@tempcnta >\z@\do
        {\@nameuse{l@tocskip@\romannumeral \@tempcnta}%
         \advance\@tempcnta\m@ne}%
      \def\NCC@maketocnum##1{#5{#3##1\NCC@atocnum}}%
      \settowidth\@tempdima{\let\do\@firstoftwo\NCC@maketocnum{#4}}%
      \@dottedtocline{#1}{\@tempdimb}{\@tempdima}%
        {#5{\let\do\@secondoftwo\ignorespaces#6\unskip}}{#5{#7}}%
      \@nobreakfalse
    \endgroup
  \fi
}
%
% The command increases \@tempdimb on the width of the argument
%
\def\NCC@tocadj#1{\settowidth\@tempdima{#1}\advance\@tempdimb\@tempdima}
%
% Redefine the \numberline to work correct if number width is greater
% than \@tempdima
%
\def\numberline#1{%
  \setbox\@tempboxa\hbox{\NCC@maketocnum{#1}}%
  \ifdim \wd\@tempboxa > \@tempdima \box\@tempboxa
  \else \hb@xt@\@tempdima{\unhbox\@tempboxa\hfil}\fi
  \ignorespaces}
\def\NCC@maketocnum#1{#1\let\do\@secondoftwo\NCC@atocnum}% Default make num.
%
% Customizing of text inserted after TOC-like number.
% \NumberlineSuffix{Prototype for alignment}{Actual}
%
\newcommand*{\NumberlineSuffix}[2]{\def\NCC@atocnum{\do{#1}{#2}}}
\@onlypreamble\NumberlineSuffix
%
% Customizing of right margin in toc
%
\newcommand*{\PnumPrototype}[1]{\def\NCC@pnum{#1}\NCC@setpnum}
\def\NCC@setpnum{%
  \settowidth\@tempdima{\NCC@pnum}\edef\@pnumwidth{\the\@tempdima}%
  \advance\@tempdima 1em \edef\@tocrmarg{\the\@tempdima}%
}  
\@onlypreamble\PnumPrototype
%
% \bff tries to set bold everywhere. This command is used in
% bold headings.
%
\newcommand{\bff}{\normalfont\bfseries\mathversion{bold}}
%
% Defaults: register floats and redefine sections and TOC-entries
%
\RegisterFloatType{figure}
\RegisterFloatType{table}
\DeclareSection{-2}{table}{}{\z@}{10pt}{}
\DeclareSection{-1}{figure}{}{10pt}{\z@}{}
\DeclareSection*1{section}{}%
                {3.5ex \@plus 1ex \@minus .2ex}%
                {2.3ex \@plus .2ex}{\Large\bff}
\DeclareSection*2{subsection}{}%
                {3.25ex \@plus 1ex \@minus .2ex}%
                {1.5ex \@plus .2ex}{\large\bff}
\DeclareSection*3{subsubsection}{}%
                {3ex \@plus 1ex \@minus .2ex}%
                {1.5ex \@plus .2ex}{\normalsize\bff}
\DeclareSection4{paragraph}{}%
                {\NCC@runskip}{-1em}{\normalsize\bff}
\DeclareSection5{subparagraph}[\parindent]{}%
                {\NCC@runskip}{-1em}{\normalsize\bff}
\def\section{\startsection\@ne}
\def\subsection{\startsection\tw@}
\def\subsubsection{\startsection\thr@@}
\def\paragraph{\startsection4}
\def\subparagraph{\startsection5}
%
\@ifundefined{chapter}{% article.cls supposed
  \DeclareSection*0{part}{\Large\bff}%
                  {5ex \@plus 1ex \@minus .2ex}%
                  {4ex \@plus .2ex}{\huge\bff}
  \DeclareTOCEntry{-2}{}{}{9}{}% table
  \DeclareTOCEntry{-1}{}{}{9}{}% figure
  \DeclareTOCEntry0{\runinsectionskip\def\@dotsep{1000}}{}{III}{\bff}[]
  \DeclareTOCEntry1{\runinsectionskip}{}{9}{}
  \DeclareTOCEntry2{}{}{9.9}{}
  \DeclareTOCEntry3{}{}{9.9.9}{}
}{% book.cls or report.cls supposed
  \DeclareSection*0{chapter}{\vspace{3ex}\huge\bff}{10ex}%
                  {8ex \@plus .2ex}{\Huge\bff}
  \DeclareTOCEntry{-2}{}{}{9.9}{}% table
  \DeclareTOCEntry{-1}{}{}{9.9}{}% figure
  \DeclareTOCEntry0{\runinsectionskip\def\@dotsep{1000}%
                    \aftergroup\penalty\aftergroup\@highpenalty}{}{9}{\bff}
  \DeclareTOCEntry1{}{}{9.9}{}[9.9]
  \DeclareTOCEntry2{}{}{9.9.9}{}[9.9.9]
  \DeclareTOCEntry3{}{}{}{}[\qquad]
}
\DeclareTOCEntry4{}{}{}{}[\qquad]
\DeclareTOCEntry5{}{}{}{}[\qquad]
%
\noindentaftersection
\SectionTagSuffix{\quad}
\CaptionTagSuffix{:\hskip .7em \@plus .2em \@minus .1em}
\NumberlineSuffix{\quad}{\enskip}
\PnumPrototype{99}
\endinput
