%=====================================================================================================================================
% Package akksection by Igor Akkerman (c) 2004, 2005
% Package providing an alternative section definition command.
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License (latest version).
%
% The latest version of this license can be found at
%     http://www.latex-project.org/lppl.txt
%
% This file may only be distributed together with the full 'AkkTeX' bundle.
%=====================================================================================================================================
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{akksection}[2005/04/30 by Igor Akkerman (c) 2004, 2005 v1.0.2 Package providing an alternative section definition command.]
%=====================================================================================================================================

\makeatletter

\newif\if@embedded

% \akk@startsection{<name>}{<ebene>}{<einzug>}{<einziehen?>}{<vorabstand>}{<eingebettet?>}{<nachabstand>}{stil}
\def\akk@startsection#1#2#3#4#5#6#7#8{%
    \if@noskipsec \leavevmode \fi%
    \par%
    \@tempskipa #5\relax%
    \csname @afterindent#4\endcsname%
    \if@nobreak%
        \everypar{}%
    \else%
        \addpenalty\@secpenalty\addvspace\@tempskipa%
    \fi%
    \@ifstar{%
        \akk@ssect{#3}{#4}{#5}{#6}{#7}{#8}%
    }{%
        \@dblarg{\akk@sect{#1}{#2}{#3}{#5}{#6}{#7}{#8}}%
    }%
}

% \akk@sect{<name>}{<ebene>}{<einzug>}{<vorabstand>}{<eingebettet?>}}{<nachabstand>}{<stil>}[<toc_eintrag>]{<label>}
\def\akk@sect#1#2#3#4#5#6#7[#8]#9{%
    \ifnum #2>\c@secnumdepth%
        \let\@svsec\@empty%
    \else%
        \refstepcounter{#1}%
        \protected@edef\@svsec{\@seccntformat{#1}\relax}%
    \fi%
    \@tempskipa #6\relax%
    \csname @embedded#5\endcsname%
    \if@embedded%
        \def\@svsechd{%
            #7{\hskip #3\relax\@svsec #9}%
            \csname #1mark\endcsname{#8}%
            \addcontentsline{toc}{#1}{%
                \ifnum #2>\c@secnumdepth%
                \else%
                    \protect\numberline{\csname the#1\endcsname}%
                \fi%
                #8%
            }%
        }%
    \else%
        \begingroup%
            #7{%
                \@hangfrom{\hskip #3\relax\@svsec}%
                \interlinepenalty \@M #9\@@par%
            }%
        \endgroup%
        \csname #1mark\endcsname{#8}% 
        \addcontentsline{toc}{#1}{%
            \ifnum #2>\c@secnumdepth
            \else%
                \protect\numberline{\csname the#1\endcsname}%
            \fi%
            #8%
        }%
    \fi%
    \akk@xsect{#5}{#6}%
}

% \akk@ssect{<einzug>}{<einziehen?>}{<vorabstand>}{<eingebettet?>}{<nachabstand>}{<stil>}{<label>}
\def\akk@ssect#1#2#3#4#5#6#7{%
    \csname @embedded#4\endcsname%
    \if@embedded%
        \def\@svsechd{#6{\hskip #1\relax #7}}%
    \else%
        \begingroup%
            #6{%
                \@hangfrom{\hskip #1}%
                \interlinepenalty \@M #7\@@par%
            }%
        \endgroup%
    \fi%
    \akk@xsect{#4}{#5}%
}

% \akk@xsect{<eingebettet>}{<nachabstand>}
\def\akk@xsect#1#2{%
    \@tempskipa #2\relax%
    \csname @embedded#1\endcsname%
    \if@embedded%
        \@nobreakfalse%
        \global\@noskipsectrue%
        \everypar{%
            \if@noskipsec%
                \global\@noskipsecfalse%
                {\setbox\z@\lastbox}%
                \clubpenalty\@M%
                \begingroup \@svsechd \endgroup%
                \unskip%
                \@tempskipa #2\relax%
                \hskip \@tempskipa%
            \else%
                \clubpenalty \@clubpenalty%
                \everypar{}%
            \fi%
        }%
    \else%
        \par \nobreak%
        \vskip \@tempskipa%
        \@afterheading%
    \fi%
    \ignorespaces%
}

\makeatother
