%%
%% This is file `screxperimental.sty'
%%
%% Copyright (c) 2010
%% Markus Kohm and any individual authors listed elsewhere in this file.
%%
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, version 1.3c of the license.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later and of this work.
%%
%% This work has the LPPL maintenance status "author-maintained".
%%
%% The Current Maintainer and author of this work is Markus Kohm.
%%
\NeedsTeXFormat{LaTeX2e}[1995/06/01]
\ProvidesPackage{screxperimental}[2010/04/23 v0.1.1a-alpha experimental
  features for KOMA-Script]

\PackageWarning{screxperimental}{%
  This is an EXPERIMENTAL package, that adds\MessageBreak
  experimental features to KOMA-Script.\MessageBreak
  You should know, that every one of this\MessageBreak
  features may be CHANGED INCOMPATIBLE or REMOVED\MessageBreak
  in any version of screxperimental.\MessageBreak
  You should know, that using this package may\MessageBreak
  be INCOMPATIBLE WITH ANY CLASS or any other\MessageBreak
  PACKAGE even KOMA-Script classes or packages.\MessageBreak
  You should know, that there's ABSOLUTLY NO\MessageBreak
  SUPPORT for using this package!\@gobble}

%% Loading the KOMA-Script base package
\RequirePackage{scrkbase}

%%% KOMA-Script options
\newcommand*{\@se@osectarg}{0}
\KOMA@ncmdkey{OptionalSectionArgument}[TOCandHead]{@se@osectarg}{%
  {TOCandHead}{0},{tocandhead}{0},%
  {TOC}{1},{toTOC}{1},{totoc}{1},{toc}{1},%
  {Head}{2},{toHead}{2},{tohead}{2},{head}{2}%
}%
\KOMAProcessOptions\relax

%% Use my own temporary switch
\newif\if@se@tempswa

%% Experimental command interface for document structure
%% - \part[<tocandheadentry>]{Title}, 
%%   \part[tocentry={<tocentry>},head={<running head>}]{Title}
\def\@tempa[#1]#2{%
  \ifnum \c@secnumdepth >\m@ne
    \refstepcounter{part}%
    \@maybeautodot\thepart%
    \addcontentsline{toc}{part}{\protect\numberline{\thepart}#1}%
  \else
    \addcontentsline{toc}{part}{#1}%
  \fi
  \begingroup
    \setparsizes{\z@}{\z@}{\z@\@plus 1fil}\par@updaterelative
    \raggedpart
    \interlinepenalty \@M
    \normalfont\sectfont\nobreak
    \ifnum \c@secnumdepth >\m@ne
      \size@partnumber{\partformat}%
      \partheadmidvskip
    \fi
    \size@part{#2}\strut%
    \partmark{#1}\par
  \endgroup
  \nobreak
  \partheadendvskip
  \@afterheading
}
\ifx\@tempa\@part
  % Using \@part-definition of scrartcl:
  \def\@part[#1]{#2}{%
    \se@istest#1=\@nil
    \if@se@tempswa
      \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#2},#1}%
    \else
      \ifcase \@se@osectarg\relax
        \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#1}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#2}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#1}}%
      \fi
    \fi
    \ifnum \c@secnumdepth >\m@ne
      \refstepcounter{part}%
      \@maybeautodot\thepart
      \ifx\se@ds@tocentry\@empty\else
        \addcontentsline{toc}{part}{%
          \protect\numberline{\thepart}\se@ds@tocentry}%
      \fi
    \else
      \ifx\se@ds@tocentry\@empty\else
        \addcontentsline{toc}{part}{\se@ds@tocentry}%
      \fi
    \fi
    \begingroup
      \setparsizes{\z@}{\z@}{\z@\@plus 1fil}\par@updaterelative
      \raggedpart
      \interlinepenalty \@M
      \normalfont\sectfont\nobreak
      \ifnum \c@secnumdepth >\m@ne
        \size@partnumber{\partformat}%
        \partheadmidvskip
      \fi
      \size@part{#2}\strut%
      \expandafter\partmark\expandafter{\se@ds@head}\par
    \endgroup
    \nobreak
    \partheadendvskip
    \@afterheading
  }
\else
  \def\@tempa[#1]#2{%
    \ifnum \c@secnumdepth >-2\relax
      \refstepcounter{part}%
      \@maybeautodot\thepart%
      \addcontentsline{toc}{part}{\protect\numberline{\thepart}#1}%
    \else
      \addcontentsline{toc}{part}{#1}%
    \fi
    \begingroup
      \setparsizes{\z@}{\z@}{\z@\@plus 1fil}\par@updaterelative
      \raggedpart
      \interlinepenalty \@M
      \normalfont\sectfont\nobreak
      \ifnum \c@secnumdepth >-2\relax
        \size@partnumber{\partformat}%
        \partheadmidvskip
      \fi
      \size@part{#2}\strut%
      \partmark{#1}\par
    \endgroup
    \@endpart
  }
  \ifx\@tempa\@part
    % Using \@part definition of scrreprt.cls or scrbook.cls
    \def\@part[#1]#2{%
      \se@istest#1=\@nil
      \if@se@tempswa
        \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#2},#1}%
      \else
        \ifcase \@se@osectarg\relax
          \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#1}}%
        \or
          \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#2}}%
        \or
          \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#1}}%
        \fi
      \fi
      \ifnum \c@secnumdepth >-2\relax
        \refstepcounter{part}%
        \@maybeautodot\thepart
        \ifx\se@ds@tocentry\@empty\else
          \addcontentsline{toc}{part}{%
            \protect\numberline{\thepart}\se@ds@tocentry}%
        \fi
      \else
        \ifx\se@ds@tocentry\@empty\else
          \addcontentsline{toc}{part}{\se@ds@tocentry}%
        \fi
      \fi
      \begingroup
        \setparsizes{\z@}{\z@}{\z@\@plus 1fil}\par@updaterelative
        \raggedpart
        \interlinepenalty \@M
        \normalfont\sectfont\nobreak
        \ifnum \c@secnumdepth >\m@ne
          \size@partnumber{\partformat}%
          \partheadmidvskip
        \fi
        \size@part{#2}\strut%
        \expandafter\partmark\expandafter{\se@ds@head}\par
      \endgroup
      \@endpart
    }
  \else
    \PackageWarning{screxperimental}{%
      Unexpected definition of \string\@part.\MessageBreak
      Extended \string\part\space handling deactivated%
    }%
  \fi
\fi

%% - \addpart[<tocandheadentry>]{Title}, 
%%   \addpart[tocentry={<tocentry>},head={<running head>}]{Title}
\def\@tempa[#1]#2{%
  \csname phantomsection\endcsname
  \addcontentsline{toc}{part}{#1}\@spart{#2}%
  \ifx\partmark\@gobble\else\@mkboth{#1}{}\fi
}
\ifx\@tempa\@addpart
  \def\@addpart[#1]#2{%
    \se@istest#1=\@nil
    \if@se@tempswa
      \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#2},#1}%
    \else
      \ifcase \@se@osectarg\relax
        \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#1}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#2}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#1}}%
      \fi
    \fi
    \csname phantomsection\endcsname
    \ifx\se@ds@tocentry\@empty\else
      \addcontentsline{toc}{part}{\se@ds@tocentry}
    \fi
    \@spart{#2}%
    \ifx\partmark\@gobble\else
      \expandafter\@mkboth\expandafter{\se@ds@head}{}%
    \fi
  }
\else
  \PackageWarning{screxperimental}{%
    Unexpected definition of \string\@addpart.\MessageBreak
    Extended \string\addpart\space handling deactivated%
  }%
\fi


%% - \chapter[<tocandheadentry>]{Title}, 
%%   \chapter[tocentry={<tocentry>},head={<running head>}]{Title}
\def\@tempa[#1]#2{%
  \ifnum \c@secnumdepth >\m@ne
    \refstepcounter{chapter}%
    \@maybeautodot\thechapter
    \typeout{\@chapapp\space\thechapter.}%
    \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}%
    \if@chaptertolists
      \doforeachtocfile{%
        \iftocfeature{\@currext}{chapteratlist}{%
          \addcontentsline{\@currext}{chapteratlist}{%
            \protect\numberline{\thechapter}#1}%
        }{}%
      }%
      \@ifundefined{float@addtolists}{}{%
        \scr@float@addtolists@warning
        \scr@ifundefinedorrelax{@currentHref}{%
          \float@addtolists{%
            \protect\contentsline{chapteratlist}{%
              \protect\numberline{\thechapter}#1}{\thepage}%
          }%
        }{%
          \float@addtolists{%
            \protect\contentsline{chapteratlist}{%
              \protect\numberline{\thechapter}#1}{\thepage}{\@currentHref}%
          }%
        }%
      }%
    \fi
  \else
    \addcontentsline{toc}{chapter}{#1}%
    \if@chaptertolists
      \doforeachtocfile{%
        \iftocfeature{\@currext}{chapteratlist}{%
          \addcontentsline{\@currext}{chapteratlist}{#1}%
        }{}%
      }%
      \@ifundefined{float@addtolists}{}{%
        \scr@float@addtolists@warning
        \scr@ifundefinedorrelax{@currentHref}{%
          \float@addtolists{%
            \protect\contentsline{chapteratlist}{#1}{\thepage}%
          }%
        }{%
          \float@addtolists{%
            \protect\contentsline{chapteratlist}{#1}{\thepage}{\@currentHref}%
          }%
        }%
      }%
    \fi
  \fi
  \chaptermark{#1}%
  \ifdim \@chapterlistsgap>\z@
    \doforeachtocfile{%
      \iftocfeature{\@currext}{chapteratlist}{%
        \addtocontents{\@currext}{\protect\addvspace{\@chapterlistsgap}}%
      }{}%
    }%
    \@ifundefined{float@addtolists}{}{%
      \scr@float@addtolists@warning
      \float@addtolists{\protect\addvspace{\@chapterlistsgap}}%
    }%
  \fi
  \if@twocolumn
    \if@at@twocolumn
      \@makechapterhead{#2}%
    \else
      \@topnewpage[\@makechapterhead{#2}]%
    \fi
  \else
    \@makechapterhead{#2}%
    \@afterheading
  \fi
}
\ifx\@chapter\@tempa
  % Using \@chapter definition of scrreprt.cls
  \def\@chapter[#1]#2{%
    \se@istest#1=\@nil
    \if@se@tempswa
      \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#2},#1}%
    \else
      \ifcase \@se@osectarg\relax
        \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#1}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#2}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#1}}%
      \fi
    \fi
    \ifnum \c@secnumdepth >\m@ne
      \refstepcounter{chapter}%
      \@maybeautodot\thechapter
      \typeout{\@chapapp\space\thechapter.}%
      \ifx\se@ds@tocentry\@empty\else
        \addcontentsline{toc}{chapter}{%
          \protect\numberline{\thechapter}\se@ds@tocentry}%
        \if@chaptertolists
          \doforeachtocfile{%
            \iftocfeature{\@currext}{chapteratlist}{%
              \addcontentsline{\@currext}{chapteratlist}{%
                \protect\numberline{\thechapter}\se@ds@tocentry}%
            }{}%
          }%
          \@ifundefined{float@addtolists}{}{%
            \scr@float@addtolists@warning
            \scr@ifundefinedorrelax{@currentHref}{%
              \float@addtolists{%
                \protect\contentsline{chapteratlist}{%
                  \protect\numberline{\thechapter}\se@ds@tocentry}{\thepage}%
              }%
            }{%
              \float@addtolists{%
                \protect\contentsline{chapteratlist}{%
                  \protect\numberline{\thechapter}\se@ds@tocentry}{\thepage}{%
                  \@currentHref}%
              }%
            }%
          }%
        \fi
      \fi
    \else
      \ifx\se@ds@tocentry\@empty\else
        \addcontentsline{toc}{chapter}{\se@ds@tocentry}%
        \if@chaptertolists
          \doforeachtocfile{%
            \iftocfeature{\@currext}{chapteratlist}{%
              \addcontentsline{\@currext}{chapteratlist}{\se@ds@tocentry}%
            }{}%
          }%
          \@ifundefined{float@addtolists}{}{%
            \scr@float@addtolists@warning
            \scr@ifundefinedorrelax{@currentHref}{%
              \float@addtolists{%
                \protect\contentsline{chapteratlist}{\se@ds@tocentry}{\thepage}%
              }%
            }{%
              \float@addtolists{%
                \protect\contentsline{chapteratlist}{\se@ds@tocentry}{\thepage}
                {\@currentHref}%
              }%
            }%
          }%
        \fi
      \fi
    \fi
    \expandafter\chaptermark\expandafter{\se@ds@head}%
    \ifdim \@chapterlistsgap>\z@
      \doforeachtocfile{%
        \iftocfeature{\@currext}{chapteratlist}{%
          \addtocontents{\@currext}{\protect\addvspace{\@chapterlistsgap}}%
        }{}%
      }%
      \@ifundefined{float@addtolists}{}{%
        \scr@float@addtolists@warning
        \float@addtolists{\protect\addvspace{\@chapterlistsgap}}%
      }%
    \fi
    \if@twocolumn
      \if@at@twocolumn
        \@makechapterhead{#2}%
      \else
        \@topnewpage[\@makechapterhead{#2}]%
      \fi
    \else
      \@makechapterhead{#2}%
      \@afterheading
    \fi
  }
\else
  \def\@tempa[#1]#2{%
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \refstepcounter{chapter}%
        \@maybeautodot\thechapter
        \typeout{\@chapapp\space\thechapter.}%
        \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}%
        \if@chaptertolists
          \doforeachtocfile{%
            \iftocfeature{\@currext}{chapteratlist}{%
              \addcontentsline{\@currext}{chapteratlist}{%
                \protect\numberline{\thechapter}#1}%
            }{}%
          }%
          \@ifundefined{float@addtolists}{}{%
            \scr@float@addtolists@warning
            \scr@ifundefinedorrelax{@currentHref}{%
              \float@addtolists{%
                \protect\contentsline{chapteratlist}{%
                  \protect\numberline{\thechapter}#1}{\thepage}%
              }%
            }{%
              \float@addtolists{%
                \protect\contentsline{chapteratlist}{%
                  \protect\numberline{\thechapter}#1}{\thepage}{\@currentHref}%
              }%
            }%
          }%
        \fi
      \else
        \addcontentsline{toc}{chapter}{#1}%
        \if@chaptertolists
          \doforeachtocfile{%
            \iftocfeature{\@currext}{chapteratlist}{%
              \addcontentsline{\@currext}{chapteratlist}{#1}%
            }{}%
          }%
          \@ifundefined{float@addtolists}{}{%
            \scr@float@addtolists@warning
            \scr@ifundefinedorrelax{@currentHref}{%
              \float@addtolists{%
                \protect\contentsline{chapteratlist}{#1}{\thepage}%
              }%
            }{%
              \float@addtolists{%
                \protect\contentsline{chapteratlist}{#1}{\thepage}{\@currentHref}%
              }%
            }%
          }%
        \fi
      \fi
    \else
      \addcontentsline{toc}{chapter}{#1}%
      \if@chaptertolists
        \doforeachtocfile{%
          \iftocfeature{\@currext}{chapteratlist}{%
            \addcontentsline{\@currext}{chapteratlist}{#1}%
          }{}%
        }%
        \@ifundefined{float@addtolists}{}{%
          \scr@float@addtolists@warning
          \scr@ifundefinedorrelax{@currentHref}{%
            \float@addtolists{%
              \protect\contentsline{chapteratlist}{#1}{\thepage}%
            }%
          }{%
            \float@addtolists{%
              \protect\contentsline{chapteratlist}{#1}{\thepage}{\@currentHref}%
            }%
          }%
        }%
      \fi
    \fi
    \chaptermark{#1}%
    \ifdim \@chapterlistsgap>\z@
      \doforeachtocfile{%
        \iftocfeature{\@currext}{chapteratlist}{%
          \addtocontents{\@currext}{\protect\addvspace{\@chapterlistsgap}}%
        }{}%
      }%
      \@ifundefined{float@addtolists}{}{%
        \scr@float@addtolists@warning
        \float@addtolists{\protect\addvspace{\@chapterlistsgap}}%
      }%
    \fi
    \if@twocolumn
      \if@at@twocolumn
        \@makechapterhead{#2}%
      \else
        \@topnewpage[\@makechapterhead{#2}]%
      \fi
    \else
      \@makechapterhead{#2}%
      \@afterheading
    \fi
  }
  \ifx\@tempa\@chapter
    % Using \@chapter definition of scrbook.cls
    \def\@chapter[#1]#2{%
      \se@istest#1=\@nil
      \if@se@tempswa
        \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#2},#1}%
      \else
        \ifcase \@se@osectarg\relax
          \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#1}}%
        \or
          \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#2}}%
        \or
          \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#1}}%
        \fi
      \fi
      \ifnum \c@secnumdepth >\m@ne
        \if@mainmatter
          \refstepcounter{chapter}%
          \@maybeautodot\thechapter
          \typeout{\@chapapp\space\thechapter.}%
          \ifx\se@ds@tocentry\@empty\else
            \addcontentsline{toc}{chapter}{%
              \protect\numberline{\thechapter}\se@ds@tocentry}%
            \if@chaptertolists
              \doforeachtocfile{%
                \iftocfeature{\@currext}{chapteratlist}{%
                  \addcontentsline{\@currext}{chapteratlist}{%
                    \protect\numberline{\thechapter}\se@ds@tocentry}%
                }{}%
              }%
              \@ifundefined{float@addtolists}{}{%
                \scr@float@addtolists@warning
                \scr@ifundefinedorrelax{@currentHref}{%
                  \float@addtolists{%
                    \protect\contentsline{chapteratlist}{%
                      \protect\numberline{\thechapter}\se@ds@tocentry}
                    {\thepage}%
                  }%
                }{%
                  \float@addtolists{%
                    \protect\contentsline{chapteratlist}{%
                      \protect\numberline{\thechapter}\se@ds@tocentry}
                    {\thepage}{\@currentHref}%
                  }%
                }%
              }%
            \fi
          \fi
        \else
          \ifx\se@ds@tocentry\@empty\else
            \addcontentsline{toc}{chapter}{\se@ds@tocentry}%
            \if@chaptertolists
              \doforeachtocfile{%
                \iftocfeature{\@currext}{chapteratlist}{%
                  \addcontentsline{\@currext}{chapteratlist}{\se@ds@tocentry}%
                }{}%
              }%
              \@ifundefined{float@addtolists}{}{%
                \scr@float@addtolists@warning
                \scr@ifundefinedorrelax{@currentHref}{%
                  \float@addtolists{%
                    \protect\contentsline{chapteratlist}{\se@ds@tocentry}
                    {\thepage}%
                  }%
                }{%
                  \float@addtolists{%
                    \protect\contentsline{chapteratlist}{\se@ds@tocentry}
                    {\thepage}{\@currentHref}%
                  }%
                }%
              }%
            \fi
          \fi
        \fi
      \else
        \ifx\se@ds@tocentry\@empty\else
          \addcontentsline{toc}{chapter}{\se@ds@tocentry}%
          \if@chaptertolists
            \doforeachtocfile{%
              \iftocfeature{\@currext}{chapteratlist}{%
                \addcontentsline{\@currext}{chapteratlist}{\se@ds@tocentry}%
              }{}%
            }%
            \@ifundefined{float@addtolists}{}{%
              \scr@float@addtolists@warning
              \scr@ifundefinedorrelax{@currentHref}{%
                \float@addtolists{%
                  \protect\contentsline{chapteratlist}{\se@ds@tocentry}
                  {\thepage}%
                }%
              }{%
                \float@addtolists{%
                  \protect\contentsline{chapteratlist}{\se@ds@tocentry}
                  {\thepage}{\@currentHref}%
                }%
              }%
            }%
          \fi
        \fi
      \fi
      \expandafter\chaptermark\expandafter{\se@ds@head}%
      \ifdim \@chapterlistsgap>\z@
        \doforeachtocfile{%
          \iftocfeature{\@currext}{chapteratlist}{%
            \addtocontents{\@currext}{\protect\addvspace{\@chapterlistsgap}}%
          }{}%
        }%
        \@ifundefined{float@addtolists}{}{%
          \scr@float@addtolists@warning
          \float@addtolists{\protect\addvspace{\@chapterlistsgap}}%
        }%
      \fi
      \if@twocolumn
        \if@at@twocolumn
          \@makechapterhead{#2}%
        \else
          \@topnewpage[\@makechapterhead{#2}]%
        \fi
      \else
        \@makechapterhead{#2}%
        \@afterheading
      \fi
    }
  \else
    \PackageWarning{screxperimental}{%
      Unexpected definition of \string\@chapter.\MessageBreak
      Extended \string\chapter\space handling deactivated%
    }%
  \fi
\fi

%% - \addchap[<tocandheadentry>]{Title}, 
%%   \addchap[tocentry={<tocentry>},head={<running head>}]{Title}
\def\@tempa[#1]#2{\@schapter{#2}%
  \addcontentsline{toc}{chapter}{#1}%
  \if@chaptertolists
    \doforeachtocfile{%
      \iftocfeature{\@currext}{chapteratlist}{%
        \addcontentsline{\@currext}{chapteratlist}{#1}%
      }{}%
    }%
    \@ifundefined{float@addtolists}{}{%
      \scr@float@addtolists@warning
      \scr@ifundefinedorrelax{@currentHref}{%
        \float@addtolists{%
          \protect\contentsline{chapteratlist}{#1}{\thepage}%
        }%
      }{%
        \float@addtolists{%
          \protect\contentsline{chapteratlist}{#1}{\thepage}{\@currentHref}%
        }%
      }%
    }%
  \fi
  \ifnum \@nameuse{scr@v@2.96}<\scr@compatibility\relax
    \ifdim \@chapterlistsgap>\z@
      \doforeachtocfile{%
        \iftocfeature{\@currext}{chapteratlist}{%
          \addtocontents{\@currext}{\protect\addvspace{\@chapterlistsgap}}%
        }{}%
      }%
      \@ifundefined{float@addtolists}{}{%
        \scr@float@addtolists@warning
        \float@addtolists{\protect\addvspace{\@chapterlistsgap}}%
      }%
    \fi
  \fi
  \if@twoside\@mkboth{#1}{}\else\@mkboth{}{#1}\fi
}%
\ifx\@tempa\@addchap
  \def\@addchap[#1]#2{\@schapter{#2}%
    \se@istest#1=\@nil
    \if@se@tempswa
      \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#2},#1}%
    \else
      \ifcase \@se@osectarg\relax
        \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#1}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#2}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#1}}%
      \fi
    \fi
    \ifx\se@ds@tocentry\@empty\else
      \addcontentsline{toc}{chapter}{\se@ds@tocentry}%
      \if@chaptertolists
        \doforeachtocfile{%
          \iftocfeature{\@currext}{chapteratlist}{%
            \addcontentsline{\@currext}{chapteratlist}{\se@ds@tocentry}%
          }{}%
        }%
        \@ifundefined{float@addtolists}{}{%
          \scr@float@addtolists@warning
          \scr@ifundefinedorrelax{@currentHref}{%
            \float@addtolists{%
              \protect\contentsline{chapteratlist}{\se@ds@tocentry}{\thepage}%
            }%
          }{%
            \float@addtolists{%
              \protect\contentsline{chapteratlist}{\se@ds@tocentry}{\thepage}
              {\@currentHref}%
            }%
          }%
        }%
      \fi
    \fi
    \ifnum \@nameuse{scr@v@2.96}<\scr@compatibility\relax
      \ifdim \@chapterlistsgap>\z@
        \doforeachtocfile{%
          \iftocfeature{\@currext}{chapteratlist}{%
            \addtocontents{\@currext}{\protect\addvspace{\@chapterlistsgap}}%
          }{}%
        }%
        \@ifundefined{float@addtolists}{}{%
          \scr@float@addtolists@warning
          \float@addtolists{\protect\addvspace{\@chapterlistsgap}}%
        }%
      \fi
    \fi
    \if@twoside
      \expandafter\@mkboth\expandafter{\se@ds@head}{}%
    \else
      \expandafter\@mkboth\expandafter{\expandafter}\expandafter{\se@ds@head}%
    \fi
  }%
\else
  \PackageWarning{screxperimental}{%
    Unexpected definition of \string\@addchap.\MessageBreak
    Extended \string\addchap\space handling deactivated%
  }%
\fi

%% - \section[<tocandheadentry>]{Title}, 
%%   \section[tocentry={<tocentry>},head={<running head>}]{Title}
%%   ...
\def\@tempa#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
  \fi
  \@xsect{#5}}
\ifx\scr@sect\@tempa
  \def\scr@sect#1#2#3#4#5#6[#7]#8{%
    \typeout{Using extended \string\scr@sect}%
    \se@istest#7=\@nil
    \if@se@tempswa
      \setkeys{screxperimentaldocstruct}{tocentry={#8},head={#8},#7}%
    \else
      \ifcase \@se@osectarg\relax
        \setkeys{screxperimentaldocstruct}{tocentry={#8},head={#8}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#7},head={#8}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#8},head={#7}}%
      \fi
    \fi
    \ifnum #2>\c@secnumdepth
      \let\@svsec\@empty
    \else
      \refstepcounter{#1}%
      \protected@edef\@svsec{\@seccntformat{#1}\relax}%
    \fi
    \@tempskipa #5\relax
    \ifdim \@tempskipa>\z@
      \begingroup
        #6{%
          \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
      \endgroup
      \typeout{head=`\se@ds@head'}%
      \expandafter\csname #1mark\expandafter\endcsname\expandafter{\se@ds@head}%
      \typeout{tocentry=`\se@ds@tocentry'}%
      \ifx\se@ds@tocentry\@empty\else
        \addcontentsline{toc}{#1}{%
          \ifnum #2>\c@secnumdepth \else
            \protect\numberline{\csname the#1\endcsname}%
          \fi
          \se@ds@tocentry}%
      \fi
    \else
      \def\@svsechd{%
        #6{\hskip #3\relax
          \@svsec #8}%
        \typeout{head=`\se@ds@head'}%
        \expandafter\csname #1mark\expandafter\endcsname\expandafter{\se@ds@head}%
        \typeout{tocentry=`\se@ds@tocentry'}%
        \ifx\se@ds@tocentry\@empty\else
          \addcontentsline{toc}{#1}{%
            \ifnum #2>\c@secnumdepth \else
              \protect\numberline{\csname the#1\endcsname}%
            \fi
            \se@ds@tocentry}%
        \fi
      }%
    \fi
    \@xsect{#5}%
  }
\else
  \PackageWarning{screxperimental}{%
    Unexpected definition of \string\scr@sect.\MessageBreak
    Extended \string\section, \string\subsection\space
    ... handling\MessageBreak
    deactivated%
  }%
\fi

%% - \addsec[<tocandheadentry>]{Title}, 
%%   \addsec[tocentry={<tocentry>},head={<running head>}]{Title}
\def\@tempa[#1]#2{\section*{#2}\addcontentsline{toc}{section}{#1}
  \if@twoside\ifx\@mkboth\markboth\markright{#1}\fi\fi
}
\ifx\@tempa\@addsec
  % Using \@addsec definition of scrbook or scrreprt
  \def\@addsec[#1]#2{%
    \section*{#2}%
    \se@istest#1=\@nil
    \if@se@tempswa
      \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#2},#1}%
    \else
      \ifcase \@se@osectarg\relax
        \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#1}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#2}}%
      \or
        \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#1}}%
      \fi
    \fi
    \ifx\se@ds@tocentry\@empty\else
      \addcontentsline{toc}{section}{\se@ds@tocentry}%
    \fi
    \if@twoside\ifx\@mkboth\markboth
        \expandafter\markright\expandafter{\se@ds@head}%
    \fi\fi
  }
\else
  \def\@tempa[#1]#2{\section*{#2}\addcontentsline{toc}{section}{#1}
    \if@twoside \@mkboth{#1}{}\else \@mkboth{#1}{#1}\fi
  }
  \ifx\@tempa\@addsec
    % Using \@addsec definition of scrartcl
    \def\@addsec[#1]#2{\section*{#2}%
      \se@istest#1=\@nil
      \if@se@tempswa
        \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#2},#1}%
      \else
        \ifcase \@se@osectarg\relax
          \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#1}}%
        \or
          \setkeys{screxperimentaldocstruct}{tocentry={#1},head={#2}}%
        \or
          \setkeys{screxperimentaldocstruct}{tocentry={#2},head={#1}}%
        \fi
      \fi
      \ifx\se@ds@tocentry\@empty\else
        \addcontentsline{toc}{section}{\se@ds@tocentry}%
      \fi
      \if@twoside 
        \expandafter\@mkboth\expandafter{#1}{}%
      \else 
        \expandafter\expandafter\expandafter\@mkboth
        \expandafter\expandafter\expandafter{\expandafter#1\expandafter}%
        \expandafter{#1}%
      \fi
    }
  \else
    \PackageWarning{screxperimental}{%
      Unexpected definition of \string\@addsec.\MessageBreak
      Extended \string\addsec\space handling deactivated%
    }%
  \fi
\fi

%% - some keys and helpers, that were used above
\DefineFamily{screxperimental}
\DefineFamilyMember[docstruct]{screxperimental}
\DefineFamilyKey[docstruct]{screxperimental}{tocentry}{%
  \def\se@ds@tocentry{#1}}
\DefineFamilyKey[docstruct]{screxperimental}{head}{%
  \def\se@ds@head{#1}}
\def\se@istest#1=#2\@nil{%
  \ifx\relax#2\relax\@se@tempswafalse\else\@se@tempswatrue\fi
}
\endinput
%% end of file `screxperimental.sty'
