% \iffalse meta-comment
% ======================================================================
% standardsectioning.dtx
% Copyright © 2008–2024 Markus Kohm
%
% This work is a KOMA-Script spin-off.  For the original sources of
% KOMA-Script's `float.hak' see file `scrhack.dtx' in the KOMA-Script
% sources at <https://sourceforge.net/p/koma-script/code/>.
%
% Development is taking place as part of `scrhack' at
% <https://github.com/komascript/third-party-enhancements>.  New issues
% should be reported there as well as known issues can be found.
%
% This work 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 "maintained".
%
% The Current Maintainer and author of this work is Markus Kohm.
%
% This work consists of the files `standardsectioning.dtx' and
% `README.md'.
%
% This work also uses code derived from the standard LaTeX classes.
% These classes have been released under the LaTeX Project Public
% License.  For the copyright of the original code see `classes.dtx'
% <https://ctan.org/tex-archive/macros/latex/base/classes.dtx>.
% Note: For support of the derived code, do not contact the authors of
%       the original code!  They are not responsible for this work.
%
% The recommended way to install `standardsectioning' is to use
% the package manager of your TeX distribution.
% ======================================================================
%
%<*dtx>
\ifx\ProvidesExplFile\undefined\def\ProvidesExplFile#1#2#3#4{}\fi
\ProvidesExplFile{standardsectioning.dtx}
%</dtx>
%<*dtx|package>
%<package>\ProvidesExplPackage{standardsectioning}
  {2024-06-06}{0.9}
  {%
%<*dtx>
    sources and unpack driver of
%</dtx>
    forcing standard class sectioning definitions%
  }
%</dtx|package>
%<*dtx>
\ifx\documentclass\undefined
  \input docstrip.tex
  \generate{%
    \file{standardsectioning.sty}{%
      \from{standardsectioning.dtx}{package}%
    }%
  }%
\else
  \let\endbatchfile\ExplSyntaxOff
\fi
\endbatchfile
\documentclass[ngerman,USenglish]{koma-script-source-doc}
\NewDocElement[%
  macrolike = true,
  toplevel  = true,
  idxtype   = \textit{var.},
  idxgroup  = Variable,
  printtype = \textit{var.},
]{Variable}{Variable}
\setcounter{IndexColumns}{2}  
\usepackage{babel}
\usepackage{csquotes}
\usepackage[style=alphabetic]{biblatex}
\begin{filecontents}[force]{\jobname.bib}
@manual{pkg:scrhack:manual,
  author={Markus Kohm},
  version={3.42},
  date={2023-07-26},
  title={Emulating Former \KOMAScript{} Package \pkg*{scrhack}},
  url={https://mirrors.ctan.org/macros/latex/contrib/scrhack/scrhack.pdf},
  urldate={2023-07-26},
}
@online{pkg:koma-script,
  author={Markus Kohm},
  version={3.41},
  date={2023-07-07},
  title={{\KOMAScript} --- A bundle of versatile classes and packages},
  url={https://ctan.org/pkg/koma-script},
  urldate={2023-07-14},
  note={The \KOMAScript{} bundle provides replacements for the \pkg*{article},
        \pkg*{report}, and \pkg*{book} classes with emphasis on typography and
        versatility. There is also a letter class.},
}
@manual{pkg:koma-script:manual:de,
  author={Markus Kohm},
  date={2023-06-16},
  title={{\KOMAScript}},
  subtitle={Die Anleitung},
  url={http://mirrors.ctan.org/macros/latex/contrib/koma-script/scrguide-de.pdf},
  urldate={2023-07-04},
}
@manual{pkg:koma-script:manual:en,
  author={Markus Kohm},
  date={2023-06-16},
  title={{\KOMAScript}},
  subtitle={The Guide},
  url={http://mirrors.ctan.org/macros/latex/contrib/koma-script/scrguide-en.pdf},
  urldate={2023-07-14},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\setcounter{StandardModuleDepth}{2}
\begin{document}
  \nocite{pkg:koma-script,pkg:koma-script:manual:en,pkg:koma-script:manual:de}
  \DocInput{standardsectioning.dtx}
\end{document}
%</dtx>
%\fi
%
% \changes{v0.1}{2023/06/01}{start of \KOMAScript{} spin-off}
% \changes{v0.9}{2024/06/06}{first release}
%
% \GetFileInfo{standardsectioning.dtx}
% \title{Using the Sectioning Command Code of the Standard Classes}
% \author{\href{mailto:komascript@gmx.info}{Markus Kohm}}
% \date{Version \filedate{} \fileversion}
% \maketitle
% \begin{abstract}
%   A long time ago
%   \href{https://www.sourceforge.net/project/koma-script}{\KOMAScript} was
%   more or less just a collection of replacements for the three standard
%   classes \cls*{article}, \cls*{book} and \cls*{report}. Replacing a
%   standard class with a
%   \href{https://www.sourceforge.net/project/koma-script}{\KOMAScript} class
%   was easy, even if users used a lot of additional packages. Over the years,
%   the author has added many new features and user interfaces to the
%   classes. This also led to completely different implementation of the
%   sectioning commands. But some packages depend on an implementation similar
%   to the standard classes.
%
%   Package \pkg*{standardsectioning} implements these commands with the code
%   of the standard classes and also deactivates parts of the
%   \href{https://www.sourceforge.net/project/koma-script}{\KOMAScript} user
%   interface, which should not be used with these changes. These are more or
%   less all commands to configure sectioning commands. It also reverts some
%   internal macros of
%   \href{https://www.sourceforge.net/project/koma-script}{\KOMAScript}.
% \end{abstract}
%
% \tableofcontents
%
%\iffalse
%<*doc>
%\fi
\section{What are the typical reasons for using package
  \texorpdfstring{\pkg*{standardsectioning}}{standardsectioning} and how to do
  it?}
\label{sec:reasons}

Sometimes replacing a standard class by a
\href{https://komascript.de/}{\KOMAScript} class results in new errors,
because of incompatibility of some of the packages or the already existing
preamble code with these classes. In such a case, you first should try out
package \pkg{scrhack} without passing any option to that package. Package
\pkg{scrhack} should be the first package after loading the document class, or
even be loaded \emph{before} the document class using \cs{RequirePackage}
instead of \cs{usepackage}. If this already fixes the issue, you can try to
find out which of the replacements and enhancements loaded by \pkg{scrhack}
solved the issue and load only these.

Only if you cannot fix the issue this kind you can try loading
\pkg*{standardsectioning} either also using \pkg{scrhack} with
corresponding options (see \cite{pkg:scrhack:manual}) or just loading
\pkg*{standardsectioning}.

Such errors happen, e.g., because
\pkg{titlesec}\index{Packages:>\pkg{titlesec}} makes assumptions about the
definition of the sectioning commands that simply do not apply to the
\href{https://komascript.de/}{\KOMAScript} classes. This leads, e.g., in the
following example:
\begin{verbatim}
  \documentclass{scrartcl}
  \usepackage{titlesec}
  \begin{document}
  \section{Test Section}
  Test text.
  \end{document}
\end{verbatim}
to the error message:
\begin{verbatim}
  Package titlesec Error: No format for this command.

  See the titlesec package documentation for explanation.
  Type  H <return>  for immediate help.
   ...                                              

  l.4 \section{Test Section}
\end{verbatim}
Several other errors can happen with combinations of \pkg{titlesec} and
\href{https://komascript.de/}{\KOMAScript} classes. Best solution for this,
would be to not use \pkg{titlesec} and instead use the class' interface to
configure the sectioning commands, e.g., \cs{RedeclareSectionCommand},
\cs{setkomafont}, or redefining \cs{sectionlinesformat}. See
\cite{pkg:koma-script:manual:en} or \cite{pkg:koma-script:manual:de} for more
information. But if you do not use any of the enhanced sectioning features of
the \href{https://komascript.de/}{\KOMAScript} classes, a first-aid workaround
would be to load package \pkg*{standardsectioning} immediately \emph{after}
the class:
\begin{verbatim}
  \documentclass{scrartcl}
  \usepackage{standardsectioning}
  \usepackage{titlesec}
  \begin{document}
  \section{Test Section}
  Test text.
  \end{document}
\end{verbatim}
Among other things, this redefines the sectioning commands to be compatible
with the standard classes.
\begin{description}\sloppy
\item[But note:] \leavevmode
  \marginline{\raisebox{\dimexpr\ht\strutbox-\height\relax}{\Huge\textcolor{red}{!}}}%
  This also means, that all the enhancements to these commands are broken! You
  cannot use \cs{DeclareSectionCommand}, \cs{RedeclareSectionCommand},
  \cs{DeclareNewSectionCommand}, \cs{ProvideSectionCommand},
  \cs{DeclareSectionCommands}, \cs{RedeclareSectionCommands},
  \cs{DeclareNewSectionCommands}, or \cs{ProvideSectionCommands} any
  longer. Other commands like \cs{partformat}, \cs{chapterformat},
  \cs{sectionformat}, \cs{subsectionformat}, \cs{subsubsectionformat},
  \cs{paragraphformat}, \cs{subparagraphformat} and many more are not used
  anymore.
\end{description}

Sometimes a wrapper classes loads a \href{https://komascript.de/}{\KOMAScript}
class and than also loads a package like \pkg{titlesec}. To load
\pkg*{standardsections} immediately after the class, you should use the
generic class hook available from \LaTeX{} 2020-10-01:
\begin{verbatim}
  \AddToHook{class/scrartcl/after}{\RequirePackage{standardsectioning}}
  \AddToHook{class/scrbook/after}{\RequirePackage{standardsectioning}}
  \AddToHook{class/scrreprt/after}{\RequirePackage{standardsectioning}}
\end{verbatim}
These lines should be placed \texttt{before} \cs{documentclass}. If you are
using an older \LaTeX{} version you can use
\href{https://komascript.de/}{\KOMAScript} package \pkg{scrlfile} to do the
same:
\begin{verbatim}
  \RequirePackage{scrlfile}
  \AfterClass{scrartcl}{\RequirePackage{standardsectioning}}
  \AfterClass{scrbook}{\RequirePackage{standardsectioning}}
  \AfterClass{scrreport}{\RequirePackage{standardsectioning}}
\end{verbatim}
also \emph{before} loading the class.

\section{How to select the correct sectioning code, if the automatism fails?}
\label{sec:options}

By default, the package uses an automatism to decide which code of which
standard class should be used. If the \KOMAScript{} class \cls{scrartcl} is
detected, automatically the code of \cls{article} is used. If the
\KOMAScript{} class \cls{scrreprt} is detected, automatically the code of
\cls{report} is used. If the \KOMAScript{} class \cls{scrbook} is detected,
automatically the code of \cls{book} is used. If none of these classes is
detected, the existence of two commands is used. If \cs{chapter} is not
defined, the code of \cls{article} is used. If \cs{chapter} is defined, but
\cs{frontmatter} is not defined, the code of \cls{report} is used. If both
\cs{chapter} and \cs{frontmatter} are defined, the code of \cls{book} is used.

%\DescribeOption{article}
%\DescribeOption{book}
%\DescribeOption{report}
Very seldom this automatism fails and the wrong code is used, which results in
error messages or printing mistakes. In this case you can load package
\pkg*{standardsectioning} with one of the options \opt{article}, \opt{report}
or \opt{book} to force the usage of the sectioning code of either
\cls{article}, \cls{report}, or \cls{book}. This could, e.g., be needed, if
you are using a class, that defines \cs{chapter} and \cs{frontmatter} but not
\cs{if@mainmatter}. In this case, you should use
\begin{verbatim}
  \usepackage[report]{standardsectioning}
\end{verbatim}
instead of
\begin{verbatim}
  \usepackage{standardsectioning}
\end{verbatim}
because otherwise the not defined \cs{if@mainmatter} will result in errors,
when using \cs{chapter}.
% \iffalse
%</doc>
% \fi
%
% \MaybeStop{\printbibliography[heading=bibintoc]\PrintIndex}
%
% \iffalse
%<*package>
%<@@=standardsectioning>
% \fi
% \section{Implementation}
% \label{sec:implementation}
%
% \subsection{Messages}
% \label{ssec:messages}
%
% There are some messages, that may be used several times:
%    \begin{macrocode}
\msg_new:nnnn { standardsectioning } { wrong-package-order }
  {
    wrong~package~order~detected.
  }
  {
    You've~loaded~this~package~after~package~#1.\iow_newline:
    But~this~cannot~work.\iow_newline:
    Loading aborted!
  }

\msg_new:nnn { standardsectioning } { deactivate-extended-section-commands }
  {
    extended~sectioning~option~handling~deactivated.
  }

\msg_new:nnn { standardsectioning } { no-force-with-KOMA-Script-class }
  {
    package~option~`#1'~is~ignored~due~to~usage~of~KOMA-Script~class~\KOMAClassName.
  }
%    \end{macrocode}
%
% \subsection{Package order}
% \label{ssec:packageorder}
%
% \pkg{hyperref} must not be loaded, because otherwise deactivation of
% \cs{scr@chapter@after@hyperref@patch} would not work.
%    \begin{macrocode}
\@ifpackageloaded { hyperref }
  {
    \msg_critical:nnn { standardsectioning } { wrong-package-order }
                      { hyperref }
  } { }
%    \end{macrocode}
% \pkg{titlesec} must not be loaded, because otherwise the re-definitions
% would overwrite the definitions of this package.
%    \begin{macrocode}
\@ifpackageloaded { titlesec }
  {
    \msg_critical:nnn { standardsectioning } { wrong-package-order }
                      { titlesec }
  } { }
%    \end{macrocode}
% \pkg{sectsty} must not be loaded, because otherwise the re-definitions
% would overwrite the definitions of this package.
%    \begin{macrocode}
\@ifpackageloaded { sectsty }
  {
    \msg_critical:nnnn { standardsectioning } { wrong-package-order }
                       { sectsty }
  } { }
%    \end{macrocode}
%
% \subsection{\KOMAScript{} deactivation code}
% \label{ssec:deactivationcode}
%
% \ExplSyntaxOn
% \begin{macro}{\@@_common_komascript_deactivation_code:}
% \ExplSyntaxOff
% Some deactivation code is common for all \KOMAScript{} classes and
% therefore we use an auxiliary function to not need to copy it several
% times.
%    \begin{macrocode}
\cs_new:Nn \@@_common_komascript_deactivation_code:
  {
%    \end{macrocode}
% \begin{macro}{\@startsection}
% \KOMAScript{} classes redefine this internal \LaTeX{} kernel macro. In this
% case \cs{scr@saved@startsection} is defined, differs from
% \cs{@startsection} and has the definition found before redefining the
% original macro. We do nothing else but resetting \cs{@startsection} to this
% saved definition.
%    \begin{macrocode}
    \RenewCommandCopy \@startsection \scr@saved@startsection
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@sect,\@ssect,\@xsect}
% All these should be resetted to their original definitions stored in
% \cs{scr@latex@\dots}.
%    \begin{macrocode}
    \RenewCommandCopy\@sect\scr@latex@sect
    \RenewCommandCopy\@ssect\scr@latex@ssect
    \RenewCommandCopy\@xsect\scr@latex@xsect
%    \end{macrocode}
% \end{macro}
% \begin{command}{\DeclareSectionCommand,\RedeclareSectionCommand,
%                 \DeclareNewSectionCommand,\ProvideSectionCommand,
%                 \DeclareSectionCommands,\RedeclareSectionCommands,
%                 \DeclareNewSectionCommands,\ProvideSectionCommands}
% These \KOMAScript{} commands do not make sense any longer after forced
% redefinition of \cs{part} \dots{} \cs{subparagraph}. They even would not
% work correctly after resetting, e.g., \cs{@startsection}. So best reaction
% is to \emph{undefine} them.
%    \begin{macrocode}
    \cs_undefine:N \DeclareSectionCommand
    \cs_undefine:N \DeclareSectionCommands
    \cs_undefine:N \RedeclareSectionCommand
    \cs_undefine:N \RedeclareSectionCommands
    \cs_undefine:N \DeclareNewSectionCommand
    \cs_undefine:N \DeclareNewSectionCommands
    \cs_undefine:N \ProvideSectionCommand
    \cs_undefine:N \ProvideSectionCommands
%    \end{macrocode}
% \end{command}
% \begin{macro}{\minisec}
% This also has to be undefined, because it also uses internal \KOMAScript{}
% definitions, which are deactivated.
%    \begin{macrocode}
    \cs_undefine:N \minisec
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\scr@chapter@before@hyperref@patch,
%               \scr@chapter@after@hyperref@patch}
% Now, definitions without respecting \pkg{hyperref} are used, so
% \pkg{hyperref} should again be able to patch them. So the hack should
% deactivate the patch deactivation of the \KOMAScript{} classes.
%    \begin{macrocode}
    \cs_gset_eq:NN \scr@chapter@before@hyperref@patch \relax
    \cs_gset_eq:NN \scr@chapter@after@hyperref@patch \relax
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\scr@osectarg}
% The \KOMAScript{} classes use this macro to store how to use the optional
% argument of section commands. But with the standard class definition the
% only possibility is to use it as running head and write it into the table of
% contents without any extended handling. So the only value, that makes sense
% is 0.
%    \begin{macrocode}
    \int_if_zero:nF { \scr@osectarg }
      {
        \msg_warning:nn { standardsectioning }
                        { deactivate-extended-section-commands }
        \cs_gset_eq:NN \scr@osectarg \c_zero_int
      }
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\scr@activate@xsection}
% Further changing of the value has to be deactivated.
%    \begin{macrocode}
    \renewcommand*{\scr@activate@xsection}[1]{%
      \int_if_zero:nF { ##1 }
        {
          \msg_warning:nn { standardsectioning }
                          { deactivate-extended-section-commands }
          \cs_gset_eq:NN \scr@osectarg \c_zero_int
        }
    }%
%    \end{macrocode}
% \end{macro}
% \begin{command}{\partlineswithprefixformat,
%                 \sectionlinesformat,\sectioncatchphraseformat}
% These \KOMAScript{} commands are not used any longer. To detect errors,
% because of redefining them, we undefine them.
%    \begin{macrocode}
    \cs_undefine:N \partlineswithprefixformat
    \cs_undefine:N \sectionlinesformat
    \cs_undefine:N \sectioncatchphraseformat
%    \end{macrocode}
% \end{command}
% \begin{option}{headings}
% The \KOMAScript{} option does not make sense any longer. So it is
% deactivated and usage will throw an error.
% \begin{macrocode}
    \RelaxFamilyKey[.\KOMAClassFileName]{KOMA}{headings}%
    \KOMA@kav@removekey{\KOMAClassFileName}{headings}%
%    \end{macrocode}
% \end{option}
% \begin{macro}{\scr@class@titlesec@warning}
% The warning about loading \pkg{titlesec} has to be removed, because we do
% not expect errors any longer.
%    \begin{macrocode}
    \cs_undefine:N \scr@class@titlesec@warning
%    \end{macrocode}
%    \begin{macrocode}
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \ExplSyntaxOff
%
% \subsection{Sectioning code of the standard classes}
% \label{ssec:standardsectioningcommands}
%
% \begin{command}{\part,\chapter,\section,\subsection,\subsubsection,
%   \paragraph,\subparagraph}
% These commands are redefined using code from
% \href{https://ctan.org/tex-archive/macros/latex/base/classes.dtx}
%      {\file{classes.dtx}}
% 2024/02/08 v1.4n.
% Only \cs{newcommand} is replaced by \cs{def} and \texttt{\#} are
% doubled. Depending on the class, we use different code:
% \ExplSyntaxOn
% \begin{macro}{\@@_article_sectioning_code:,\@@_book_sectioning_code:,
%               \@@_report_sectioning_code:}
% \ExplSyntaxOff
%    \begin{macrocode}
\cs_new:Nn \@@_article_sectioning_code:
  {
    \def\part{%
      \if@noskipsec \leavevmode \fi
      \par
      \addvspace{4ex}%
      \@afterindentfalse
      \secdef\@part\@spart}%
    \def\@part[##1]##2{%
      \ifnum \c@secnumdepth >\m@ne
        \refstepcounter{part}%
        \addcontentsline{toc}{part}{\thepart\hspace{1em}##1}%
      \else
        \addcontentsline{toc}{part}{##1}%
      \fi
      {\parindent \z@ \raggedright
       \interlinepenalty \@M
       \normalfont
       \ifnum \c@secnumdepth >\m@ne
         \Large\bfseries \partname\nobreakspace\thepart
         \par\nobreak
       \fi
       \huge \bfseries ##2%
       \markboth{}{}\par}%
      \nobreak
      \vskip 3ex
      \@afterheading}%
    \def\@spart##1{%
      {\parindent \z@ \raggedright
        \interlinepenalty \@M
        \normalfont
        \huge \bfseries ##1\par}%
      \nobreak
      \vskip 3ex
      \@afterheading}%

    \def\section{\@startsection {section}{1}{\z@}%
                                       {-3.5ex \@plus -1ex \@minus -.2ex}%
                                       {2.3ex \@plus.2ex}%
                                       {\normalfont\Large\bfseries}}%
    \def\subsection{\@startsection{subsection}{2}{\z@}%
                                         {-3.25ex\@plus -1ex \@minus -.2ex}%
                                         {1.5ex \@plus .2ex}%
                                         {\normalfont\large\bfseries}}%
    \def\subsubsection{\@startsection{subsubsection}{3}{\z@}%
                                         {-3.25ex\@plus -1ex \@minus -.2ex}%
                                         {1.5ex \@plus .2ex}%
                                         {\normalfont\normalsize\bfseries}}%
    \def\paragraph{\@startsection{paragraph}{4}{\z@}%
                                        {3.25ex \@plus1ex \@minus.2ex}%
                                        {-1em}%
                                        {\normalfont\normalsize\bfseries}}%
    \def\subparagraph{\@startsection{subparagraph}{5}{\parindent}%
                                           {3.25ex \@plus1ex \@minus .2ex}%
                                           {-1em}%
                                          {\normalfont\normalsize\bfseries}}%
  }

\cs_new:Nn \@@_report_sectioning_code:
  {
    \def\part{%
      \if@openright
        \cleardoublepage
      \else
        \clearpage
      \fi
      \thispagestyle{plain}%
      \if@twocolumn
        \onecolumn
        \@tempswatrue
      \else
        \@tempswafalse
      \fi
      \null\vfil
      \secdef\@part\@spart}

    \def\@part[##1]##2{%
        \ifnum \c@secnumdepth >-2\relax
          \refstepcounter{part}%
          \addcontentsline{toc}{part}{\thepart\hspace{1em}##1}%
        \else
          \addcontentsline{toc}{part}{##1}%
        \fi
        \markboth{}{}%
        {\centering
         \interlinepenalty \@M
         \normalfont
         \ifnum \c@secnumdepth >-2\relax
           \huge\bfseries \partname\nobreakspace\thepart
           \par
           \vskip 20\p@
         \fi
         \Huge \bfseries ##2\par}%
        \@endpart}
    \def\@spart##1{%
        {\centering
         \interlinepenalty \@M
         \normalfont
         \Huge \bfseries ##1\par}%
        \@endpart}
    \def\@endpart{\vfil\newpage
                  \if@twoside
                   \if@openright
                    \null
                    \thispagestyle{empty}%
                    \newpage
                   \fi
                  \fi
                  \if@tempswa
                    \twocolumn
                  \fi}
    \def\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                        \thispagestyle{plain}%
                        \global\@topnum\z@
                        \@afterindentfalse
                        \secdef\@chapter\@schapter}
    \def\@chapter[##1]##2{\ifnum \c@secnumdepth >\m@ne
                             \refstepcounter{chapter}%
                             \typeout{\@chapapp\space\thechapter.}%
                             \addcontentsline{toc}{chapter}%
                                       {\protect\numberline{\thechapter}##1}%
                        \else
                          \addcontentsline{toc}{chapter}{##1}%
                        \fi
                        \chaptermark{##1}%
                        \addtocontents{lof}{\protect\addvspace{10\p@}}%
                        \addtocontents{lot}{\protect\addvspace{10\p@}}%
                        \if@twocolumn
                          \@topnewpage[\@makechapterhead{##2}]%
                        \else
                          \@makechapterhead{##2}%
                          \@afterheading
                        \fi}
    \def\@makechapterhead##1{%
      \vspace*{50\p@}%
      {\parindent \z@ \raggedright \normalfont
        \ifnum \c@secnumdepth >\m@ne
            \huge\bfseries \@chapapp\space \thechapter
            \par\nobreak
            \vskip 20\p@
        \fi
        \interlinepenalty\@M
        \Huge \bfseries ##1\par\nobreak
        \vskip 40\p@
      }}
    \def\@schapter##1{\if@twocolumn
                       \@topnewpage[\@makeschapterhead{##1}]%
                     \else
                       \@makeschapterhead{##1}%
                       \@afterheading
                     \fi}
    \def\@makeschapterhead##1{%
      \vspace*{50\p@}%
      {\parindent \z@ \raggedright
        \normalfont
        \interlinepenalty\@M
        \Huge \bfseries  ##1\par\nobreak
        \vskip 40\p@
      }}
    \def\section{\@startsection {section}{1}{\z@}%
                                       {-3.5ex \@plus -1ex \@minus -.2ex}%
                                       {2.3ex \@plus.2ex}%
                                       {\normalfont\Large\bfseries}}
    \def\subsection{\@startsection{subsection}{2}{\z@}%
                                         {-3.25ex\@plus -1ex \@minus -.2ex}%
                                         {1.5ex \@plus .2ex}%
                                         {\normalfont\large\bfseries}}
    \def\subsubsection{\@startsection{subsubsection}{3}{\z@}%
                                         {-3.25ex\@plus -1ex \@minus -.2ex}%
                                         {1.5ex \@plus .2ex}%
                                         {\normalfont\normalsize\bfseries}}
    \def\paragraph{\@startsection{paragraph}{4}{\z@}%
                                        {3.25ex \@plus1ex \@minus.2ex}%
                                        {-1em}%
                                        {\normalfont\normalsize\bfseries}}
    \def\subparagraph{\@startsection{subparagraph}{5}{\parindent}%
                                           {3.25ex \@plus1ex \@minus .2ex}%
                                           {-1em}%
                                          {\normalfont\normalsize\bfseries}}
  }

\cs_new:Nn \@@_book_sectioning_code:
  {
    \def\part{%
      \if@openright
        \cleardoublepage
      \else
        \clearpage
      \fi
      \thispagestyle{plain}%
      \if@twocolumn
        \onecolumn
        \@tempswatrue
      \else
        \@tempswafalse
      \fi
      \null\vfil
      \secdef\@part\@spart}

    \def\@part[##1]##2{%
        \ifnum \c@secnumdepth >-2\relax
          \refstepcounter{part}%
          \addcontentsline{toc}{part}{\thepart\hspace{1em}##1}%
        \else
          \addcontentsline{toc}{part}{##1}%
        \fi
        \markboth{}{}%
        {\centering
         \interlinepenalty \@M
         \normalfont
         \ifnum \c@secnumdepth >-2\relax
           \huge\bfseries \partname\nobreakspace\thepart
           \par
           \vskip 20\p@
         \fi
         \Huge \bfseries ##2\par}%
        \@endpart}
    \def\@spart##1{%
        {\centering
         \interlinepenalty \@M
         \normalfont
         \Huge \bfseries ##1\par}%
        \@endpart}
    \def\@endpart{\vfil\newpage
                  \if@twoside
                   \if@openright
                    \null
                    \thispagestyle{empty}%
                    \newpage
                   \fi
                  \fi
                  \if@tempswa
                    \twocolumn
                  \fi}
    \def\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                        \thispagestyle{plain}%
                        \global\@topnum\z@
                        \@afterindentfalse
                        \secdef\@chapter\@schapter}
    \def\@chapter[##1]##2{\ifnum \c@secnumdepth >\m@ne
                           \if@mainmatter
                             \refstepcounter{chapter}%
                             \typeout{\@chapapp\space\thechapter.}%
                             \addcontentsline{toc}{chapter}%
                                       {\protect\numberline{\thechapter}##1}%
                           \else
                             \addcontentsline{toc}{chapter}{##1}%
                           \fi
                        \else
                          \addcontentsline{toc}{chapter}{##1}%
                        \fi
                        \chaptermark{##1}%
                        \addtocontents{lof}{\protect\addvspace{10\p@}}%
                        \addtocontents{lot}{\protect\addvspace{10\p@}}%
                        \if@twocolumn
                          \@topnewpage[\@makechapterhead{##2}]%
                        \else
                          \@makechapterhead{##2}%
                          \@afterheading
                        \fi}
    \def\@makechapterhead##1{%
      \vspace*{50\p@}%
      {\parindent \z@ \raggedright \normalfont
        \ifnum \c@secnumdepth >\m@ne
          \if@mainmatter
            \huge\bfseries \@chapapp\space \thechapter
            \par\nobreak
            \vskip 20\p@
          \fi
        \fi
        \interlinepenalty\@M
        \Huge \bfseries ##1\par\nobreak
        \vskip 40\p@
      }}
    \def\@schapter##1{\if@twocolumn
                       \@topnewpage[\@makeschapterhead{##1}]%
                     \else
                       \@makeschapterhead{##1}%
                       \@afterheading
                     \fi}
    \def\@makeschapterhead##1{%
      \vspace*{50\p@}%
      {\parindent \z@ \raggedright
        \normalfont
        \interlinepenalty\@M
        \Huge \bfseries  ##1\par\nobreak
        \vskip 40\p@
      }}
    \def\section{\@startsection {section}{1}{\z@}%
                                       {-3.5ex \@plus -1ex \@minus -.2ex}%
                                       {2.3ex \@plus.2ex}%
                                       {\normalfont\Large\bfseries}}
    \def\subsection{\@startsection{subsection}{2}{\z@}%
                                         {-3.25ex\@plus -1ex \@minus -.2ex}%
                                         {1.5ex \@plus .2ex}%
                                         {\normalfont\large\bfseries}}
    \def\subsubsection{\@startsection{subsubsection}{3}{\z@}%
                                         {-3.25ex\@plus -1ex \@minus -.2ex}%
                                         {1.5ex \@plus .2ex}%
                                         {\normalfont\normalsize\bfseries}}
    \def\paragraph{\@startsection{paragraph}{4}{\z@}%
                                        {3.25ex \@plus1ex \@minus.2ex}%
                                        {-1em}%
                                        {\normalfont\normalsize\bfseries}}
    \def\subparagraph{\@startsection{subparagraph}{5}{\parindent}%
                                           {3.25ex \@plus1ex \@minus .2ex}%
                                           {-1em}%
                                          {\normalfont\normalsize\bfseries}}
  }
%    \end{macrocode}
% \end{macro}
% \end{command}
%
% \subsection{Class dependent deactivation and implemention}
% \label{ssec:doit}
%
% If a \KOMAScript{} class has been loaded we need use the common deactivation
% code and the \cls*{article} sectioning code.
% \ExplSyntaxOn
% \begin{macro}{\@@_acticvation_code:}
% \ExplSyntaxOff
%    \begin{macrocode}
\cs_new:Nn \@@_activation_code:
  {
    \@ifclassloaded { scrartcl }
      {
        \@@_common_komascript_deactivation_code:
        \@@_article_sectioning_code:
      }
      {
%    \end{macrocode}
% \begin{command}{\chapterlineswithprefixformat,\chapterlinesformat}
% For \cls*{scrbook} and \cls*{scrreprt} we need additional deactivation
% code and the \cls*{book} resp. \cls*{report} sectioning code.
%    \begin{macrocode}
        \@ifclassloaded { scrbook }
          {
            \@@_common_komascript_deactivation_code:
            \cs_undefine:N \chapterlineswithprefixformat
            \cs_undefine:N \chapterlinesformat
            \@@_book_sectioning_code:
          }
          {
            \@ifclassloaded { scrreprt }
              {
                \@@_common_komascript_deactivation_code:
                \cs_undefine:N \chapterlineswithprefixformat
                \cs_undefine:N \chapterlinesformat
                \@@_report_sectioning_code:
              }
              {
%    \end{macrocode}
% \end{command}
% For all other classes, we use a heuristic to either use the sectioning code
% of \cls*{article}, \cls*{book}, or \cls*{report}
%    \begin{macrocode}
                \cs_if_exist:NTF \chapter
                  {
                    \cs_if_exist:cTF { frontmatter }
                      {
                        \@@_book_sectioning_code:
                      }
                      {
                        \@@_report_sectioning_code:
                      }
                  }
                  {
                    \@@_article_sectioning_code:
                  }
              }
          }
       }
  }   
%    \end{macrocode}
% \end{macro}
% \ExplSyntaxOff
%
% \begin{option}{book,article,report}
%   These options can be used to force the usage of one of the selection
%   codes, but only if no \KOMAScript{} class has been used. So in the code
%   section we first have to test for a \KOMAScript{} class and either throw a
%   warning or do the redefinition of \cs{@@\_activation\_code:}.
%    \begin{macrocode}
\DeclareKeys
  {
    article .meta:nn = { @@/internal } { force=article },
    article .usage   = load,
    book    .meta:nn = { @@/internal } { force=book },
    book    .usage   = load,
    report  .meta:nn = { @@/internal } { force=report },
    report  .usage   = load,
  }
\keys_define:nn { @@/internal }
  {  
     force .code =
      {
        \cs_if_exist:NTF \KOMAClassName
          {
            \msg_warning:nnn { standardsectioning }
                             { no-force-with-KOMA-Script-class }
                             { #1 }
          }
          {
            \cs_set_eq:Nc \@@_activation_code: { @@_#1_sectioning_code: }
          }
      }
  }
\ProcessKeyOptions
\@@_activation_code:  
%    \end{macrocode}
% \end{option}
%
% \iffalse
%</package>
% \fi
%
% \Finale
% \PrintChanges
%  
% \endinput
% Local Variables:
% mode: doctex
% ispell-local-dictionary: "en_US"
% eval: (flyspell-mode 1)
% TeX-master: t
% End: