% \iffalse meta-comment
% ======================================================================
% scrkernel-sections.dtx
% Copyright (c) Markus Kohm, 2002-2024
%
% This file is part of the LaTeX2e KOMA-Script bundle.
%
% 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 "author-maintained".
%
% The Current Maintainer and author of this work is Markus Kohm.
%
% This work consists of all files listed in MANIFEST.md.
% ======================================================================
%%% From File: $Id: scrkernel-sections.dtx 4130 2024-10-24 08:14:37Z kohm $
% The runs of run-time file generation:
%<preidentify>%%%            (run: preidentify)
% - everything that is needed before selfidentification of resulting files
%   (unused)
%<identify>%%%            (run: identify)
% - everything that is needed for selfidentification of resulting files
%   (unused)
%<prepare>%%%            (run: prepare)
% - everything that is needed to define options
%<option>%%%            (run: option)
% - definition of options
%<execoption>%%%            (run: execoption)
% - initial option executions up to \KOMAProcessOptions
%<body>%%%            (run: body)
% - everything, that should be done after \KOMAProcessOptions
%<exit>%%%            (run: exit)
% - everything, that should be done immediately before leaving the file
%<*dtx>
\ifx\ProvidesFile\undefined\def\ProvidesFile#1[#2]{}\fi
\begingroup
  \def\filedate$#1: #2-#3-#4 #5${\gdef\filedate{#2/#3/#4}}
  \filedate$Date: 2024-10-24 10:14:37 +0200 (Do, 24. Okt 2024) $
  \def\filerevision$#1: #2 ${\gdef\filerevision{r#2}}
  \filerevision$Revision: 4130 $
  \edef\reserved@a{%
    \noexpand\endgroup
    \noexpand\ProvidesFile{scrkernel-sections.dtx}%
                          [\filedate\space\filerevision\space
                           KOMA-Script source (disposition)]
  }%
\reserved@a
\documentclass[USenglish]{koma-script-source-doc}
\usepackage{babel}
\setcounter{StandardModuleDepth}{3}
\begin{document}
\DocInput{scrkernel-sections.dtx}
\end{document}
%</dtx>
% \fi
%
% \changes{v2.3h}{1996/01/20}{configurable font sized for headings}
% \changes{v2.95}{2002/06/26}{first version after splitting \file{scrclass.dtx}}
% \changes{v3.15}{2014/12/09}{reordered code in \file{scrkernel-sections.dtx}}
% \changes{v3.36}{2022/03/08}{switch over from \cls*{scrdoc} to
%   \cls*{koma-script-source-doc}}
% \changes{v3.36}{2022/03/21}{the whole implementation documentation in English}
% \changes{v3.40}{2023/04/17}{guide names changed}
%
% \GetFileInfo{scrkernel-sections.dtx}
% \title{The Code for Parts, Chapters, Sections etc.\@ of
%   \href{https://komascript.de}{\KOMAScript} Classes}
% \author{\href{mailto:komascript@gmx.info}{Markus Kohm}}
% \date{Revision \fileversion{} of \filedate}
% \maketitle
% \begin{abstract}
%   \file{scrkernel-sections.dtx} implements all the options, macros and user
%   commands used for the sectioning of documents.
% \end{abstract}
% \tableofcontents
%
% \section{User Manual}
%
% You can find the user documentation the commands implemented here in the
% \KOMAScript{} manual, either the German \file{scrguide-de.pdf} or the
% English \file{scrguide-en.pdf}.
%
% \MaybeStop{\PrintIndex}
%
% \section{Implementation of Parts, Chapters, Section etc.}
%
% The letter class does not provide sectioning.
%    \begin{macrocode}
%<*class&!letter>
%    \end{macrocode}
%
% \begin{macro}{@maybeasf}
% \changes{v2.8e}{2001/07/10}{removed}
% \end{macro}
%
% \subsection{Interleaf pages}
%
% With \cls*{scrbook} and \cls*{scrreprt} chapters can optionally (or by
% default) start automatically on a odd page. So if the last page before
% already was an odd page, we have to insert a so called interleaf page. The
% main handling of interleaf pages is already defined in
% \file{scrkernel-pagestyles.dtx} (but there is a ToDo note to move is to a
% new file). Here we only support the optional selection of the start page of
% a chapter.
%
% \begin{option}{open}
% \changes{v2.98c}{2008/03/06}{added}
% \changes{v3.12}{2013/03/05}{using \cs{FamilyKeyState}}
% \changes{v3.17}{2015/03/09}{using internal value storage}
% \changes{v3.18}{2015/05/22}{initialization using \opt{headings}}
% \changes{v3.39}{2022/11/11}{initial dot removed from member argument of
%   option storage commands}
% \begin{macro}{\@openrighttrue,\@openrightfalse,\if@openright}
% The option supports three values:
% \begin{center}
%   \begin{tabular}{llll}
%     \opt{open} & \cs{if@openright} & \cs{cleardoublestandardpage} &
%                                                              meaning\\\hline
%     \opt{=any} & \cs{iffalse}      & \cs{cleardoubleoddstandardpage} &
%                                                 chapters on any page\\
%     \opt{=right} & \cs{iftrue}     & \cs{cleardoubleoddstandardpage} &
%                                                chapters on odd pages\\
%     \opt{=left}  & \cs{iftrue}     & \cs{cleardoubleevenstandardpage} &
%                                               chapters on even pages\\
%   \end{tabular}
% \end{center}
%    \begin{macrocode}
%<*book|report>
%<*prepare>
\newif\if@openright
%<report>\@openrightfalse
%<book>\@openrighttrue
%</prepare>
%<*option>
\KOMA@key{open}{%
  \KOMA@set@ncmdkey{open}{@tempa}{%
    {any}{0},%
    {right}{1},%
    {left}{2}%
  }{#1}%
  \ifx\FamilyKeyState\FamilyKeyStateProcessed
    \KOMA@kav@xreplacevalue{\KOMAClassFileName}{open}{#1}%
    \KOMA@kav@remove{\KOMAClassFileName}{headings}{openany}%
    \KOMA@kav@remove{\KOMAClassFileName}{headings}{openright}%
    \KOMA@kav@remove{\KOMAClassFileName}{headings}{openleft}%
    \ifcase \@tempa\relax
      \KOMA@kav@add{\KOMAClassFileName}{headings}{openany}%
      \@openrightfalse
      \renewcommand*{\cleardoublestandardpage}{\cleardoubleoddstandardpage}%
    \or
      \KOMA@kav@add{\KOMAClassFileName}{headings}{openright}%
      \@openrighttrue
      \renewcommand*{\cleardoublestandardpage}{\cleardoubleoddstandardpage}%
    \or
      \KOMA@kav@add{\KOMAClassFileName}{headings}{openleft}%
      \@openrighttrue
      \renewcommand*{\cleardoublestandardpage}{\cleardoubleevenstandardpage}%
    \fi
  \fi
}
%</option>
%<*execoption>
%<book>\KOMAExecuteOptions{headings=openright}
%<report>\KOMAExecuteOptions{headings=openany}
%</execoption>
%    \end{macrocode}
% \end{macro}^^A \if@openright
% \end{option}^^A open
%
% \begin{option}{openright,openany}
% \changes{v2.98c}{2008/03/05}{obsolete}
% \changes{v3.01a}{2008/11/21}{standard instead of obsolete}
% Two deprecated options of \KOMAScript{} 2.
%    \begin{macrocode}
%<*option>
\KOMA@DeclareStandardOption{openright}{open=right}
\KOMA@DeclareStandardOption{openany}{open=any}
%</option>
%</book|report>
%    \end{macrocode}
% \end{option}^^A openright,openany
%
%
% \subsection{Numbers and the auto dot feature}
%
% Not only parts, chapter, section etc.\@ are numbered. But several other
% numbers are related to these numbers. The feature we define here is also
% known as the auto dot feature.
%
% \begin{option}{numbers}
% \changes{v2.98c}{2008/03/07}{added}
% \changes{v3.12}{2013/03/05}{using \cs{FamilyKeyState}}
% \changes{v3.17}{2015/03/09}{using internal value storage}
% \changes{v3.39}{2022/11/11}{initial dot removed from member argument of
%   option storage commands}
% \begin{macro}{\scr@dotchangeatdocument}
% \changes{v2.98c}{2008/03/07}{added}
% The \opt{numbers} option provides several settings for the dot at the end of
% such numbers. But because the setting is always document global, this option
% cannot be changed inside the document, but in the document preamble only. If
% you try to change it inside the document, the error of
% \cs{scr@dotchangedatdocument} will be raised. Currently following values to
% the option are supported:
% \begin{center}
%   \begin{tabular}{ll}
%     \opt{numbers} & meaning\\\hline
%     \opt{=auto}   & automatically detect, if the dot has to be added\\
%     \opt{=enddot} & always use a dot at the end\\
%     \opt{=noenddot}& don't use a dot at the end\\
%   \end{tabular}
% \end{center}
% The criterion for automatically adding a dot is: Does any sectioning number
% contain something else but an Arabic number for a number portion?
%    \begin{macrocode}
%<*prepare>
\newcommand*{\scr@dotchangeatdocument}[1]{%
  \ClassError{\KOMAClassName}{%
    change of end dot feature after \string\begin{document}%
  }{%
    You've tried to set `numbers=#1' after \string\begin{document},\MessageBreak
    but this option is only allowed at the document preamble.\MessageBreak
    See KOMA-Script manual for more information about.%
  }%
}
%</prepare>
%<*option>
\KOMA@key{numbers}{%
  \KOMA@set@ncmdkey{numbers}{@tempa}{%
    {autoendperiod}{0},{autoenddot}{0},{auto}{0},%
    {endperiod}{1},{withendperiod}{1},{periodatend}{1},%
    {enddot}{1},{withenddot}{1},{dotatend}{1},%
    {noendperiod}{2},{noperiodatend}{2},%
    {noenddot}{2},{nodotatend}{2}%
  }{#1}%
  \ifx\FamilyKeyState\FamilyKeyStateProcessed
    \KOMA@kav@xreplacevalue{\KOMAClassFileName}{numbers}{#1}%
    \ifcase \@tempa\relax
      \if@atdocument\scr@dotchangeatdocument{#1}\else
        \let\scr@altsecnumhook\@empty
      \fi
    \or
      \if@atdocument\scr@dotchangeatdocument{#1}\else
        \let\scr@altsecnumhook\scr@altsecnumhooktrue
      \fi
    \or
      \if@atdocument\scr@dotchangeatdocument{#1}\else
        \let\scr@altsecnumhook\scr@altsecnumhookfalse
      \fi
    \fi
  \fi
}
%</option>
%<execoption>\KOMAExecuteOptions{numbers=autoendperiod}
%    \end{macrocode}
% \end{macro}^^A \scr@dotchangeatdocument
% \end{option}^^A numbers
%
% \begin{option}{pointednumbers,pointlessnumbers}
% \changes{v2.3h}{1995/01/19}{added}
% \changes{v2.4g}{1996/11/04}{option name fixed}
% \changes{v2.4g}{1996/11/04}{immediate setting \cs{@altsecnumformatfalse}
%   to \cs{@altsecnumformattrue}}
% \changes{v2.98c}{2008/03/07}{obsolete}
% \changes{v3.01a}{2008/11/20}{deprecated}
% \changes{v3.99}{2022/11/16}{removed from \KOMAScript~4}
% Two deprecated options.
%    \begin{macrocode}
%<*option&!v4>
\KOMA@DeclareDeprecatedOption{pointednumbers}{numbers=enddot}
\KOMA@DeclareDeprecatedOption{pointlessnumbers}{numbers=noenddot}
%</option&!v4>
%    \end{macrocode}
% \end{option}^^A pointednumbers,pointlessnumbers
%
% \begin{command}{\autodot}
% \changes{v2.7}{2001/01/03}{added}
% \begin{macro}{\@altsecnumformattrue,\@altsecnumformatfalse,\if@altsecnumformat}
% \changes{v2.3c}{1995/08/06}{added}
% \begin{macro}{\@autodottrue,\@autodotfalse,\if@autodot}
% \changes{v2.8e}{2001/07/10}{added}
% \changes{v2.9k}{2003/01/12}{write to \cs{@mainaux}}
% \changes{v2.95}{2004/01/15}{write to \cs{@mainaux} just before closing the
%   file}
% \changes{v2.96a}{2006/12/07}{using \cs{immediate}}
% \changes{v3.00}{2008/10/07}{global}
% \changes{v3.20}{2016/03/25}{using \cs{csname}\dots\cs{endcsname}}
% Depending on \cs{if@altsecnumformat} \cs{autodot} shows a dot or nothing.
% \begin{description}
% \item[ToDo:] Add an optional look forward feature to also not add a dot, if
%   the next character is a punctuation character, e.g. a dot, semicolon,
%   comma etc.
% \end{description}
% \cs{if@autodot} is the current result of the inspection of all sectioning
% numbers. If it is \cs{iftrue}, switching on the dot will be written to the
% main \file{aux}-file.
%    \begin{macrocode}
%<*prepare>
\newif\if@altsecnumformat\@altsecnumformatfalse
\newif\if@autodot
\renewcommand*{\@autodottrue}{\global\let\if@autodot\iftrue}
\renewcommand*{\@autodotfalse}{\global\let\if@autodot\iffalse}
\@autodotfalse
%</prepare>
%<*body>
\BeforeClosingMainAux{%
  \if@autodot\if@filesw\immediate\write\@mainaux{%
      \string\global\string\csname\space @altsecnumformattrue\string\endcsname}%
  \fi\fi}
%</body>
%    \end{macrocode}
% \end{macro}^^A \if@autodot
% \end{macro}^^A \if@altsecnumformat
%
% \begin{macro}{\scr@altsecnumhook,\scr@altsecnumhooktrue,\scr@altsecnumhookfalse}
% \changes{v2.98c}{2008/03/07}{added}
% Used by option \opt{numbers} for (de-)activation of the dot in the document
% preamble and to overwrite the setting found in the \file{aux}-file.
% \begin{description}
% \item[Note:] The hook is executed after reading the main \file{aux}-file. So
%   the overwriting of the setting does work. It is needed as long a boolean
%   switch is used, because otherwise option \opt{numbers=auto} would not be
%   able to reset option \opt{numbers=enddot} ot \opt{numbers=noenddot}. With
%   this code we simply can use an empty \cs{scr@altsecnumhook} for
%   \opt{numbers=auto} and two compellent definitions for the other cases.
% \end{description}
%    \begin{macrocode}
%<*prepare>
\newcommand*{\scr@altsecnumhook}{}
\AtBeginDocument{\scr@altsecnumhook}
\newcommand*{\scr@altsecnumhooktrue}{%
  \@altsecnumformattrue\global\let\@altsecnumformatfalse\@altsecnumformattrue
}
\newcommand*{\scr@altsecnumhookfalse}{%
  \@altsecnumformatfalse\global\let\@altsecnumformattrue\@altsecnumformatfalse
}
%</prepare>
%    \end{macrocode}
% \end{macro}^^A \scr@altsecnumhook,\scr@altsecnumhooktrue,\scr@altsecnumhookfalse
%
% \begin{macro}{\@maybeautodot,\@@maybeautodot}
% \changes{v2.8e}{2001/07/10}{added}
% \changes{v3.00}{2008/10/07}{not any longer needed \cs{aftergroup} removed}
% We scan a \cs{the\meta{counter}} command to decide, if the automatic dot has
% to be added (later). It sets \cs{@autodottrue}, if there is any kind of
% alphabetic or Roman numbering command inside the one time expansion of the
% \cs{the\meta{counter}} given as argument.
% \begin{description}
% \item[Note:] This is a heuristic only. It would, e.g., fail, if you are
%   using another macro, that expands to the output of the counter, e.g.,
%\begin{verbatim}
%  \def\myalph{\alph}\def\thesection{\myalph{section}}
%\end{verbatim}
%   However, it seems that the heuristic is so good, that in more than twenty
%   years nobody has complained about.
% \end{description}
%    \begin{macrocode}
%<*body>
\newcommand{\@maybeautodot}[1]{\if@autodot\else\begingroup%
  \expandafter\@@maybeautodot #1\@stop\endgroup\fi
}
\newcommand*{\@@maybeautodot}[1]{%
  \ifx #1\@stop\let\@@maybeautodot\relax
  \else
    \ifx #1\Alph \@autodottrue\fi
    \ifx #1\alph \@autodottrue\fi
    \ifx #1\Roman \@autodottrue\fi
    \ifx #1\roman \@autodottrue\fi
    \ifx #1\@Alph \@autodottrue\fi
    \ifx #1\@alph \@autodottrue\fi
    \ifx #1\@Roman \@autodottrue\fi
    \ifx #1\@roman \@autodottrue\fi
    \ifx #1\romannumeral \@autodottrue\fi
  \fi
  \@@maybeautodot
}
%</body>
%    \end{macrocode}
% \end{macro}^^A \@maybeautodot,\@@maybeautodot
%
%    \begin{macrocode}
%<*body>
\newcommand*\autodot{\if@altsecnumformat.\fi}
%</body>
%    \end{macrocode}
% \end{command}^^A \autodot
%
%
% \subsection{Definition and handling of sectioning commands}
%
% Some of the \LaTeX{} kernel features are used here too. But almost none of
% these features is enough to implement all the functionality of \KOMAScript{}
% classes. So we redefine several of the \LaTeX{} kernel macros to extend them
% and define several new features.
% \begin{description}
% \item[ToDo:] Don't redefine \LaTeX{} kernel macros, but define new macros
%   for the interface. Maybe this should also be swapped to a new package used
%   by the classes.
% \end{description}
%
% \begin{counter}{secnumdepth}
% The counter rules down to which section level the headings should be
% numbered.
% \begin{description}
% \item[ToDo:] Move this behind the declaration of the section levels to be
%   able to use symbolic value \cs{subsectionnumdepth}. It is not related to
%   the auto dot feature.
% \end{description}
%    \begin{macrocode}
%<*body>
%<book|report>\setcounter{secnumdepth}{2}
%<article>\setcounter{secnumdepth}{3}
%</body>
%    \end{macrocode}
% \end{counter}
%
%
% \begin{command}{\ifnumbered,\Ifnumbered}
% \changes{v3.12}{2013/12/16}{added}
% \changes{v3.28}{2019/11/19}{\cs{ifnumbered} renamed to \cs{Ifnumbered}}
% \begin{command}{\ifunnumbered,\Ifunnumbered}
% \changes{v3.12}{2013/12/16}{added}
% \changes{v3.28}{2019/11/19}{\cs{ifunnumbered} renamed to \cs{Ifunnumbered}}
% Commands
% \begin{quote}\raggedright
%   \cs{Ifnumbered}\marg{section level name}\marg{numbered}\marg{not numbered}\\
%   \cs{Ifunnumbered}\marg{section level name}\marg{not numbered}\marg{numbered}\\
% \end{quote}
% If \meta{section level name} has to be numbered, execute code \meta{numbered}
% otherwise execute code \meta{not numbered}.
%    \begin{macrocode}
%<*body>
\providecommand*{\ifnumbered}{%
  \ClassWarning{\KOMAClassName}{Usage of deprecated command
    `\string\ifnumbered'.\MessageBreak
    The command has been renamed because of a\MessageBreak
    recommendation of The LaTeX Project Team.\MessageBreak
    Please replace `\string\ifnumbered' by `\string\Ifnumbered'%
  }%
  \Ifnumbered
}
\newcommand*{\Ifnumbered}[1]{%
  \if@currentusenumber
    \scr@ifundefinedorrelax{#1numdepth}{%
      \expandafter\@secondoftwo
    }{%
      \expandafter\ifnum \@nameuse{#1numdepth}>\c@secnumdepth
        \expandafter\expandafter\expandafter\@secondoftwo
      \else
        \expandafter\expandafter\expandafter\@firstoftwo
      \fi
    }%
  \else
    \expandafter\@secondoftwo
  \fi  
}
\providecommand*{\ifunnumbered}{%
  \ClassWarning{\KOMAClassName}{Usage of deprecated command
    `\string\ifunnumbered'.\MessageBreak
    The command has been renamed because of a\MessageBreak
    recommendation of The LaTeX Project Team.\MessageBreak
    Please replace `\string\ifunnumbered' by `\string\Ifunnumbered'%
  }%
  \Ifunnumbered
}
\newcommand*{\Ifunnumbered}[1]{%
  \Ifnumbered{#1}{\@secondoftwo}{\@firstoftwo}%
}
%</body>
%    \end{macrocode}
% \end{command}^^A \Ifunnumbered
% \end{command}^^A \ifnumbered
%
%
% \begin{macro}{\scr@activate@xsection,\scr@osectarg}
% \changes{v3.10}{2011/08/30}{added}
% Option \opt{headings=optiontotocandhead}, \opt{headings=optiontotoc} and
% \opt{headings=optiontohead} used
% \begin{quote}
%   \cs{scr@activate@xsection}\marg{value}
% \end{quote}
% with the values 3, 2 and 1 to activate the extended optional argument. This
% sets \cs{scr@osectarg} to the corresponding value. The initial value 0
% deactivates the extended optional argument.
% \begin{description}
% \item[ToDo:] Do we really need \cs{scr@activate@xsection}, that is used
%   inside option \opt{headings} only?
% \end{description}
%    \begin{macrocode}
%<*prepare>
\newcommand*{\scr@osectarg}{0}
\newcommand*{\scr@activate@xsection}[1]{%
  \renewcommand*{\scr@osectarg}{#1}%
}
%</prepare>
%    \end{macrocode}
% \end{macro}^^A \scr@activate@xsection,\scr@osectarg
%
%
% \begin{macro}{\@startsection}
% \changes{v3.13a}{2014/09/11}{re-definition for extended optional argument}
% \begin{macro}{\scr@startsection}
% \changes{v3.13a}{2014/09/11}{added}
% \changes{v3.18}{2015/05/22}{using \cs{numexpr} for argument 2}
% \changes{v3.18}{2015/05/22}{using \cs{glueexpr} for arguments 3--5}
% \changes{v3.20}{2016/04/12}{\cs{@ifstar} replaced by \cs{kernel@ifstar}}
% \changes{v3.26}{2018/09/18}{supporting \opt{afterindent}}
% \begin{macro}{\scr@saved@startsection}
% \changes{v3.13a}{2014/09/11}{added}
% \changes{v3.20}{2016/04/12}{supporting \pkg{amsgen}}
% \begin{macro}{\startsection@sectionname,\startsection@secnumdepth,
%               \startsection@indent,\startsection@beforeskip,
%               \startsection@afterskip,\startsection@afterindent,
%               \@startsection@runintrue,\@startsection@runinfalse,
%               \if@startsection@runin}
% \changes{v3.26}{2018/05/14}{added}
% The \cs{@startsection} of the \LaTeX{} kernel does not provide all the
% features we need for \KOMAScript. And the generic hooks of \LaTeX{} do not
% allow to change it wide enough. So we either have to use \pkg{xparse} to
% patch it very much or we have to define our own version. In this case
% patching is too fragile, so we define \cs{scr@startsection} and store the
% original definition in \cs{scr@saved@startsection}.
% \begin{description}
% \item[Note:] Package \pkg{amsgen} redefines \cs{@ifstar}. The new version
%   does not ignore white spaces. So testing the original definition using
%   \cs{@ifstar} may fail. It is better to use \cs{kernel@ifstar}.
% \item[ToDo:] All the testing here doesn't make much sense, because we are
%   inside a class and packages like \pkg{amsgen} should not yet have been
%   loaded. So the test only needs to test, if the \LaTeX{} kernel has been
%   changed.
% \item[Note:] In our own \cs{scr@startsection} the macros
%   \cs{startsection@sectionname}, \cs{startsection@secnumdepth},
%   \cs{startsection@indent}, \cs{startsection@beforeskip},
%   \cs{startsection@afterskip}, \cs{if@afterindent},
%   \cs{if@startsection@runin}, and \cs{startsection@afterindent} are valid
%   from executing the do hook \dhook{heading/postinit}. So they can be used
%   inside all the hooks.
% \item[Note:] \cs{startsection@indent}, \cs{startsection@beforeskip},
%   \cs{startsection@afterskip} and \cs{startsection@afterindent} are either
%   skips or \cs{glueexpr}, that can be used like skips.
% \item[Note:] \cs{startsection@beforeskip} and \cs{startsection@afterskip}
%   are effective values already. Not special treatment for negative or
%   positive values must be done.
% \item[Note:] \cs{if@startsection@runin}
%   are either \cs{iftrue} or \cs{iffalse}.
% \item[Note:] \cs{if@startsection@indentafter} does not exist and is not
%   needed, because \cs{if@afterindent} is already valid and also either
%   \cs{iftrue} or \cs{iffalse}.
% \item[Note:] Using one of the \cs{startsection@\dots}
%   resp. \cs{if@startsection@\dots} macros outside \cs{scr@startsection} is
%   not allowed and the result is unspecified. Inside \cs{scr@startsection}
%   they have to be interpreted as constants, so changing them is not allowed!
% \end{description}
%    \begin{macrocode}
%<*body>
\newcommand*{\scr@saved@startsection}[6]{%
  \if@noskipsec \leavevmode \fi
  \par
  \@tempskipa #4\relax
  \@afterindenttrue
  \ifdim \@tempskipa <\z@
    \@tempskipa -\@tempskipa \@afterindentfalse
  \fi
  \if@nobreak
    \everypar{}%
  \else
    \addpenalty\@secpenalty\addvspace\@tempskipa
  \fi
  \kernel@ifstar
    {\@ssect{#3}{#4}{#5}{#6}}%
    {\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}%
}
\def\reserved@a#1#2#3#4#5#6{%
  \if@noskipsec \leavevmode \fi
  \par
  \@tempskipa #4\relax
  \@afterindenttrue
  \ifdim \@tempskipa <\z@
    \@tempskipa -\@tempskipa \@afterindentfalse
  \fi
  \if@nobreak
    \everypar{}%
  \else
    \addpenalty\@secpenalty\addvspace\@tempskipa
  \fi
  \@ifstar
    {\@ssect{#3}{#4}{#5}{#6}}%
    {\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}%
}
\ifcase \ifx\@startsection\scr@saved@startsection 0
        \else
          \ifx\@startsection\reserved@a 0
          \else 1
          \fi
        \fi
  \newcommand*{\scr@startsection}[6]{%
    \ExecuteDoHook{heading/preinit/#1}%
    \if@noskipsec \leavevmode \fi
    \par
    \@tempskipa \glueexpr #4\relax
    \@ifundefined{scr@#1@afterindent}{%
      \def\startsection@afterindent##1##2##3{##3}%
    }{%
      \expandafter\let\expandafter\startsection@afterindent
      \csname scr@#1@afterindent\endcsname
    }%
    \startsection@afterindent{\@afterindenttrue}{\@afterindentfalse}{%
      \@afterindenttrue
      \ifdim \@tempskipa <\z@
        \@tempskipa -\@tempskipa \@afterindentfalse
      \fi
    }%  
    \def\startsection@sectionname{#1}%
    \def\startsection@secnumdepth{\numexpr #2\relax}%
    \def\startsection@indent{\glueexpr #3\relax}%
    \let\startsection@beforeskip\@tempskipa
    \@ifundefined{scr@#1@runin}{%
      \def\scr@sect@runin##1##2##3{##3}%
    }{%
      \expandafter\let\expandafter\scr@sect@runin
      \csname scr@#1@runin\endcsname
    }%
    \scr@sect@runin{%
      \def\startsection@afterskip{\glueexpr #3\relax}%
      \expandafter\let\csname if@startsection@runin\expandafter\endcsname
      \csname iftrue\endcsname
    }{%
      \def\startsection@afterskip{\glueexpr #3\relax}%
      \expandafter\let\csname if@startsection@runin\expandafter\endcsname
      \csname iffalse\endcsname
    }{%
      \ifdim \glueexpr #5\relax <\z@
        \def\startsection@afterskip{\glueexpr (#3)*\m@ne\relax}%
        \expandafter\let\csname if@startsection@runin\expandafter\endcsname
        \csname iftrue\endcsname
      \else
        \def\startsection@afterskip{\glueexpr #3\relax}%
        \expandafter\let\csname if@startsection@runin\expandafter\endcsname
        \csname iffalse\endcsname
      \fi
    }%
    \ExecuteDoHook{heading/postinit/#1}%
    \if@nobreak
      \everypar{}%
    \else
      \addpenalty\@secpenalty\addvspace\@tempskipa
    \fi
    \kernel@ifstar {%
      \ExecuteDoHook{heading/branch/star/#1}%
      \def\scr@s@ct@@nn@m@{#1}%
      \@ssect{\glueexpr #3\relax}{\glueexpr #4\relax}{\glueexpr #5\relax}{#6}%
    }{%
      \ExecuteDoHook{heading/branch/nostar/#1}%
      \scr@section@dblarg{%
        \@sect{#1}{\numexpr #2\relax}{\glueexpr #3\relax}{\glueexpr #4\relax}%
        {\glueexpr #5\relax}{#6}%
      }%
    }%
  }
\else
  \ClassWarningNoLine{\KOMAClassName}{`\string\@startsection' has been
    changed.\MessageBreak
    \KOMAClassName\space needs it's own definition of
    `\string\@startsection'\MessageBreak
    to provide extended features for the optional argument\MessageBreak
    of `\string\section' etc.\MessageBreak
    Generally it defines `\string\@startsection' completely new\MessageBreak
    to achieve this. Because of the unexpected definition\MessageBreak
    an alternative approach will be used.\MessageBreak
    If this fails and if there isn't a new release of\MessageBreak
    KOMA-Script that fixes the problem, please\MessageBreak
    send a report to the KOMA-Script author.\MessageBreak
    Note, that this alternative approach does not\MessageBreak
    execute the elements of do-hooks:\MessageBreak
    \space\space- `heading/postinit',\MessageBreak
    \space\space- `heading/branch/star',\MessageBreak
    \space\space- `heading/branch/nostar',\MessageBreak
    and does always use `runin=bysign' and\MessageBreak
    `afterindent=bysign'%
  }%
  \let\scr@saved@startsection\@startsection
  \newcommand*{\scr@startsection}[6]{%
    \ExecuteDoHook{heading/preinit/#1}%
    \kernel@ifstar {%
      \scr@saved@startsection{#1}{\numexpr #2\relax}{\glueexpr #3\relax}%
      {\glueexpr #4\relax}{\glueexpr #5\relax}{#6}*%
    }{%
      \scr@section@dblarg{%
        \scr@saved@startsection{#1}{\numexpr #2\relax}{\glueexpr #3\relax}%
        {\glueexpr #4\relax}{\glueexpr #5\relax}{#6}}%
    }%
  }
\fi
%    \end{macrocode}
% \begin{macro}{\scr@sect@runin}
% \changes{v3.26}{2018/09/18}{added}
% Depending on sectioning feature \opt{runin} the macro expands to the first
% argument, if a in-text heading aka run-in heading has to used, the second
% argument, if a free-standing heading has to be used, or the third argument,
% of run-in or free-standing depends on the sign of \opt{afterskip}. The
% initial definition is depending on the sign of \opt{afterskip}. This initial
% definition is also done inside \cs{@xsect}. \cs{scr@startsection} also
% redefines \cs{scr@sect@runin}. So it should always be the currently valid
% definition, even if \cs{scr@startsection} is used with a section command,
% that does not provide all the variable of \KOMAScript.
%    \begin{macrocode}
\newcommand*{\scr@sect@runin}[3]{#3}
%    \end{macrocode}
% \end{macro}^^A \scr@sect@runin
% \begin{macro}{\scr@startsection@ulm@error}
% \changes{v3.26}{2018/05/14}{added}
% This is the initial error message for using one of the internal macros
% before \cs{scr@startsection}. This should never happen.
%    \begin{macrocode}
\newcommand*{\scr@startsection@ulm@error}[1]{%
  \ClassError{\KOMAClassName}{%
    \string#1 undefined outside of \string\scr@startsection
  }{%
    A KOMA-Script macro for package authors have been used outside
    the\MessageBreak
    specified definition scope.\MessageBreak
    See `scrkernel-sections.dtx' for more information.%
  }%
}
\newcommand*\startsection@sectionname{%
  \scr@startsection@ulm@error\startsection@sectionname}
\newcommand*\startsection@secnumdepth{%
  \scr@startsection@ulm@error\startsection@secnumdepth}
\newcommand*\startsection@indent{%
  \scr@startsection@ulm@error\startsection@indent}
\newcommand*\startsection@beforeskip{%
  \scr@startsection@ulm@error\startsection@beforeskip}
\newcommand*\startsection@afterskip{%
  \scr@startsection@ulm@error\startsection@afterskip}
\newcommand*{\if@startsection@runin}{%
  \scr@startsection@ulm@error\if@startsection@runin}
\newif\if@startsection@runin
\newcommand*\startsection@afterindent{%
  \scr@startsection@ulm@error\startsection@afterindent}
%    \end{macrocode}
% \end{macro}^^A \scr@startsection@ulm@error
% \end{macro}^^A \startsection@sectionname … \scr@startsection@runin
% \begin{macro}{\At@startsection}
% \changes{v3.14}{2014/09/11}{added}
% \changes{v3.27}{2019/02/04}{using \cs{AddtoDoHook}}
% \changes{v3.27}{2019/02/04}{deprecated}
% This is deprecated now. You should use the corresponding
% \dhook{heading/postinit} do hook. However the do hooks are command specific,
% but the argument of \cs{At@startsection} is style specific.
%    \begin{macrocode}
\newcommand*{\At@startsection}[1]{%
  \ClassInfo{\KOMAClassName}{%
    Usage of deprecated command `\string\At@startsection'
    mapped to\MessageBreak
    `\string\AddtoDoHook{heading/postinit}
                        {...\string\@gobble}'%
  }%
  \AddtoDoHook{heading/postinit}{\scr@doonlyifstyleofargis{#1}{section}}%
}
%    \end{macrocode}
% \begin{macro}{\scr@doonlyifstyleofargis}
% \changes{v3.27}{2019/02/04}{added}
% Execute the first argument only, of the style of the third argument is the
% second argument.
%    \begin{macrocode}
\newcommand*{\scr@doonlyifstyleofargis}[3]{%
  \IfSectionCommandStyleIs{#3}{#2}{#1}{}%
}
%    \end{macrocode}
% \end{macro}^^A \scr@doonlyifstyleofargis
% \end{macro}^^A \At@startsection
% \begin{macro}{\Before@ssect}
% \changes{v3.14}{2014/09/11}{added}
% \changes{v3.27}{2019/02/04}{using \cs{AddtoDoHook}}
% \changes{v3.27}{2019/02/04}{deprecated}
% \changes{v3.35}{2021/10/15}{\cs{ClassWarning} instead of \cs{ClassInfo}}
% \changes{v3.35}{2021/10/15}{Usage of \cs{scr@doonlyifstyleofargis} fixed}
% This is deprecated now. You should use the corresponding
% \dhook{heading/branch/star} do hook. However the do hooks are command
% specific, but the argument of \cs{Before@ssect} is style specific.
%    \begin{macrocode}
\newcommand*{\Before@ssect}[1]{%
  \ClassWarning{\KOMAClassName}{%
    Usage of deprecated command `\string\Before@ssect'
    mapped to\MessageBreak
    `\string\AddtoDoHook{heading/branch/star}%
                        {...\string\@gobble}'%
  }%
  \AddtoDoHook{heading/branch/star}{\scr@doonlyifstyleofargis{#1}{section}}%
}
%    \end{macrocode}
% \end{macro}^^A \Before@ssect
% \begin{macro}{\Before@sect}
% \changes{v3.14}{2014/09/11}{added}
% \changes{v3.27}{2019/02/04}{using \cs{AddtoDoHook}}
% \changes{v3.27}{2019/02/04}{deprecated}
% \changes{v3.35}{2021/10/15}{\cs{ClassWarning} instead of \cs{ClassInfo}}
% \changes{v3.35}{2021/10/15}{Usage of \cs{scr@doonlyifstyleofargis} fixed}
% This is deprecated now. You should use the corresponding
% \dhook{heading/branch/nostar} do hook. However the do hooks are command
% specific, but the argument of \cs{Before@sect} is style specific.
%    \begin{macrocode}
\newcommand*{\Before@sect}[1]{%
  \ClassWarning{\KOMAClassName}{%
    Usage of deprecated command `\string\Before@sect'
    mapped to\MessageBreak
    `\string\AddtoDoHook{heading/branch/nostar}%
                        {...\string\@gobble}'%
  }%
  \AddtoDoHook{heading/branch/nostar}{\scr@doonlyifstyleofargis{#1}{section}}%
}
%    \end{macrocode}
% \end{macro}^^A \Before@sect
% \begin{macro}{\scr@section@dblarg,\scr@section@xdblarg}
% \changes{v3.13a}{2014/09/11}{added}
% Similar to the \LaTeX{} kernel macros \cs{@dblarg} and \cs{@xdblarg} but
% preserve the definition of \cs{scr@osectarg}. So changes of
% \cs{scr@osectarg} inside the definition of the command of the first
% argument, are locally to the execution of that command.
%    \begin{macrocode}
\newcommand{\scr@section@dblarg}[1]{%
  \kernel@ifnextchar[%]
    {#1}%
    {\scr@section@xdblarg{#1}}%
}
\newcommand{\scr@section@xdblarg}[2]{%
  \begingroup
    \edef\reserved@a{%
      \unexpanded{\endgroup\let\scr@osectarg\z@#1[{#2}]{#2}\def\scr@osectarg}%
      {\scr@osectarg}}%
  \reserved@a
}
%    \end{macrocode}
% \end{macro}^^A \scr@section@dblarg,\scr@section@xdblarg
% \end{macro}^^A \scr@saved@startsection
% \end{macro}^^A \scr@startsection
% \begin{description}
% \item[ToDo:] Currently we still redefine \cs{@startsection} to use either
%   \cs{scr@saced@startsection} or \cs{scr@startsection}. But it would be
%   better to not redefine a \LaTeX{} kernel macro. However, if not doing so,
%   packages, that use \cs{@startsection} would not support \KOMAScript{}
%   features without detecting \KOMAScript{} and using \cs{scr@startsection}.
% \end{description}
%    \begin{macrocode}
\renewcommand*{\@startsection}{%
  \ifnum \scr@osectarg=\z@
    \expandafter\scr@saved@startsection
  \else
    \expandafter\scr@startsection
  \fi
}
%</body>
%    \end{macrocode}
% \end{macro}^^A \@startsection
%
% \begin{command}{\UseNumberUsageError}
% \changes{v3.27}{2019/02/02}{added}
% \begin{command}{\IfUseNumber}
% \changes{v3.27}{2019/02/02}{error if used outside sectioning command}
% Command \cs{IfUseNumner} is defined only inside sectioning commands. Using
% it outside is not specified. To report an error in this case, it should be
% set to \cs{UseNumberUsageError} outside a sectioning command.
%    \begin{macrocode}
%<*body>
\newcommand*{\UseNumberUsageError}[2]{%
  \ClassError{\KOMAClassName}{%
    \string\IfUseNumber\space not allowed%
  }{%
    You cannot use \string\IfUseNumber\space outside a heading.\MessageBreak
    If you'd continue, I'll ignore both arguments, because I do not
    know\MessageBreak
    whether to use the first or the second argument.%
  }%
}
\newcommand*{\IfUseNumber}[2]{}%
\let\IfUseNumber\UseNumberUsageError
%</body>
%    \end{macrocode}
% \end{command}^^A \IfUseNumnr
% \end{command}^^A \UseNumberUsageError
%
%
% \begin{command}{\SecDef}
% \changes{v3.13a}{2014/09/11}{added}
% \changes{v3.20}{2016/04/12}{\cs{@ifstar} replaced by \cs{kernel@ifstar}}
% \begin{command}{\secdef}
% \changes{v3.13a}{2014/09/11}{re-definition because of extended optional argument}
%    \begin{macrocode}
%<*body>
\newcommand*{\SecDef}[2]{\kernel@ifstar{#2}{\scr@section@dblarg{#1}}}
\CheckCommand*{\secdef}[2]{\@ifstar{#2}{\@dblarg{#1}}}
\let\secdef\SecDef
%</body>
%    \end{macrocode}
% \end{command}^^A \secdef
% \end{command}^^A \SecDef
% \begin{macro}{\scr@sect}
% \changes{v3.10}{2011/08/30}{removed}
% \end{macro}^^A \scr@sect
% \begin{macro}{\@sect}
% \changes{v3.10}{2011/08/30}{re-definition because of extended optional argument}
% \changes{v3.10}{2011/08/30}{using
%   \cs{csname}\texttt{add\#1tocentry}\cs{endcsname}
%   resp. \cs{addtocentrydefault}}
% \changes{v3.18}{2015/05/20}{defines \cs{IfUsePrefixLine} locally}
% \changes{v3.18}{2015/05/22}{using \cs{numexpr} for argument 2}
% \changes{v3.18}{2015/05/22}{using \cs{gluexpr} for arguments 3--5}
% \changes{v3.19}{2015/07/17}{using \cs{sectionlinesformat} and
%   \cs{sectioncatchphraseformat}}
% \changes{v3.27}{2019/07/08}{new optional \opt{nonumber}}
% \changes{v3.30}{2020/02/25}{orphan spaces removed}
% \begin{macro}{\scr@latex@sect}
% \changes{v3.27}{2019/07/24}{added to be used by \pkg*{scrhack}}
% Because we want to support the extended optional argument for all commands
% defined using \cs{@startsection} we also have to redefine \cs{@sect}. Once
% again several security tests are done, before redefining an internal
% \LaTeX{} kernel macro.
% \begin{description}
% \item[ToDo:] It would be better not to redefine an internal \LaTeX{} kernel
%   macro, but use a own definition in our own \cs{scr@startsection}. But in
%   this case packages, that use \cs{@sect} would not automatically support
%   several \KOMAScript{} features.
% \item[Note:] \cs{chapter} and \cs{part} do not use \cs{@sect} but
%   \cs{SecDef}.
% \end{description}
%    \begin{macrocode}
%<*body>
\def\scr@latex@sect#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\@sect\scr@latex@sect\else
  \let\scr@latex@sect\@sect
  \ClassWarning{\KOMAClassName}{%
    Unexpected definition of \string\@sect!\MessageBreak
    Please send information about this to\MessageBreak
    the KOMA-Script maintainer!\MessageBreak
    Maybe LaTeX will be broken by the redefinition\MessageBreak
    of \string\@sect}%
\fi
%    \end{macrocode}
% \end{macro}^^A \scr@latex@sect
%    \begin{macrocode}
%<trace>\ClassInfo{\KOMAClassName}{redefining LaTeX kernel macro \string\@sect}
\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum \scr@osectarg=\z@
    \@scr@tempswafalse
  \else
    \scr@istest#7=\@nil
  \fi
  \@currentusenumbertrue  
  \if@scr@tempswa
    \setkeys{KOMAarg.section}{tocentry={#8},head={#8},reference={#8},#7}%
  \else
    \ifcase \scr@osectarg\relax
      \setkeys{KOMAarg.section}{tocentry={#7},head={#7},reference={#7}}%
    \or
      \setkeys{KOMAarg.section}{tocentry={#8},head={#7},reference={#8}}%
    \or
      \setkeys{KOMAarg.section}{tocentry={#7},head={#8},reference={#7}}%
    \or
      \setkeys{KOMAarg.section}{tocentry={#7},head={#7},reference={#7}}%
    \fi
  \fi
%    \end{macrocode}
% \changes{v3.23}{2017/02/04}{support for \pkg{minitoc}}
% \changes{v3.23}{2017/03/24}{usage of \cs{ext@figure} and \cs{ext@table}
%    instead of \texttt{lof} and \texttt{lot}}
% Add the \texttt{xsect} entries to \texttt{lof} and \texttt{lot}. Note, that
% the original \cs{starsection} tests of \pkg{minitoc} has a bug and so
% does not work. And the original code of \pkg{minitoc} has hard coded
% \texttt{lot} and \texttt{lof} instead of \cs{ext@figure} and
% \cs{ext@table}. This is a fixed one:
%    \begin{macrocode}
  \scr@ifundefinedorrelax{scr@mt@saved@sect}{}{%
    \expandafter\ifx\csname #1\endcsname\section
      \addcontentsline{\ext@figure}{xsect}{\@currenttocentry}%
      \addcontentsline{\ext@table}{xsect}{\@currenttocentry}%
    \fi
    \expandafter\ifx\csname #1\endcsname\starsection\relax
      \addcontentsline{\ext@figure}{xsect}{\@currenttocentry}%
      \addcontentsline{\ext@table}{xsect}{\@currenttocentry}%
    \fi
  }%
  \let\IfUsePrefixLine\@secondoftwo
  \ifcase
    \if@currentusenumber
      \ifnum \numexpr #2\relax>\c@secnumdepth \z@\else \@ne\fi
    \else
      \z@
    \fi
    \let\@svsec\@empty
%    \end{macrocode}
% \changes{v3.31}{2020/06/02}{execute counter reset list}
% Even for not numbered sections, the reset list für the section counter has
% to be executed.
%    \begin{macrocode}
%<!v4>    \expandafter\ifnum\scr@v@is@ge{3.31}\relax
      \begingroup
        \let\@elt\@stpelt
        \csname cl@#1\endcsname
      \endgroup
%<!v4>    \fi
%    \end{macrocode}
% \changes{v3.31}{2020/06/02}{\cs{hy@insteadofrefstepcounter} added}
% At this point we have to make sure, that the \pkg{hyperref} code for not
% increasing a counter is executed.
%    \begin{macrocode}
    \hy@insteadofrefstepcounter{#1}%
  \else
    \refstepcounter{#1}%
    \expandafter\@maybeautodot\csname the#1\endcsname
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
%    \end{macrocode}
% \changes{v3.26}{2018/09/18}{support for \cs{scr@sect@runin}}
% \changes{v3.27}{2019/02/02}{\cs{IfUseNumber} defined locally}
%    \begin{macrocode}
  \ifdim
    \scr@sect@runin{\z@}{\p@}{\glueexpr #5\relax}>\z@
    \begingroup
      \ifx\@svsec\@empty
        \let\IfUseNumber\@secondoftwo
      \else
        \let\IfUseNumber\@firstoftwo
      \fi
%    \end{macrocode}
% \changes{v3.21}{2016/06/12}{missing \cs{nobreak} added}
% \changes{v3.27}{2019/02/02}{\cs{scr@do@at} added}
%    \begin{macrocode}
      \ExecuteDoHook{heading/begingroup/#1}%
      #6{\nobreak\interlinepenalty \@M
        \sectionlinesformat{#1}{\glueexpr #3\relax}\@svsec{#8}\@@par}%
      \ExecuteDoHook{heading/endgroup/#1}%
    \endgroup
%<*trace>
    \ClassInfo{\KOMAClassName}{%
      head=`\detokenize\expandafter{\@currentheadentry}'\MessageBreak
      tocentry=`\detokenize\expandafter{\@currenttocentry}'\MessageBreak
      reference=`\detokenize\expandafter{\@currentlabelname}'}%
%</trace>
    \expandafter\csname #1mark\expandafter\endcsname\expandafter{\@currentheadentry}%
    \ifx\@currenttocentry\@empty\else
      \scr@ifundefinedorrelax{add#1tocentry}{%
        \expandafter\gdef\csname add#1tocentry\endcsname##1##2{%
          \addtocentrydefault{#1}{##1}{##2}%
        }%
      }{}%
      \ifcase
        \if@currentusenumber
          \ifnum \numexpr #2\relax>\c@secnumdepth \z@\else \@ne\fi
        \else
          \z@
        \fi
        \csname add#1tocentry\endcsname{}{\@currenttocentry}%
      \else
%    \end{macrocode}
% \changes{v3.31}{2020/06/06}{using \cs{\dots tocformat}}%
% \begin{description}
% \item[ToDo:] Move \cs{autodot} to the definition of \cs{\dots tocformat}.
% \end{description}
%    \begin{macrocode}
        \@ifundefined{#1tocformat}{%
          \expandafter\gdef\csname #1tocformat\endcsname
          {\csname the#1\endcsname}%
        }{}%
        \csname add#1tocentry\endcsname{\csname #1tocformat\endcsname}{%
          \@currenttocentry}%
      \fi
    \fi
  \else
%    \end{macrocode}
% \changes{v3.21}{2016/06/12}{missing \cs{nobreak} added}
% \changes{v3.27}{2019/02/02}{\cs{ExecuteDoHook} added}
%    \begin{macrocode}
    \def\@svsechd{%
      \ifx\@svsec\@empty
        \let\IfUseNumber\@secondoftwo
      \else
        \let\IfUseNumber\@firstoftwo
      \fi
      \ExecuteDoHook{heading/begingroup/#1}%
      #6{\nobreak\sectioncatchphraseformat{#1}{\glueexpr #3\relax}\@svsec{#8}}%
%<*trace>
      \ClassInfo{\KOMAClassName}{%
        head=`\detokenize\expandafter{\@currentheadentry}'\MessageBreak
        tocentry=`\detokenize\expandafter{\@currenttocentry}'\MessageBreak
        reference=`\detokenize\expandafter{\@currentlabelname}'}%
%</trace>
      \expandafter\csname #1mark\expandafter\endcsname
      \expandafter{\@currentheadentry}%
      \ifx\@currenttocentry\@empty\else
        \scr@ifundefinedorrelax{add#1tocentry}{%
          \expandafter\gdef\csname add#1tocentry\endcsname####1####2{%
            \addtocentrydefault{#1}{####1}{####2}%
          }%
        }{}%
        \ifcase
          \if@currentusenumber
            \ifnum \numexpr #2\relax>\c@secnumdepth \z@\else \@ne\fi
          \else
            \z@
          \fi
          \csname add#1tocentry\endcsname{}{\@currenttocentry}%
        \else
%    \end{macrocode}
% \changes{v3.31}{2020/06/06}{using \cs{\dots tocformat}}%
% \begin{description}
% \item[ToDo:] Move \cs{autodot} to the definition of \cs{\dots tocformat}.
% \end{description}
%    \begin{macrocode}
        \@ifundefined{#1tocformat}{%
          \expandafter\gdef\csname #1tocformat\endcsname
          {\csname the#1\endcsname}%
        }{}%
        \csname add#1tocentry\endcsname{\csname #1tocformat\endcsname}{%
          \@currenttocentry}%
        \fi
      \fi
      \ExecuteDoHook{heading/endgroup/#1}%  
    }%
  \fi
  \let\IfUsePrefixLine\scr@IfUsePrefixLineWarning
  \@xsect{\glueexpr #5\relax}%
}
%    \end{macrocode}
% \begin{macro}{\scr@ds@tocentry,\@currenttocentry}
% \changes{v3.10}{2011/08/30}{added}
% \changes{v3.22}{2016/07/12}{\cs{scr@ds@tocentry} renamed to \cs{@currenttocentry}}
% \begin{macro}{\scr@ds@head,\@currentheadentry}
% \changes{v3.10}{2011/08/30}{added}
% \changes{v3.22}{2016/07/12}{\cs{scr@ds@head} renamed to \cs{@currentheadentry}}
% \begin{macro}{\@currentlabelname}
% \changes{v3.22}{2016/07/12}{added}
% \begin{macro}{\@scr@tempswatrue,\@scr@tempswafalse,\if@scr@tempswa}
% \changes{v3.10}{2011/08/30}{added}
% \begin{macro}{\@currentusenumbertrue,\@currentusenumberfalse,\if@currentusenumber}
% \changes{v3.27}{2019/07/08}{added}
% \begin{macro}{\scr@istest}
% \changes{v3.10}{2011/08/30}{added}
% We are using several \pkg*{scrbase} options to handle the extended
% optional argument features.
% \begin{description}
% \item[ToDo:] Is there a general initialization hook to make additional such
%   options at package author level possible?
% \end{description}
%    \begin{macrocode}
\DefineFamily{KOMAarg}
\DefineFamilyMember[.section]{KOMAarg}
\providecommand*\@currenttocentry{}
\providecommand*\@currentheadentry{}
\providecommand*\@currentlabelname{}
\FamilyStringKey[.section]{KOMAarg}{tocentry}{\@currenttocentry}
\FamilyStringKey[.section]{KOMAarg}{head}{\@currentheadentry}
\IfLTXAtLeastTF{2020/10/01}{%
  \NewHook{\KOMAClassName/option/section/reference/fallback}%
}{}%
\DefineFamilyKey[.section]{KOMAarg}{reference}{%
  \scr@ifundefinedorrelax{NR@gettitle}{%
    \scr@ifundefinedorrelax{GetTitleString}{%
%    \end{macrocode}
% \changes{v3.41}{2023/06/29}{substitute definition of \cs{@currentlabelname}
%   changed}
% This case is complicated. Up to \LaTeX{} 2022/11/01 it was good enough to
% use:
% \begin{verbatim}
% \def\@currentlabelname{#1}
% \end{verbatim}
% But from \LaTeX{} 2023/06/01 the kernel definition of \cs{label}
% additionally writes \cs{@currentlabelname} to the \file{aux}-file. So this
% breaks, if \cs{label} is part of the argument, because it results in an
% infinite expansion loop. To avoid this, the code has been changed. To
% remove \cs{label}, \cs{index}, \cs{glossary}.
% \begin{description}
% \item[ToDo:] The current \cs{@currentlabelname} code is only a workaround
%   for \href{https://sourceforge.net/p/koma-script/tickets/55/}{issue 55}.
%   Maybe using an empty value would be better. But in this case, what to do
%   with \cs{TG@gettitle} and \cs{ztitlerefsetup}?
% \end{description}
%    \begin{macrocode}
      \begingroup
        \let\label\@gobble
        \let\index\@gobble
        \let\glossary\@gobble
        \scr@ifundefinedorrelax{UseHook}{}{%
          \UseHook{\KOMAClassName/option/section/reference/fallback}%
        }%
        \protected@edef\@currentlabelname{#1}%
      \expandafter\expandafter\expandafter\endgroup
      \expandafter\def\expandafter\@currentlabelname\expandafter{%
        \@currentlabelname}%
    }{%
      \GetTitleString{#1}%
      \let\@currentlabelname\GetTitleStringResult
    }%
  }{%
    \NR@gettitle{#1}%
  }%
  \scr@ifundefinedorrelax{TR@gettitle}{}{%
    \expandafter\TR@gettitle\expandafter{\@currentlabelname}%
  }%
  \scr@ifundefinedorrelax{ztitlerefsetup}{}{%
    \ztitlerefsetup{title=\@currentlabelname}%
  }%
  \FamilyKeyStateProcessed
}
\FamilyInverseBoolKey[.section]{KOMAarg}{nonumber}{@currentusenumber}
\newcommand*{\scr@istest}{}
\def\scr@istest#1=#2\@nil{%
  \ifx\relax#2\relax\@scr@tempswafalse\else\@scr@tempswatrue\fi
}
\newif\if@scr@tempswa
%    \end{macrocode}
% \end{macro}^^A \scr@istest
% \end{macro}^^A \if@currentusenumber
% \end{macro}^^A \if@scr@tempswa
% \end{macro}^^A \@currentlabelname
% \end{macro}^^A \scr@ds@head,\@currentheadentry
% \end{macro}^^A \scr@ds@tocentry,\@currenttocentry
%
% \begin{command}{\sectionlinesformat,\sectioncatchphraseformat}
% \changes{v3.19}{2015/07/17}{added}
% Headings with style \texttt{section} can either be free-standing or
% in-line. Free-standing headings are printed using:
% \begin{quote}
%   \cs{sectionlinesformat}\marg{level name}\marg{indent}\marg{number}\marg{text}
% \end{quote}
% The in-line headings are printed using:
% \begin{quote}
%   \cs{sectioncatchphraseformat}\marg{level name}\marg{indent}\marg{number}\marg{text}
% \end{quote}
% The \meta{level name} can also be interpreted as the section command
% name. The \meta{indent} is the horizontal indent of the first line of the
% heading. The \meta{number} is either empty for not numbered headings or the
% formatted number to be used. \meta{text} is the formatted text to be
% used. Formatted text means, the fonts are already set and \cs{@@par} for the
% end of paragraph already has been added to the \meta{text} of
% \cs{sectionlinesformat}. But if the user uses more paragraphs than this
% final one, the user is responsible to prevent page breaks!
% \begin{description}
% \item[Note:] Argument \meta{indent} is a kind of design mistake. It would
%   have been better to get the indent by the attributes of the \meta{level
%   name}. However having an argument makes it easier for users to redefine
%   the command.
% \end{description}
%    \begin{macrocode}
\newcommand{\sectionlinesformat}[4]{%
  \@hangfrom{\hskip #2#3}{#4}%
}
\newcommand{\sectioncatchphraseformat}[4]{%
  \hskip #2#3#4%
}
%    \end{macrocode}
% \end{command}^^A \sectionlinesformat,\sectioncatchphraseformat
% \end{macro}^^A \@sect
%
% \begin{macro}{\@ssect}
% \changes{v3.19}{2015/07/17}{re-definition because of several new features}
% \begin{macro}{\scr@latex@ssect}
% \changes{v3.27}{2019/07/24}{added to be used by \pkg*{scrhack})}
% \changes{v3.30}{2020/02/25}{orphan space removed}
% The star variant of section commands do not use \cs{@sect} but
% \cs{@ssect}. Here we have the problem, that the command name argument is
% missing. The whole definition is very similar to \cs{@sect} but without a
% number, ToC entry or running head.
% \begin{description}
% \item[Note:] we do not have to support an extended optional argument,
%   because there isn't an optional argument.
% \item[ToDo:] It would be better not to redefine an internal \LaTeX{} kernel
%   macro, but use a own definition in our own \cs{scr@startsection}. But in
%   this case packages, that use \cs{@ssect} would not automatically support
%   several \KOMAScript{} features.
% \item[Note:] \cs{chapter} and \cs{part} do not use \cs{@ssect} but
%   \cs{SecDef}.
% \end{description}
%    \begin{macrocode}
\def\scr@latex@ssect#1#2#3#4#5{%
  \@tempskipa #3\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #4{%
        \@hangfrom{\hskip #1}%
          \interlinepenalty \@M #5\@@par}%
    \endgroup
  \else
    \def\@svsechd{#4{\hskip #1\relax #5}}%
  \fi
  \@xsect{#3}}
\ifx\@ssect\scr@latex@ssect
\else
  \let\scr@latex@ssect\@ssect
  \ClassWarning{\KOMAClassName}{%
    Unexpected definition of \string\@ssect!\MessageBreak
    Please send information about this to\MessageBreak
    the KOMA-Script maintainer!\MessageBreak
    Maybe LaTeX will be broken by the redefinition\MessageBreak
    of \string\@ssect}%
\fi
%    \end{macrocode}
% \end{macro}^^A \scr@latex@ssect
%    \begin{macrocode}
%<trace>\ClassInfo{\KOMAClassName}{redefining LaTeX kernel macro
%<trace>  \string\@ssect}
\def\@ssect#1#2#3#4#5{%
  \scr@ifundefinedorrelax{scr@s@ct@@nn@m@}{%
    \ClassWarning{\KOMAClassName}{Incompatible usage of
      \string\@ssect\space detected.\MessageBreak
      You've used the KOMA-Script implementation of
      \string\@ssect\MessageBreak
      from within a non compatible caller, that does not\MessageBreak
      \string\scr@s@ct@@nn@m@\space locally.\MessageBreak
      This could result in several error messages}%
    \def\scr@s@ct@@nn@m@{\string\scr@s@ct@@nn@m@}%
  }{%
%    \end{macrocode}
% \changes{v3.31}{2020/06/02}{execute counter reset list}
% Even for not numbered sections, the reset list für the corresponding section
% counter has to be executed.
%    \begin{macrocode}
%<!v4>    \expandafter\ifnum\scr@v@is@ge{3.31}\relax
      \begingroup
        \let\@elt\@stpelt
        \csname cl@\scr@s@ct@@nn@m@\endcsname
      \endgroup
%<!v4>    \fi
  }%
%    \end{macrocode}
% \changes{v3.26}{2018/09/18}{support for \cs{scr@sect@runin}}
% \changes{v3.27}{2019/02/02}{defining \cs{IfUseNumber} locally}
%    \begin{macrocode}
  \ifdim
    \scr@sect@runin{\z@}{\p@}{\glueexpr #3\relax}>\z@
    \begingroup
      \let\IfUseNumber\@secondoftwo
%    \end{macrocode}
% \changes{v3.21}{2016/06/12}{missing \cs{nobreak} added}
% \changes{v3.27}{2019/02/02}{\cs{ExecuteDoHook} added}
%    \begin{macrocode}
      \edef\reserved@a{%
        \noexpand\ExecuteDoHook{heading/begingroup/\scr@s@ct@@nn@m@}%
      }\reserved@a
      #4{\nobreak\interlinepenalty \@M
        \expandafter\sectionlinesformat\expandafter{\scr@s@ct@@nn@m@}%
        {\glueexpr #1\relax}\@empty{#5}\@@par}%
      \edef\reserved@a{%
        \noexpand\ExecuteDoHook{heading/endgroup/\scr@s@ct@@nn@m@}%
      }\reserved@a
    \endgroup
  \else
    \edef\@svsechd{%
      \unexpanded{\let\IfUseNumber\@secondoftwo}%
      \noexpand\ExecuteDoHook{heading/begingroup/\scr@s@ct@@nn@m@}%
      \unexpanded{#4}{%
%    \end{macrocode}
% \changes{v3.21}{2016/06/12}{missing \cs{nobreak} added}
%    \begin{macrocode}
        \noexpand\nobreak
        \noexpand\sectioncatchphraseformat{\scr@s@ct@@nn@m@}%
        \unexpanded{{\glueexpr #1\relax}\@empty{#5}}%
      }%
      \noexpand\ExecuteDoHook{heading/endgroup/\scr@s@ct@@nn@m@}%
    }%
  \fi
  \let\scr@s@ct@@nn@m@\relax
  \@xsect{\glueexpr #3\relax}%
}
%</body>
%    \end{macrocode}
% \end{macro}^^A \@ssect
% \begin{macro}{\@xsect}
% \changes{v3.26}{2018/09/18}{re-definition}
% \changes{v3.30}{2020/02/25}{orphan space removed}
% \begin{macro}{\scr@latex@xsect}
% \changes{v3.27}{2019/07/24}{added to be used by \pkg*{scrhack}}
% We need a differentiation with \cs{scr@sect@runin} to be initialized in
% \cs{@xsect} and also need to reset this internal macro at the end.
% \begin{description}
% \item[ToDo:] It would be better not to redefine the internal \LaTeX{} kernel
%   macro, but to define and use our own one.
% \end{description}
%    \begin{macrocode}
%<*body>
\def\scr@latex@xsect#1{%
  \@tempskipa #1\relax
  \ifdim \@tempskipa>\z@
    \par \nobreak
    \vskip \@tempskipa
    \@afterheading
  \else
    \@nobreakfalse
    \global\@noskipsectrue
    \everypar{%
      \if@noskipsec
        \global\@noskipsecfalse
       {\setbox\z@\lastbox}%
        \clubpenalty\@M
        \begingroup \@svsechd \endgroup
        \unskip
        \@tempskipa #1\relax
        \hskip -\@tempskipa
      \else
        \clubpenalty \@clubpenalty
        \everypar{}%
      \fi}%
  \fi
  \ignorespaces
}
\ifx\@xsect\scr@latex@xsect\else
  \let\scr@latex@xsect\@xsect
  \ClassWarning{\KOMAClassName}{%
    Unexpected definition of \string\@xsect!\MessageBreak
    Please send information about this to\MessageBreak
    the KOMA-Script maintainer!\MessageBreak
    Maybe LaTeX will be broken by the redefinition\MessageBreak
    of \string\@xsect}%
\fi
%    \end{macrocode}
% \end{macro}^^A \scr@latex@xsect
%    \begin{macrocode}
%<trace>\ClassInfo{\KOMAClassName}{redefining LaTeX kernel macro
%<trace> \string\@xsect}
\def\@xsect#1{%
  \@ifundefined{scr@sect@runin}{%
    \def\scr@sect@runin##1##2##3{##3}%
  }%
  \@tempskipa #1\relax
  \ifdim \scr@sect@runin{\z@}{\p@}{\@tempskipa}>\z@
    \par \nobreak
    \vskip \@tempskipa
    \@afterheading
  \else
    \@nobreakfalse
    \global\@noskipsectrue
    \everypar{%
      \if@noskipsec
        \global\@noskipsecfalse
       {\setbox\z@\lastbox}%
        \clubpenalty\@M
        \begingroup \@svsechd \endgroup
        \unskip
        \@tempskipa #1\relax
        \ifdim \@tempskipa<\z@
          \hskip -\@tempskipa
        \else
          \hskip \@tempskipa
        \fi
      \else
        \clubpenalty \@clubpenalty
        \everypar{}%
      \fi}%
  \fi
  \def\scr@sect@runin##1##2##3{##3}%
  \ignorespaces
}
%</body>
%    \end{macrocode}
% \end{macro}^^A \@xsect
%
% \begin{command}{\IfUsePrefixLine}
% \changes{v3.18}{2015/06/09}{added}
% \begin{macro}{\scr@IfUsePrefixLineWarning}
% \changes{v3.18}{2015/06/09}{added}
% This command makes sense inside the section commands only. But users often
% tried to use it outside instead of \cs{ifnumbered}. This mostly has resulted
% in a undefined command sequence error users did not understand. So now, we
% use an internal definition to be uses outside the section commands, that
% results in a warning with more information about the mistake and that does
% neither use the first nor the second argument (instead of using both of them).
%    \begin{macrocode}
%<*body>
\newcommand*{\scr@IfUsePrefixLineWarning}[2]{%
  \ClassWarning{\KOMAClassName}{%
    Usage of \string\IfUsePrefixLine\space outside
    section command\MessageBreak
    style makes no sense, because the behaviour not only\MessageBreak
    depends on class options but also on the run-time\MessageBreak
    section command style.\MessageBreak
    Neither the then-code nor the else-code will be\MessageBreak
    executed.\MessageBreak
    You may change this and avoid the warning by LOCALLY\MessageBreak
    setting \string\IfUsePrefixLine\space to either
    \string\@firstoftwo\space or\MessageBreak
    \string\@secondoftwo\space before using it%
  }%
}
\newcommand*{\IfUsePrefixLine}[2]{}
\let\IfUsePrefixLine\scr@IfUsePrefixLineWarning
%</body>
%    \end{macrocode}
% \end{macro}^^A \scr@IfUsePrefixLineWarning
% \end{command}^^A \IfUsePrefixLine
%
%
% \begin{command}{\raggedsection}
% \changes{v2.3h}{1996/01/20}{added}
% Should be either \cs{raggedleft}, \cs{raggedright}, \cs{centering} or
% empty. Empty means no change, which is usually justified. Users could also
% use other alignment/justification commands like the commands of package
% \pkg{ragged2e}. The initial value is \cs{raggedright}.
% \begin{description}
% \item[Note:] We are here in the class. So usually \cs{raggedright} is the
%   \cs{raggedright} of the \LaTeX{} kernel. This also means: no hyphenation.
% \item[Note:] We define is already here, because it is not only used for
%   style \texttt{section} but also as initial default of the \emph{higher
%   level} styles.
% \end{description}
%    \begin{macrocode}
%<*body>
\newcommand*{\raggedsection}{}
\let\raggedsection\raggedright
%</body>
%    \end{macrocode}
% \end{command}^^A \raggedsection
%
%
% All the options of \cs{DeclareSectionCommand} are options of family
% \texttt{KOMAarg} member \texttt{.dsc}.
%    \begin{macrocode}
%<*body>
\DefineFamily{KOMAarg}
\DefineFamilyMember[.dsc]{KOMAarg}
%    \end{macrocode}
% \begin{command}{\DeclareSectionCommandStyleOption}
% \changes{v3.17}{2015/03/23}{added}
% \begin{command}{\AddToSectionCommandOptionsDoList,\RelaxSectionCommandOptions}
% \changes{v3.20}{2015/11/18}{added}
% \begin{macro}{\@AddToSectionCommandOptionsDoList,\scr@dsc@doopts}
% \changes{v3.20}{2015/11/18}{added}
% The basic command to define a section command style option is:
% \begin{quote}
%   \cs{DeclareSectionCommandStyleOption}\marg{style}\marg{option}\marg{definition}
% \end{quote}
% Is similar to a corresponding \cs{DefineFamilyKey}. But the option
% definition is not done immediately but added to
% \cs{scr@dsc@style@\meta{style}@options} which is executed later inside
% \cs{DeclareSectionCommand} after detection of the \opt{style} option. The
% option is also added to the \emph{section command options do list} of
% \cs{scr@dsc@doopts} using
% \begin{quote}
%   \cs{AddToSectionCommandOptionsDoList}\marg{option}
% \end{quote}
% This is used, e.g., to relax all options inside \cs{DeclareSectionCommand}
% using \cs{RelaxSectionCommandOptions} to not have options defined, that are
% used by one style, while declaring a command of another style, that does not
% support the option.
% \begin{description}
% \item[Note:] We also could have used another option member for every style
%   to avoid such problems. With \LaTeX3 also usage of
%   \pkg[https://www.ctan.org/pkg/l3kernel]{l3keys}, which provides a kind of
%   hierarchical options, would have been possible.
% \end{description}
%    \begin{macrocode}
\newcommand*{\DeclareSectionCommandStyleOption}[3]{%
  \AddToSectionCommandOptionsDoList{#2}%
  \expandafter\g@addto@macro\csname scr@dsc@style@#1@options\endcsname{%
    \DefineFamilyKey[.dsc]{KOMAarg}{#2}{#3}%
  }%
}
\newcommand*{\AddToSectionCommandOptionsDoList}[1]{%
  \kernel@ifnextchar [%]
    {\@AddToSectionCommandOptionListWithDefault{#1}}%
    {\l@addto@macro\scr@dsc@doopts{\do{#1}}}%
}
\newcommand*{\@AddToSectionCommandOptionListWithDefault}{}
\def\@AddToSectionCommandOptionListWithDefault#1[#2]{%
  \l@addto@macro\scr@dsc@doopts{\do[{#2}]{#1}}
}%
\newcommand*{\scr@dsc@doopts}{}
\newcommand*{\RelaxSectionCommandOptions}{%
  \begingroup
    \def\do@endgroup{\endgroup}%
    \def\do{%
      \kernel@ifnextchar [%]
        {\@do}%
        {\@do[]}%
    }%
    \def\@do[##1]##2{%
      \l@addto@macro\do@endgroup{\RelaxFamilyKey[.dsc]{KOMAarg}{##2}}%
    }%
    \scr@dsc@doopts
  \do@endgroup
}
%    \end{macrocode}
% \end{macro}^^A \@AddToSectionCommandOptionsListWithDefault,\scr@dsc@dooptions
% \end{command}^^A \AddToSectionCommandOptionsDoList,\RelaxSectionCommandOptions
% \end{command}^^A \DeclareSectionCommandStyleOption
% We provide several kinds of options:
% \begin{command}{\DeclareSectionCommandDummyOption}
% \changes{v3.17}{2015/03/23}{added}
% \changes{v3.20}{2015/11/18}{removed}
% \end{command}^^A \DeclareSectionCommandDummyOption
% \begin{command}{\DeclareSectionCommandStyleLengthOption,
%                 \DeclareSectionCommandStyleNumberOption,
%                 \DeclareSectionCommandStyleFontOption}
% \changes{v3.17}{2015/03/23}{added}
% \changes{v3.20}{2016/04/25}{argument expansion}
% \changes{v3.38}{2022/09/16}{copy option added}
% All these commands have the four  arguments:
% \begin{quote}
%   \marg{style}\marg{option}\marg{prefix}\marg{postfix}
% \end{quote}
% Most of the commands all correspond to \pkg*{scrbase} option declaration
% commands:
% \begin{center}
%   \begin{tabular}{lll}
%     \cs{DeclareSectionCommand} & \pkg*{scrbase} & meaning \\\hline
%     \cs{DeclareSectionCommandStyleLengthOption} 
%                                  & \cs{FamilyLengthMacroKey} & length option\\
%     \cs{DeclareSectionCommandStyleNumberOption}
%                                  & \cs{FamilyCounterMacroKey} & numerical option\\
%     \cs{DeclareSectionCommandStyleFontOption}
%                                  & & font element option\\
%     \end{tabular}
% \end{center}
% The \meta{macro} of the corresponding \pkg*{scrbase} command is build by
% \meta{prefix}\cs{scr@dsc@current}\meta{postfix}. For
% \cs{DeclareSectionCommandStyleOption} the build name is the font
% element instead. However, before using (setting) the option,
% \cs{scr@dsc@current} has to be defined to be \meta{level name}
% (aka \emph{section command name}). Depending on \cs{ifscr@dsc@expandtopt}
% (see option \opt{expandtopt}) lengths given as argument to the option of
% \cs{DeclareSectionCommandStyleLengthOption} is expanded to a \texttt{pt}
% value instead of used as it is.
%    \begin{macrocode}
\newcommand*{\DeclareSectionCommandStyleLengthOption}[4]{%
  \DeclareSectionCommandStyleOption{#1}{#2}{%
    \protected@edef\reserved@a{%
      \noexpand\FamilySetLength{KOMAarg}{#2}{\noexpand\@tempskipa}{##1}%
    }\reserved@a
    \ifx\FamilyKeyState\FamilyKeyStateProcessed
      \ifscr@dsc@expandtopt
        \expandafter\edef\csname #3\scr@dsc@current#4\endcsname{%
          \the\@tempskipa}%
      \else
        \expandafter\edef\csname #3\scr@dsc@current#4\endcsname{##1}%
      \fi
    \fi
  }%
  \DeclareSectionCommandStyleOption{#1}{#2:}{%
    \scr@ifundefinedorrelax{#3##1#4}{\FamilyKeyStateUnknownValue}{%
      \ifscr@dsc@expandtopt
        \setlength{\@tempskipa}{\@nameuse{#3##1#4}}%
        \expandafter\edef\csname #3\scr@dsc@current#4\endcsname{\the\@tempskipa}%
      \else
        \@namedef{#3\scr@dsc@current#4}{\@nameuse{#3##1#4}}%
      \fi
      \FamilyKeyStateProcessed
    }%
  }%
}
\newcommand*{\DeclareSectionCommandStyleNumberOption}[4]{%
  \DeclareSectionCommandStyleOption{#1}{#2}{%
    \protected@edef\reserved@a{%
      \noexpand\FamilySetCounterMacro{KOMAarg}{#2}{\noexpand\reserved@a}{##1}
    }\reserved@a
    \ifx\FamilyKeyState\FamilyKeyStateProcessed
      \expandafter\let\csname #3\scr@dsc@current#4\endcsname\reserved@a
    \fi
  }%
  \DeclareSectionCommandStyleOption{#1}{#2:}{%
    \scr@ifundefinedorrelax{#3##1#4}{\FamilyKeyStateUnknownValue}{%
      \ifscr@dsc@expandtopt
        \@tempcnta=\@nameuse{#3##1#4}%
        \expandafter\edef\csname #3\scr@dsc@current#4\endcsname{\the\@tempcnta}%
      \else
        \@namedef{#3\scr@dsc@current#4}{\@nameuse{#3##1#4}}%
      \fi
      \FamilyKeyStateProcessed
    }%
  }%
}
\newcommand*{\DeclareSectionCommandStyleFontOption}[4]{%
  \DeclareSectionCommandStyleOption{#1}{#2}{%
    \IfExistskomafont{#3\scr@dsc@current#4}{%
      \setkomafont
    }{%
      \newkomafont
    }{#3\scr@dsc@current#4}{##1}%
    \FamilyKeyStateProcessed
  }%
  \DeclareSectionCommandStyleOption{#1}{#2:}{%
    \IfExistskomafont{#3##1#4}{%
      \IfExistskomafont{#3\scr@dsc@current#4}{%
        \setkomafont
      }{%
        \newkomafont
      }{#3\scr@dsc@current#4}{\usekomafont{#3##1#4}}%
      \FamilyKeyStateProcessed
    }{\FamilyKeyStateUnknownValue}%
  }%
}
%    \end{macrocode}
% \end{command}^^A \DeclareSectionCommandStyleLengthOption …
% \begin{command}{\DeclareSectionCommandStyleFuzzyOption}
% \changes{v3.26}{2018/09/18}{added}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} renamed to \cs{Ifstr}}
% \changes{v3.38}{2022/09/16}{copy option added}
% We also have a kind of fuzzy option. A fuzzy option defined by
% \begin{quote}
%   \cs{DeclareSectionCommandStyleFuzzyOption}\meta{style}\meta{option}%
%   \meta{prefix}\meta{postfix}\meta{also}
% \end{quote}
% is an option, that provides three values: \texttt{true} (and the aliases
% \texttt{on} and \texttt{yes}), \texttt{false} (and the aliases \texttt{off}
% and \texttt{no}), and a third one \meta{also}. It defines a macro with the
% name \meta{prefix}\cs{scr@dsc@current}\meta{postfix} and three arguments. If
% the option is true, the macro expands to the first argument. If the option
% is false, the macro expands to the second argument. If the option is
% \meta{also} the macro expands to the third argument.
%    \begin{macrocode}
\newcommand*{\DeclareSectionCommandStyleFuzzyOption}[5]{%
  \DeclareSectionCommandStyleOption{#1}{#2}{%
    \FamilySetBool{KOMAarg}{#2}{@tempswa}{##1}%
    \ifx\FamilyKeyState\FamilyKeyStateProcessed
      \if@tempswa
        \expandafter\def\csname #3\scr@dsc@current#4\endcsname
        ####1####2####3{####1}%
      \else
        \expandafter\def\csname #3\scr@dsc@current#4\endcsname
        ####1####2####3{####2}%
      \fi
    \else
      \Ifstr{##1}{#5}{%
        \expandafter\def\csname #3\scr@dsc@current#4\endcsname
        ####1####2####3{####3}%
        \FamilyKeyStateProcessed
      }{}%
    \fi
  }%
  \DeclareSectionCommandStyleOption{#1}{#2:}{%
    \scr@ifundefinedorrelax{#3##1#4}{\FamilyKeyStateUnknownValue}{%
      \ifscr@dsc@expandtopt
        \expandafter\let\csname #3\scr@dsc@current#4\expandafter\endcsname
        \csname #3##1#4\endcsname
      \else
        \expandafter\def\csname #3\scr@dsc@current#4\endcsname{%
          \@nameuse{#3##1#4}%
        }%
      \fi  
      \FamilyKeyStateProcessed
    }%
  }%
}
%    \end{macrocode}
% \end{command}^^A \DeclareSectionCommandStyleFuzzyOptions
%
% These types of options are used to define all the options of a style, e.g.,
% \begin{macro}{\scr@dsc@style@section@options}
% \changes{v3.17}{2015/03/23}{added}
% the option declarations of style \texttt{section}:
%    \begin{macrocode}
\newcommand*{\scr@dsc@style@section@options}{}
%    \end{macrocode}
% \begin{description}
% \item[\opt{indent=\meta{length}}:]
%   the left indent of the heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleLengthOption{section}
                                       {indent}{scr@}{@sectionindent}
%    \end{macrocode}
% \begin{description}
% \item[\opt{afterindent=\meta{fuzzy}}:]
%   \changes{v3.26}{2018/09/18}{option \opt{afterindent}}
%   fuzzy logic. A boolean value states whether to indent the first line
%   after the heading. With additional \opt{afterindent=bysign} the decision
%   depends on the sign of \opt{beforeskip}. Negative values of
%   \opt{beforeskip} result in no paragraph indent.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleFuzzyOption{section}
                                      {afterindent}{scr@}{@afterindent}
                                      {bysign}
%    \end{macrocode}
% \begin{description}
% \item[\opt{beforeskip=\meta{length}}:]
%   The vertical distance before the heading. If \opt{afterindent=bysign}, the
%   absolute value of \meta{length} is used. Otherwise the \meta{length} can
%   also be negative to move up the heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleLengthOption{section}
                                       {beforeskip}{scr@}{@beforeskip}
%    \end{macrocode}
% \begin{description}
% \item[\opt{runin=\meta{fuzzy}}:]
%   \changes{v3.26}{2018/09/18}{option \opt{runin}}
%   fuzzy logic. A boolean value states whether the heading is run-in (or
%   free-standing). With additional \opt{runin=bysign} the decision depends on
%   the sign of \opt{afterskip}. Negative values of \opt{afterskip} result in
%   a run-in heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleFuzzyOption{section}
                                      {runin}{scr@}{@runin}
                                      {bysign}
%    \end{macrocode}
% \begin{description}
% \item[\opt{afterskip=\meta{length}}:]
%   In case of a run-in heading (see option \opt{runin}) the horizontal
%   distance after the heading. In case of a free-standing heading the
%   vertical distance after the heading. If the run-in heading is activated by
%   a negative \meta{length}, the absolute value is used. Otherwise the
%   \meta{length} can also be negative, which usually does not make much
%   sense.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleLengthOption{section}
                                       {afterskip}{scr@}{@afterskip}
%    \end{macrocode}
% \begin{description}
% \item[\opt{font=\meta{font settings}}:]
%   The font setting for the font element of the heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleFontOption{section}{font}{}{}
%    \end{macrocode}
% \end{macro}^^A \scr@dsc@style@section@options
% \begin{macro}{\scr@dsc@def@style@section@command}
% \changes{v3.18}{2015/05/21}{added}
% \changes{v3.20}{2016/01/19}{inline section test fixed}
% Macro
% \begin{quote}
%   \cs{scr@dsc@def@style@section@command}\marg{command name}
% \end{quote}
% defines a section command \cs{\meta{command name}} of style
% \texttt{section}.
% \begin{description}
% \item[Note:] Here we already use our own \cs{scr@startsection} instead of
%   \cs{@startsection}. So maybe not redefining \cs{@startsection} or
%   redefining \cs{@startsection} only optionally wouldn't be a big step.
% \end{description}
%    \begin{macrocode}
\newcommand*{\scr@dsc@def@style@section@command}[1]{%
  \@namedef{#1}{%
    \scr@startsection{#1}%
    {\csname #1numdepth\endcsname}%
    {\csname scr@#1@sectionindent\endcsname}%
    {\csname scr@#1@beforeskip\endcsname}%
    {\csname scr@#1@afterskip\endcsname}%
    {%
      \ifdim\glueexpr\csname scr@#1@afterskip\endcsname >\z@
%<!v4>        \expandafter\ifnum\scr@v@is@gt{2.96}\relax
          \setlength{\parfillskip}{\z@ plus 1fil}%
%<!v4>        \fi
      \fi
      \raggedsection\normalfont\sectfont\nobreak
      \usekomafont{#1}%
    }%
  }%
}
%    \end{macrocode}
% \end{macro}^^A \scr@dsc@def@style@section@command
%
% \begin{macro}{\scr@dsc@style@chapter@options}
% \changes{v3.17}{2015/03/23}{added}
% Now, the option declaration of style \texttt{chapter} (similar, but not the
% same):
%    \begin{macrocode}
%<*book|report>
\newcommand*{\scr@dsc@style@chapter@options}{}
%    \end{macrocode}
% \begin{description}
% \item[\opt{pagestyle=\meta{name}}:]
%   The page style, that should be used for the starting page of the chapter
%   like outline level.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleOption{chapter}{pagestyle}{%
  \@namedef{\scr@dsc@current pagestyle}{#1}%
  \FamilyKeyStateProcessed
}%
%    \end{macrocode}
% \begin{description}
% \item[\opt{afterindent=\meta{fuzzy}}:]
%   \changes{v3.26}{2018/09/18}{option \opt{afterindent}}
%   fuzzy logic. A boolean value states whether to indent the first line
%   after the heading. With additional \opt{afterindent=bysign} the decision
%   depends on the sign of \opt{beforeskip}. Negative values of
%   \opt{beforeskip} result in no paragraph indent.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleFuzzyOption{chapter}
                                      {afterindent}{scr@}{@afterindent}
                                      {bysign}
%    \end{macrocode}
% \begin{description}
% \item[\opt{beforeskip=\meta{length}}:]
%   The vertical distance before the heading. If \opt{afterindent=bysign}, the
%   absolute value of \meta{length} is used. Otherwise the \meta{length} can
%   also be negative to move up the heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleLengthOption{chapter}
                                       {beforeskip}{scr@}{@beforeskip}
%    \end{macrocode}
% \begin{description}
% \item[\opt{innerskip=\meta{length}}:]
%   The vertical distance between the prefix line and the text (if a prefix
%   line is used).
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleLengthOption{chapter}
                                       {innerskip}{scr@}{@innerskip}
%    \end{macrocode}
% \begin{description}
% \item[\opt{afterskip=\meta{length}}:] 
%   The vertical distance after the heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleLengthOption{chapter}
                                       {afterskip}{scr@}{@afterskip}
%    \end{macrocode}
% \begin{description}
% \item[\opt{font=\meta{font settings}}:]
%   The font setting for the font element of the heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleFontOption{chapter}{font}{}{}
%    \end{macrocode}
% \begin{description}
% \item[\opt{prefixfont=\meta{font settings}}:]
%   The font setting for the font element of the prefix line of the heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleFontOption{chapter}{prefixfont}{}{prefix}
%    \end{macrocode}
% \end{macro}^^A \scr@dsc@style@chapter@options
% \begin{macro}{\scr@dsc@def@style@chapter@command}
% \changes{v3.18}{2015/05/22}{added}
% Macro
% \begin{quote}
%   \cs{scr@dsc@def@style@chapter@command}\marg{command name}
% \end{quote}
% defines a section command \cs{\meta{command name}} of style
% \texttt{chapter}. We need some more macros to be initialized than with
% \cs{scr@dsc@def@style@section@command}.
% \begin{description}
% \item[ToDo:] Make a more generic (and general) interface for this.
% \end{description}
%    \begin{macrocode}
\newcommand*{\scr@dsc@def@style@chapter@command}[1]{%
  \@namedef{#1}{\scr@startchapter{#1}}%
  \@namedef{@#1}{\scr@@startchapter{#1}}%
  \@namedef{@s#1}{\scr@@startschapter{#1}}%
  \@namedef{@make#1head}{\scr@makechapterhead{#1}}%
  \@namedef{@makes#1head}{\scr@makeschapterhead{#1}}%
  \@namedef{@@make#1head}{\scr@@makechapterhead{#1}}%
  \@namedef{@@makes#1head}{\scr@@makeschapterhead{#1}}%
  \@namedef{set#1preamble}{\set@preamble{#1}}%
}
%</book|report>
%    \end{macrocode}
% \begin{macro}{\scr@dsc@style@chapter@subcommands}
% \changes{v3.30}{2020/04/08}{added}
% Internal name list of additional commands to be defined for style
% \texttt{chapter} commands. This is used, to decide, if a new section command
% declaration is really new or already redefines some commands. This should
% help to avoid, e.g., defining a new chapter command \cs{schapter}, if
% \cs{chapter} is already defines, because \cs{schapter} would redefine the
% \cs{@schapter} of \cs{chapter}.
%    \begin{macrocode}
\newcommand*{\scr@dsc@style@chapter@subcommands}[1]{%
  @#1,@s#1,@make#1head,@makes#1head,@@make#1head,@@makes#1head,set#1preamble%
}
%    \end{macrocode}
% \end{macro}^^A \scr@dsc@style@chapter@subcommands
% \end{macro}^^A \scr@dsc@def@style@chapter@command
%
% \begin{macro}{\scr@dsc@style@part@options}
% \changes{v3.17}{2015/03/23}{added}
% Now the options for style \texttt{part} (very similar to \texttt{chapter}):
%    \begin{macrocode}
\newcommand*{\scr@dsc@style@part@options}{}
%    \end{macrocode}
% \begin{description}
% \item[\opt{pagestyle=\meta{name}}:]
%   The page style, that should be used for the starting page of the chapter
%   like outline level.
% \end{description}
%    \begin{macrocode}
%<*report|book>
\DeclareSectionCommandStyleOption{part}{pagestyle}{%
  \@namedef{\scr@dsc@current pagestyle}{#1}%
  \FamilyKeyStateProcessed
}%
%</report|book>
%    \end{macrocode}
% \begin{description}
% \item[\opt{afterindent=\meta{fuzzy}}:]
%   \changes{v3.26}{2018/09/18}{option \opt{afterindent}}
%   fuzzy logic. A boolean value states whether to indent the first line
%   after the heading. With additional \opt{afterindent=bysign} the decision
%   depends on the sign of \opt{beforeskip}. Negative values of
%   \opt{beforeskip} result in no paragraph indent.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleFuzzyOption{part}
                                      {afterindent}{scr@}{@afterindent}
                                      {bysign}
%    \end{macrocode}
% \begin{description}
% \item[\opt{beforeskip=\meta{length}}:]
%   The vertical distance before the heading. If \opt{afterindent=bysign}, the
%   absolute value of \meta{length} is used. Otherwise the \meta{length} can
%   also be negative to move up the heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleLengthOption{part}
                                       {beforeskip}{scr@}{@beforeskip}
%    \end{macrocode}
% \begin{description}
% \item[\opt{innerskip=\meta{length}}:]
%   The vertical distance between the prefix line and the text (if a prefix
%   line is used).
% \end{description}
%    \begin{macrocode}
%<*book|report>
\DeclareSectionCommandStyleLengthOption{part}
                                       {innerskip}{scr@}{@innerskip}
%</book|report>
%    \end{macrocode}
% \begin{description}
% \item[\opt{afterskip=\meta{length}}:] 
%   The vertical distance after the heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleLengthOption{part}
                                       {afterskip}{scr@}{@afterskip}
%    \end{macrocode}
% \begin{description}
% \item[\opt{font=\meta{font settings}}:]
%   The font setting for the font element of the heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleFontOption{part}{font}{}{}
%    \end{macrocode}
% \begin{description}
% \item[\opt{prefixfont=\meta{font settings}}:]
%   The font setting for the font element of the prefix line of the heading.
% \end{description}
%    \begin{macrocode}
\DeclareSectionCommandStyleFontOption{part}{prefixfont}{}{prefix}
%    \end{macrocode}
% \end{macro}^^A \scr@dsc@style@part@options
% \begin{macro}{\scr@dsc@def@style@part@command}
% \changes{v3.18}{2015/05/23}{added}
% Macro
% \begin{quote}
%   \cs{scr@dsc@def@style@part@command}\marg{command name}
% \end{quote}
% defines a section command \cs{\meta{command name}} of style
% \texttt{part}. This looks like a mixture of
% \cs{scr@dsc@def@style@section@command} and
% \cs{scr@dsc@def@style@chapter@command}.
% \begin{description}
% \item[ToDo:] Make a more generic (and general) interface for this.
% \end{description}
%    \begin{macrocode}
\newcommand*{\scr@dsc@def@style@part@command}[1]{%
  \@namedef{#1}{\scr@startpart{#1}}%
  \@namedef{@#1}{\scr@@startpart{#1}}%
  \@namedef{@s#1}{\scr@@startspart{#1}}%
%<book|report>  \@namedef{@end#1}{\scr@@endpart{#1}}%
%<book|report>  \@namedef{set#1preamble}{\set@preamble{#1}}%
}
%    \end{macrocode}
% \begin{macro}{\scr@dsc@style@part@subcommands}
% \changes{v3.30}{2020/04/08}{added}
% Internal name list of additional commands to be defined for style
% \texttt{part} commands.
%    \begin{macrocode}
\newcommand*{\scr@dsc@style@part@subcommands}[1]{%
  @#1,@s#1,@end#1,set#1preamble%
}
%    \end{macrocode}
% \end{macro}^^A \scr@dsc@style@part@subcommands
% \end{macro}^^A \scr@dsc@def@style@part@command
%
% \begin{macro}{\scr@dsc@style@section@neededoptionstest}
% \changes{v3.17}{2015/03/24}{added}
% \changes{v3.24}{2017/04/25}{initial values of \opt{indent} and
%   \opt{font}}
% \changes{v3.26}{2018/09/18}{supporting \opt{runin} and \opt{afterindent}}
% This macro checks, that all attributes needed for the definition of a
% section command (with style \texttt{section}) are properly setup. If not, an
% error is reported and the definition is prevented (using
% \cs{aftergroup}\cs{@gobbletwo} inside \cs{scr@declaresectioncommanderror}).
%    \begin{macrocode}
\newcommand*{\scr@dsc@style@section@neededoptionstest}{%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @sectionindent}{%
    \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                     {section indent}{indent}{0pt}%
    {\@namedef{scr@\scr@dsc@current @sectionindent}{0pt}}%
  }{}%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @beforeskip}{%
    \scr@declaresectioncommanderror{\scr@dsc@current}%
                                   {before section skip}{beforeskip}%
  }{}%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @afterskip}{%
    \scr@declaresectioncommanderror{\scr@dsc@current}%
                                   {after section skip}{afterskip}%
  }{}%
  \IfExistskomafont{\scr@dsc@current}{}{%
    \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                     {font}{font}{\normalsize}%
    {\newkomafont{\scr@dsc@current}{\normalsize}}%
  }%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @runin}{%
    \l@addto@macro\local@endgroup{%
      \ClassInfo{\KOMAClassName}
                {using compatibility default `runin=bysign'\MessageBreak
                  for `\expandafter\string\csname \scr@dsc@current\endcsname}%
      \expandafter\gdef\csname scr@\scr@dsc@current @runin\endcsname
      ##1##2##3{##3}%
    }%
  }{}%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @afterindent}{%
    \l@addto@macro\local@endgroup{%
      \ClassInfo{\KOMAClassName}
                {using compatibility default `afterindent=bysign'\MessageBreak
                  for `\expandafter\string\csname \scr@dsc@current\endcsname}%
      \expandafter\gdef\csname scr@\scr@dsc@current @afterindent\endcsname
      ##1##2##3{##3}%
    }%
  }{}%
}
%    \end{macrocode}
% \end{macro}^^A \scr@dsc@style@section@neededoptionstest
%
% \begin{macro}{\scr@dsc@style@chapter@neededoptionstest}
% \changes{v3.17}{2015/03/24}{added}
% \changes{v3.24}{2017/04/25}{initial values for all but level
%   \texttt{chapter}}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} renamed to \cs{Ifstr}}
% This macro checks, that all attributes needed for the definition of a
% chapter command (with style \texttt{chapter}) are properly setup. If not, an
% error is reported and the definition is prevented (using
% \cs{aftergroup}\cs{@gobbletwo} inside \cs{scr@declaresectioncommanderror}).
%    \begin{macrocode}
%<*book|report>
\newcommand*{\scr@dsc@style@chapter@neededoptionstest}{%
  \scr@ifundefinedorrelax{\scr@dsc@current pagestyle}{%
    \Ifstr{\scr@dsc@current}{chapter}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}%
                                     {initial page style}{pagestyle}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                       {initial page style}{pagestyle}%
                                       {\chapterpagestyle}%
      {\@namedef{\scr@dsc@current pagestyle}{\chapterpagestyle}}%
    }%
  }{}%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @innerskip}{%
    \Ifstr{\scr@dsc@current}{chapter}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}%
                                     {inner chapter skip}{innerskip}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                       {inner chapter skip}{innerskip}%
                                       {\scr@chapter@innerskip}%
      {\@namedef{scr@\scr@dsc@current @innerskip}{\scr@chapter@innerskip}}%
    }%
  }{}%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @beforeskip}{%
    \Ifstr{\scr@dsc@current}{chapter}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}%
                                     {before chapter skip}{beforeskip}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                       {before chapter skip}{beforeskip}%
                                       {\scr@chapter@beforeskip}%
      {\@namedef{scr@\scr@dsc@current @beforeskip}{\scr@chapter@beforeskip}}%
    }%
  }{}%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @afterskip}{%
    \Ifstr{\scr@dsc@current}{chapter}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}%
                                     {after chaper skip}{afterskip}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                       {after chaper skip}{afterskip}%
                                       {\scr@chapter@afterskip}%
      {\@namedef{scr@\scr@dsc@current @afterskip}{\scr@chapter@afterskip}}%
    }%
  }{}%
  \IfExistskomafont{\scr@dsc@current}{}{%
    \Ifstr{\scr@dsc@current}{chapter}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}{font}{font}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                       {font}{font}%
                                       {\usekomafont{chapter}}%
      {\newkomafont{\scr@dsc@current}{\usekomafont{chapter}}}%
    }%
  }%
  \IfExistskomafont{\scr@dsc@current prefix}{}{%
    \Ifstr{\scr@dsc@current}{chapter}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}%
                                     {prefix line font}{prefixfont}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                       {prefix line font}{prefixfont}%
                                       {\usekomafont{chapterprefix}}%
      {\newkomafont{\scr@dsc@current prefix}{\usekomafont{chapterprefix}}}%
    }%
  }{}%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @afterindent}{%
    \l@addto@macro\local@endgroup{%
      \ClassInfo{\KOMAClassName}
                {using compatibility default `afterindent=bysign'\MessageBreak
                  for `\expandafter\string\csname \scr@dsc@current\endcsname}%
      \expandafter\gdef\csname scr@\scr@dsc@current @afterindent\endcsname
      ##1##2##3{##3}%
    }%
  }{}%
}
%</book|report>
%    \end{macrocode}
% \end{macro}^^A \scr@dsc@style@chapter@neededoptionstest
%
% \begin{macro}{\scr@dsc@style@part@neededoptionstest}
% \changes{v3.17}{2015/03/24}{added}
% \changes{v3.24}{2017/04/25}{initial values for all but level \texttt{part}}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} renamed to \cs{Ifstr}}
% This macro checks, that all attributes needed for the definition of a
% part command (with style \texttt{part}) are properly setup. If not, an
% error is reported and the definition is prevented (using
% \cs{aftergroup}\cs{@gobbletwo} inside \cs{scr@declaresectioncommanderror}).
%    \begin{macrocode}
\newcommand*{\scr@dsc@style@part@neededoptionstest}{%
%<*book|report>
  \scr@ifundefinedorrelax{\scr@dsc@current pagestyle}{%
    \Ifstr{\scr@dsc@current}{part}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}%
                                     {initial page style}{pagestyle}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                       {initial page style}{pagestyle}%
                                       {\partpagestyle}%
      {\@namedef{\scr@dsc@current pagestyle}{\partpagestyle}}%
    }%
  }{}%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @innerskip}{%
    \Ifstr{\scr@dsc@current}{part}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}%
                                     {inner part skip}{innerskip}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                       {inner part skip}{innerskip}%
                                       {\scr@part@innerskip}%
      {\@namedef{scr@\scr@dsc@current @innerskip}{\scr@part@innerskip}}%
    }%
  }{}%
%</book|report>
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @beforeskip}{%
    \Ifstr{\scr@dsc@current}{part}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}%
                                     {before part skip}{beforeskip}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                       {before part skip}{beforeskip}%
                                       {\scr@part@beforeskip}%
      {\@namedef{scr@\scr@dsc@current @beforeskip}{\scr@part@beforeskip}}%
    }%
  }{}%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @afterskip}{%
    \Ifstr{\scr@dsc@current}{part}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}%
                                     {after part skip}{afterskip}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                       {after part skip}{afterskip}%
                                       {\scr@part@afterskip}%
      {\@namedef{scr@\scr@dsc@current @afterskip}{\scr@part@afterskip}}%
    }%
  }{}%
  \IfExistskomafont{\scr@dsc@current}{}{%
    \Ifstr{\scr@dsc@current}{part}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}{font}{font}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}{font}{font}%
                                       {\usekomafont{part}}%
      {\newkomafont{\scr@dsc@current}{\usekomafont{part}}}%
    }%
  }{}%
  \IfExistskomafont{\scr@dsc@current prefix}{}{%
    \Ifstr{\scr@dsc@current}{part}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}%
                                     {number font}{prefixfont}%
    }{%
      \scr@declaresectioncommandwarning{\scr@dsc@current}%
                                       {number font}{prefixfont}%
                                       {\usekomafont{partprefix}}%
      {\newkomafont{\scr@dsc@current prefix}{\usekomafont{partprefix}}}%
    }%
  }{}%
  \scr@ifundefinedorrelax{scr@\scr@dsc@current @afterindent}{%
    \l@addto@macro\local@endgroup{%
      \ClassInfo{\KOMAClassName}{%
        using compatibility default
%<article>        `afterindent=false'\MessageBreak
%<book|report>        `afterindent=true'\MessageBreak
        for `\expandafter\string\csname \scr@dsc@current\endcsname}%
      \expandafter\gdef\csname scr@\scr@dsc@current @afterindent\endcsname
      ##1##2##3%
%<article>      {##2}%
%<book|report>      {##1}%
    }%
  }{}%
}
%    \end{macrocode}
% \end{macro}^^A \scr@dsc@style@part@neededoptionstest
%
% \begin{macro}{\scr@dsc@parametersonlytrue,\scr@dsc@parametersonlyfalse,
%               \ifscr@dsc@parametersonly}
% \changes{v3.17}{2015/03/24}{added}
% \changes{v3.20}{2015/11/18}{dummy options removed}
% In the first option pass of \cs{DeclareSectionCommand} this is set to true,
% if the \opt{style} option is used without value, to change the settings of a
% command without running \cs{scr@dsc@def@style@\meta{style}@command} and
% therefore without defining the command itself.
% \begin{description}
% \item[ToDo:] Is this a not documented feature? What's the sense?
% \end{description}
%    \begin{macrocode}
\newif\ifscr@dsc@parametersonly
%    \end{macrocode}
% \end{macro}^^A \ifscr@dsc@parametersonly
%
% \begin{command}{\DeclareSectionCommand}
% \changes{v3.15}{2014/11/21}{added}
% \changes{v3.17}{2015/03/23}{re-implemented because of style depending
%   options}
% \changes{v3.18}{2015/05/22}{\cs{scr@dsc@def@style@\dots @command}
%   defines style depending macros}
%   \changes{v3.20}{2015/10/03}{using \cs{DeclareTOCStyleEntry}}
% The main command to define new section commands or redefine existing section
% commands. Incidentally it also defines the corresponding ToC entry command.
% \begin{quote}
%   \cs{DeclareSectionCommand}\oarg{options}\marg{command name}
% \end{quote}
% is used to define \cs{\meta{command name}}. The \meta{options} are used to
% specify the behave of the command. It is declared as optional
% argument. However for new commands not all the options are really optional.
% \begin{description}
% \item[Note:] Don't forget to declare all options, that are defined for
%   \cs{DeclareSectionCommand} also for \cs{ProvideSectionCommands}!
% \end{description}
%    \begin{macrocode}
\newcommand*{\DeclareSectionCommand}[2][]{%
  \edef\scr@dsc@current{#2}%
%    \end{macrocode}
% The first pass to get the:
% \begin{description}
% \item[\opt{style=\meta{name}}:]
%   style of the section command.
% \end{description}
%    \begin{macrocode}
  \scr@dsc@parametersonlyfalse
  \DefineFamilyKey[.dsc]{KOMAarg}{style}{%
    \IfArgIsEmpty{##1}{%
      \scr@dsc@parametersonlytrue
      \FamilyKeyStateProcessed
    }{%
      \scr@ifundefinedorrelax{scr@dsc@def@style@##1@command}{%
        \FamilyKeyStateUnknownValue
      }{%
        \@namedef{scr@\scr@dsc@current @style}{##1}%
        \FamilyKeyStateProcessed
      }%
    }%
  }%
%    \end{macrocode}
% \begin{description}
% \item[\opt{tocstyle=\meta{name}}:] \changes{v3.20}{2015/11/12}{early
%   evaluation of option \opt{tocstyle}} \changes{v3.27}{2019/10/02}{prefix
%   for storing directory style changed} style of the ToC entry. From
%   \pkg*{tocbasic} 3.27 the style name macro has been changed from
%   \cs{scr@dte@\meta{level name}@style} to \cs{scr@tso@\meta{level
%   name}@style}.
%   \begin{description}
%   \item[ToDo:] Don't support deprecated \cs{scr@dte@\meta{level name}@style}
%     any longer.
%   \end{description}
% \end{description}
%    \begin{macrocode}
  \DefineFamilyKey[.dsc]{KOMAarg}{tocstyle}{%
    \IfArgIsEmpty{##1}{%
      \@ifundefined{scr@dte@\scr@dsc@current @style}{}{%
        \expandafter\let\csname scr@dte@\scr@dsc@current @style\endcsname
        \relax
      }%
      \expandafter\let\csname scr@tso@\scr@dsc@current @style\endcsname
      \relax
      \FamilyKeyStateProcessed
    }{%
      \scr@ifundefinedorrelax{scr@dte@def@l@##1}{%
        \FamilyKeyStateUnknownValue
      }{%
        \@namedef{scr@dte@\scr@dsc@current @style}{##1}%
        \@namedef{scr@tso@\scr@dsc@current @style}{##1}%
        \FamilyKeyStateProcessed
      }%
    }%
  }%
%    \end{macrocode}
% \begin{description}
% \item[\texttt{@else@=\meta{value}}:] 
%   \changes{v3.20}{2015/11/18}{early evaluation of option \opt{tocstyle}}
%   All other options are ignored using the \opt{@else@} option, which simply
%   accepts every option. So the first pass does not report any unknown
%   options.
% \end{description}
%    \begin{macrocode}
  \DefineFamilyKey[.dsc]{KOMAarg}{@else@}{%
%<*trace>
    \typeout{TRACE: ignoring option `@else@=\detokenize{##1}'}%
    \typeout{TRACE:
      \string\scr@key@atlist=`\detokenize\expandafter{\scr@key@atlist}'}%
    \typeout{TRACE:
      \string\scr@key@name=`\detokenize\expandafter{\scr@key@name}'}%
    \typeout{TRACE:
      \string\scr@key@value=`\detokenize\expandafter{\scr@key@value}'}%
%</trace>
    \FamilyKeyStateProcessed
  }%
  \FamilyExecuteOptions[.dsc]{KOMAarg}{#1}%
%    \end{macrocode}
% \changes{v3.20}{2015/11/18}{Resetting options after first pass}
% After the first pass we have to reset these options.
%    \begin{macrocode}
  \RelaxFamilyKey[.dsc]{KOMAarg}{@else@}%
  \RelaxFamilyKey[.dsc]{KOMAarg}{tocstyle}%
  \RelaxFamilyKey[.dsc]{KOMAarg}{style}%
  \begingroup
    \scr@ifundefinedorrelax{scr@\scr@dsc@current @style}{%
      \scr@declaresectioncommanderror{\scr@dsc@current}
                                     {section command style}{style}%
    }{}%
  \endgroup
  \@firstofone{%
%    \end{macrocode}
% \changes{v3.20}{2015/11/12}{dummy options for main pass removed}
% Now, we know the style and can activate all the options of the style.
%    \begin{macrocode}
    \@nameuse{scr@dsc@style@\@nameuse{scr@\scr@dsc@current @style}@options}%
%    \end{macrocode}
% And initialize the ToC style.
%    \begin{macrocode}
    \scr@ifundefinedorrelax{scr@tso@\scr@dsc@current @style}{%
      \scr@ifundefinedorrelax{l@\scr@dsc@current}{%
        \@namedef{scr@tso@\scr@dsc@current @style}{default}%
      }{}%
    }{}%
%    \end{macrocode}
% \changes{v3.20}{2016/04/12}{\cs{@ifnextchar} replaced by
%   \cs{kernel@ifnextchar}}
% \changes{v3.21}{2016/06/12}{support for prefix \opt{tocentry} instead of
%   \opt{toc}}
% We also have to define options for all the ToC style options by adding the
% prefix \opt{toc} to the names of these options. This also means a ToC style
% option \opt{style} must not exist (because we already have an option
% \opt{tocstyle}). This is the main reason, \cs{DeclareTOCStyleEntry} has not
% such an option but a corresponding argument. Additionally ToC style options
% should never start with \opt{toc} (because of the ugly resulting
% \opt{toctoc\dots}).
%    \begin{macrocode}
    \let\scr@dsc@tocstyle@options\@empty
    \let\scr@dsc@extra@relax@opts\@empty
    \scr@ifundefinedorrelax{scr@tso@\scr@dsc@current @style}{%
      \expandafter\let
      \csname scr@tso@\scr@dsc@current @style\expandafter\endcsname
      \csname scr@dte@\scr@dsc@current @style\endcsname
    }{}%
    \scr@ifundefinedorrelax{scr@tso@\scr@dsc@current @style}{}{%
      \begingroup
        \def\scr@dte@current{#2}%
        \edef\reserved@a{%
          \noexpand\@ExecuteTOCEntryStyleInitCode{%
            \@nameuse{scr@tso@\scr@dsc@current @style}%
          }{\scr@dsc@current}%
        }\reserved@a
        \def\do@endgroup{\endgroup}%
        \def\do##1{%
          \l@addto@macro\do@endgroup{%
            \l@addto@macro\scr@dsc@extra@relax@opts{%
              \RelaxFamilyKey[.dsc]{KOMAarg}{toc##1}%
              \RelaxFamilyKey[.dsc]{KOMAarg}{tocentry##1}%
            }%
          }%
          \kernel@ifnextchar[%]
            {\@dodefault{##1}}%
            {\@donodefault{##1}}%
        }%
        \def\@donodefault##1{%
          \l@addto@macro\do@endgroup{%
            \DefineFamilyKey[.dsc]{KOMAarg}{toc##1}{%
              \l@addto@macro{\scr@dsc@tocstyle@options}{##1={####1},}%
              \FamilyKeyStateProcessed
            }%
            \DefineFamilyKey[.dsc]{KOMAarg}{tocentry##1}{%
              \l@addto@macro{\scr@dsc@tocstyle@options}{##1={####1},}%
              \FamilyKeyStateProcessed
            }%
          }%
        }%
        \def\@dodefault##1[##2]{%
          \l@addto@macro\do@endgroup{%
            \DefineFamilyKey[.dsc]{KOMAarg}{toc##1}[##2]{%
              \l@addto@macro{\scr@dsc@tocstyle@options}{##1={####1},}%
              \FamilyKeyStateProcessed
            }%
            \DefineFamilyKey[.dsc]{KOMAarg}{tocentry##1}[##2]{%
              \l@addto@macro{\scr@dsc@tocstyle@options}{##1={####1},}%
              \FamilyKeyStateProcessed
            }%
          }%
        }%
        \scr@dte@doopts
      \do@endgroup
    }%
%    \end{macrocode}
% Make option \opt{style} and \opt{tocstyle} (fast) dummies, because they
% already have been handled completely.
%    \begin{macrocode}
    \DefineFamilyKey[.dsc]{KOMAarg}{style}{\FamilyKeyStateProcessed}%
    \DefineFamilyKey[.dsc]{KOMAarg}{tocstyle}{\FamilyKeyStateProcessed}%
%    \end{macrocode}
% Some options exist for all section command styles and therefore are defined
% here and not a part of the style options:
% \begin{description}
% \item[\opt{expandtopt=\meta{boolean}}:]
%   If \texttt{true} all lengths are expanded at setting time to unit pt
%   instead of used unchanged. With this, the lengths are not depending on the
%   font size of usage time but on the current font size.
% \end{description}
%    \begin{macrocode}
    \FamilyBoolKey[.dsc]{KOMAarg}{expandtopt}{scr@dsc@expandtopt}%
    \scr@dsc@expandtoptfalse
%    \end{macrocode}
% \begin{description}
% \item[\opt{increaselevel=\meta{number}}:]
%   Automatically increase \opt{level} by \meta{number} (with default 1). This
%   is needed for \cs{DeclareSectionCommands} and therefore only defined if
%   \cs{scr@local@levelincrease} is defined.
% \end{description}
%    \begin{macrocode}
    \scr@ifundefinedorrelax{scr@local@levelincrease}{%
      \RelaxFamilyKey[.dsc]{KOMAarg}{increaselevel}%
    }{%
      \FamilyCounterMacroKey[.dsc]{KOMAarg}{increaselevel}[1]%
                            {\scr@local@levelincrease}%
    }%
%    \end{macrocode}
% \begin{description}
% \item[\opt{level=\meta{number}}:]
%   The numerical outline level of the command.
% \end{description}
%    \begin{macrocode}
    \scr@ifundefinedorrelax{scr@local@leveloffset}{%
      \edef\reserved@a{%
        \noexpand\FamilyCounterMacroKey[.dsc]{KOMAarg}{level}{%
          \expandafter\noexpand\csname \scr@dsc@current numdepth\endcsname}%
      }\reserved@a
    }{%
      \edef\reserved@a{%
        \noexpand\DefineFamilyKey[.dsc]{KOMAarg}{level}{%
          \noexpand\FamilySetCounterMacro{KOMAarg}{level}{%
            \expandafter\noexpand\csname \scr@dsc@current numdepth\endcsname
          }%
          \unexpanded{%
            {\numexpr ##1+\scr@local@leveloffset\relax}%
            \edef\scr@local@leveloffset{%
              \the\numexpr\scr@local@leveloffset+\scr@local@levelincrease\relax
            }%
          }%
        }%
      }\reserved@a
    }%
%    \end{macrocode}
% \begin{description}
% \item[\opt{counterwithout=\meta{name}}:]
%   \changes{v3.19}{2015/09/09}{option \opt{counterwithout}}
%   The counter of this command should not depend on the counter
%   \cnt{name} any longer.
% \end{description}
%    \begin{macrocode}
    \FamilyStringKey[.dsc]{KOMAarg}{counterwithout}{\scr@local@counterwithout}%
    \let\scr@local@counterwithout\relax
%    \end{macrocode}
% \begin{description}
% \item[\opt{counterwithin=\meta{name}}:]
%   The counter of this command depends on the counter \cnt{name}.
% \end{description}
%    \begin{macrocode}
    \FamilyStringKey[.dsc]{KOMAarg}{counterwithin}{\scr@local@counterwithin}%
    \let\scr@local@counterwithin\relax
%    \end{macrocode}
% \begin{description}
% \item[\opt{toclevel=\meta{number}}:]
%   The numerical ToC entry level. Default is \opt{level}. This option exists
%   only, if the ToC style supports it. But because with
%   \cs{DeclareSectionCommands} we need an offset for this (similar to the
%   increase of \opt{level}). So we have to redefine it in case of existing
%   \cs{scr@local@tocleveloffset}.
% \end{description}
%    \begin{macrocode}
    \scr@ifundefinedorrelax{scr@local@tocleveloffset}{}{%
      \scr@ifundefinedorrelax{KV@KOMAarg.dsc@toclevel}{%
      }{%
        \DefineFamilyKey[.dsc]{KOMAarg}{toclevel}{%
          \edef\reserved@a{%
            \noexpand\l@addto@macro{\noexpand\scr@dsc@tocstyle@options}{%
              level=\the\numexpr ##1+\scr@local@tocleveloffset\relax,%
            }%
          }\reserved@a
          \edef\scr@local@tocleveloffset{%
            \the\numexpr\scr@local@tocleveloffset+\scr@local@levelincrease\relax
          }%
          \FamilyKeyStateProcessed
        }%
      }%
    }%
%    \end{macrocode}
% \changes{v3.20}{2015/11/12}{only temporary definition of options}
% The main pass of options execution. Afterwards reset all the options.
%    \begin{macrocode}
    \FamilyExecuteOptions[.dsc]{KOMAarg}{#1}%
    \scr@dsc@extra@relax@opts
    \let\scr@dsc@extra@relax@opts\relax
    \RelaxSectionCommandOptions
    \RelaxFamilyKey[.dsc]{KOMAarg}{style}%
    \RelaxFamilyKey[.dsc]{KOMAarg}{tocstyle}%
    \RelaxFamilyKey[.dsc]{KOMAarg}{expandtopt}%
    \scr@ifundefinedorrelax{scr@local@levelincrease}{}{%
      \RelaxFamilyKey[.dsc]{KOMAarg}{increaselevel}%
    }%
    \RelaxFamilyKey[.dsc]{KOMAarg}{level}%
    \RelaxFamilyKey[.dsc]{KOMAarg}{counterwithin}%
%    \end{macrocode}
% Now, we can test, if all needed options has been defined.
% \changes{v3.27}{2019/02/07}{\opt{level} must no be -\cs{maxdimen}}
% Also test, that \opt{level} is not -\len{maxdimen}, because this value is
% used internally to switch of numbering locally.
%    \begin{macrocode}
    \begingroup
      \def\local@endgroup{\endgroup}%
      \scr@ifundefinedorrelax{\scr@dsc@current numdepth}{%
        \scr@declaresectioncommanderror{\scr@dsc@current}{section level}{level}%
      }{%
        \expandafter\ifnum \csname #2numdepth\endcsname>-\maxdimen
          \@nameuse{scr@dsc@style@%
            \@nameuse{scr@\scr@dsc@current @style}%
            @neededoptionstest}%
        \else
          \ClassError{\KOMAClassName}{%
            `level' must be > -\the\numexpr\maxdimen\relax
          }{%
            KOMA-Script internally sets `secnumdepth' to
            -\the\numexpr\maxdimen\relax\space
            to locally switch of\MessageBreak
            the numbering, i.e., inside running heads.\MessageBreak
            Because of this, you can define section commands with\MessageBreak
            `level' greater than -\the\numexpr\maxdimen\relax\space only.%
          }%
          \let\local@endgroup\endgroup
          \aftergroup\@gobbletwo
        \fi
      }%
    \local@endgroup
%    \end{macrocode}
% If the configuration is compete, we are ready for the (re-)definition of the
% command.
%    \begin{macrocode}
    \@firstofone{%
%    \end{macrocode}
% \changes{v3.20}{2015/10/15}{missing \cs{expandafter} added}
% First of all we need a counter:
%    \begin{macrocode}
      \@ifundefined{c@#2}{\newcounter{#2}}{}%
      \ifx\scr@local@counterwithout\relax
      \else\ifx\scr@local@counterwithout\@empty
        \else
          \@removefromreset{#2}{\scr@local@counterwithout}%
        \fi
        \@namedef{the#2}{\arabic{#2}}%
      \fi
      \ifx\scr@local@counterwithin\relax
      \else\ifx\scr@local@counterwithin\@empty
          \@namedef{the#2}{\arabic{#2}}%
        \else
          \@removefromreset{#2}{\scr@local@counterwithin}%
          \@addtoreset{#2}{\scr@local@counterwithin}%
          \expandafter\def\csname the#2\expandafter\endcsname\expandafter{%
            \csname the\scr@local@counterwithin\endcsname.\arabic{#2}}%
        \fi
      \fi
      \@ifundefined{#2format}{%
        \@namedef{#2format}{\csname the#2\endcsname\autodot\enskip}%
      }{}%
%    \end{macrocode}
% Then the command itself (but only, if the \opt{style} was not empty):
%    \begin{macrocode}
      \ifscr@dsc@parametersonly
        \ClassInfo{\KOMAClassName}{%
          not defining `\expandafter\string\csname #2\endcsname' due
          to\MessageBreak
          empty section style option%
        }%
      \else
        \scr@ifundefinedorrelax{%
          scr@dsc@def@style@\@nameuse{scr@#2@style}@command%
        }{%
          \ClassWarning{\KOMAClassName}{%
            not defining `\expandafter\string\csname #2\endcsname'
            due\MessageBreak
            to not yet defined section style\MessageBreak
            `\@nameuse{scr@#2@style}'%
          }%
        }{%
          \@nameuse{scr@dsc@def@style@\@nameuse{scr@#2@style}@command}{#2}%
        }%
      \fi
%    \end{macrocode}
% We also need commands for the running head and a corresponding number
% formatting. With \pkg*{scrlayer} this can and should be done using
% \cs{DeclareSectionNumberDepth}. Otherwise we do it step by step.
% \changes{v3.15a}{2015/01/21}{\cs{endskip} instead of \cs{enskip}}
%    \begin{macrocode}
      \@ifundefined{DeclareSectionNumberDepth}{%
        \@ifundefined{#2markformat}{%
          \@namedef{#2markformat}{\csname the#2\endcsname\autodot\enskip}%
        }{}%
        \@ifundefined{#2mark}{%
          \expandafter\let\csname #2mark\endcsname\@gobble
        }{}%
      }{%
        \DeclareSectionNumberDepth{#2}{\csname #2numdepth\endcsname}%
      }%
%    \end{macrocode}
% We also need macros to generate and print the ToC entry.
%    \begin{macrocode}
      \expandafter\providecommand\expandafter*%
      \csname add#2tocentry\endcsname[2]{%
        \addtocentrydefault{#2}{##1}{##2}%
      }%
      \scr@ifundefinedorrelax{scr@tso@#2@style}{%
        \expandafter\let\csname scr@tso@#2@style\expandafter\endcsname
        \csname scr@dte@#2@style\endcsname
      }{}%
      \scr@ifundefinedorrelax{scr@tso@#2@style}{%
      }{%
        \DeclareTOCStyleEntry[\scr@dsc@tocstyle@options]{%
          \@nameuse{scr@tso@#2@style}%
        }{#2}%
      }%
      \scr@ifundefinedorrelax{l@#2}{%
        \DeclareTOCStyleEntry[\scr@dsc@tocstyle@options]{default}{#2}%
      }{}%
%    \end{macrocode}
% \changes{v3.20}{2015/0/07}{support for ToC levels with \pkg{hyperref}}
% \changes{v3.26}{2018/06/27}{overwrite ToC levels of \pkg{hyperref}}
% \changes{v3.26}{2018/06/27}{fallback handling of gaps in the bookmark
%   levels}
% And we need extra code for \pkg{hyperref} and the bookmarks.
%    \begin{macrocode}
      \if@atdocument
        \let\reserved@a\@firstofone
      \else
        \@ifpackageloaded{hyperref}{%
          \let\reserved@a\@firstofone
        }{%
          \def\reserved@a##1{%
            \AfterAtEndOfPackage{hyperref}{\AtBeginDocument{##1}}%
          }%
        }%
      \fi
      \reserved@a{%
        \expandafter\let\csname toclevel@#2\expandafter\endcsname
        \csname #2tocdepth\endcsname
        \scr@ifundefinedorrelax{bookmarksetup}{%
          \scr@ifundefinedorrelax{scr@min@toclevel}{%
            \expandafter\let\expandafter\scr@min@toclevel
            \csname toclevel@#2\endcsname
          }{%
            \expandafter\ifnum \csname toclevel@#2\endcsname
            < \numexpr \scr@min@toclevel-1\relax
              \ClassWarning{\KOMAClassName}{%
                Trying emergency fix for bookmark level gap,\MessageBreak
                because toclevel of `#2'
                (\csname toclevel@#2\endcsname)\MessageBreak
                is more than 1 lower than currently lowest\MessageBreak
                known level (\scr@min@toclevel).\MessageBreak
                Note: This fix can fail and you should load\MessageBreak
                package `bookmark' to avoid usage of this\MessageBreak
                fix%
              }%
              \expandafter\edef\csname toclevel@#2\endcsname{%
                \the\numexpr \scr@min@toclevel-1\relax
              }%
            \fi
            \expandafter\ifnum \csname toclevel@#2\endcsname
            < \scr@min@toclevel\relax
              \expandafter\let\expandafter\scr@min@toclevel
              \csname toclevel@#2\endcsname
            \fi
          }%
          \scr@ifundefinedorrelax{scr@max@toclevel}{%
            \expandafter\let\expandafter\scr@max@toclevel
            \csname toclevel@#2\endcsname
          }{%
            \expandafter\ifnum \csname toclevel@#2\endcsname
            > \numexpr \scr@max@toclevel+1\relax
              \ClassWarning{\KOMAClassName}{%
                Trying emergency fix for bookmark level gap,\MessageBreak
                because toclevel of `#2'
                (\csname toclevel@#2\endcsname)\MessageBreak
                is more than 1 greater than currently highest\MessageBreak
                known level (\scr@max@toclevel).\MessageBreak
                Note: This fix can fail and you should load\MessageBreak
                package `bookmark' to avoid usage of this\MessageBreak
                fix%
              }%
              \expandafter\edef\csname toclevel@#2\endcsname{%
                \the\numexpr \scr@max@toclevel+1\relax
              }%
            \fi
            \expandafter\ifnum \csname toclevel@#2\endcsname
            > \scr@max@toclevel\relax
              \expandafter\let\expandafter\scr@max@toclevel
              \csname toclevel@#2\endcsname
            \fi
          }%
        }{}%
      }%
    }%
  }%
  \let\scr@dsc@current\relax
}
%    \end{macrocode}
% \begin{macro}{\scr@declaresectioncommanderror}
% \changes{v3.15}{2014/11/21}{added}
% It there were not enough options to \cs{DeclareSectionCommand} to define the
% command, we report an error and also execute
% \cs{aftergroup}\cs{@gobbletwo}. So the |\@firstofone{…}| code after
% |\endgroup| resp. |\local@endgroup| inside \cs{DeclareSectionCommand} that
% defines the new section command etc. will never be used in such a case.
%    \begin{macrocode}
\newcommand*{\scr@declaresectioncommanderror}[3]{%
  \ClassError{\KOMAClassName}{%
    #2 of \expandafter\string\csname #1\endcsname\space unknown%
  }{%
    Please use option `#3' to declare the #2.\MessageBreak
    If you'll continue, declaration will be ignored%
  }%
  \let\local@endgroup\endgroup
  \aftergroup\@gobbletwo
}
%    \end{macrocode}
% \end{macro}^^A \scr@declaresectioncommanderror
% \begin{macro}{\scr@declaresectioncommandwarning}
% \changes{v3.24}{2017/04/25}{added}
% Sometimes a warning and extension of \cs{local@endgroup} is enough.
%    \begin{macrocode}
\newcommand*{\scr@declaresectioncommandwarning}[5]{%
  \ClassInfo{\KOMAClassName}{%
    #2 of \expandafter\string\csname #1\endcsname\space unknown.\MessageBreak
    You should use option `#3' if you\MessageBreak
    do not want to use the default value\MessageBreak
    `\detokenize{#4}'%
  }%
  \l@addto@macro\local@endgroup{#5}%
}
%    \end{macrocode}
% \end{macro}^^A \scr@declaresectioncommandwarning
% \end{command}^^A \DeclareSectionCommand
%
%
% \begin{command}{\DeclareNewSectionCommand}
% \changes{v3.15}{2014/11/21}{added}
% \changes{v3.25}{2018/03/10}{additional tests and errors about
%   \cs{the\dots}, \cs{\dots mark}, \cs{\dots format},
%   \cs{\dots markformat}}
% \changes{v3.30}{2020/04/08}{orphan spaces removed}
% This is similar to \cs{DeclareSectionCommand} but reports an error, if the
% command or a main helper already exist. So it can be used for new commands
% only.
%    \begin{macrocode}
\newcommand*{\DeclareNewSectionCommand}[2][]{%
  \@ifundefined{#2}{%
    \@tempswatrue
    \let\reserved@b\@empty
    \@for \reserved@a:=the#2,#2mark,#2format,#2markformat\do{%
      \expandafter\scr@ifundefinedorrelax\expandafter{\reserved@a}{}{%
        \@tempswafalse
        \edef\reserved@b{\expandafter\string\csname \reserved@a\endcsname}%
      }%
    }%
    \if@tempswa
%    \end{macrocode}
% \changes{v3.30}{2020/04/08}{extended test for really new}
% After the general user commands, we just have to detect the style \dots
%    \begin{macrocode}
      \expandafter\let\expandafter\scr@dsc@current
      \csname scr@tso@##2@style\endcsname
      \DefineFamilyKey[.dsc]{KOMAarg}{style}{%
        \edef\scr@dsc@current{##1}%
        \FamilyKeyStateProcessed
      }%
      \DefineFamilyKey[.dsc]{KOMAarg}{@else@}{\FamilyKeyStateProcessed}%
      \FamilyExecuteOptions[.dsc]{KOMAarg}{#1}%
      \RelaxFamilyKey[.dsc]{KOMAarg}{@else@}%
      \RelaxFamilyKey[.dsc]{KOMAarg}{style}%
      \@tempswatrue
%    \end{macrocode}
% \dots{} to gut through the \cs{scr@dsc@style@\meta{style}@subcommands} and
% test, if one of them exists.
%    \begin{macrocode}
      \@ifundefined{scr@dsc@current}{}{%
        \scr@ifundefinedorrelax{scr@dsc@style@\scr@dsc@current @subcommands}{%
        }{%
          \edef\reserved@b{%
            \csname scr@dsc@style@\scr@dsc@current @subcommands\endcsname{#2}%
          }%
          \expandafter\@for\expandafter\reserved@a\expandafter:\expandafter=%
          \reserved@b\do{%
            \expandafter\scr@ifundefinedorrelax\expandafter{\reserved@a}{}{%
              \@tempswafalse
              \edef\reserved@b{%
                \expandafter\string\csname \reserved@a\endcsname}%
            }%
          }%
        }
      }%
      \let\scr@dsc@current\relax
    \fi
    \if@tempswa
%    \end{macrocode}
% If not, we can use \cs{DeclareSectionCommand}. Otherwise just report an
% error.
%    \begin{macrocode}
      \DeclareSectionCommand[{#1}]{#2}%
    \else
      \ClassError{\KOMAClassName}{%
        command `\reserved@b' already defined%
      }{%
        You've tried to define the section command `\expandafter\string\csname
        #2\endcsname' newly.\MessageBreak
        Such a section command needs an additional new command
        `\reserved@b',\MessageBreak
        but this already exists. So you cannot define
        `\expandafter\string\csname #2\endcsname' newly.\MessageBreak
        Maybe you should use one of \string\DeclareSectionCommand,
        \string\RedeclareSectionCommand,\MessageBreak
        or \string\ProvideSectionCommand.\MessageBreak
        If you'll continue, the command will be ignored.%
      }%
    \fi
  }{%
    \ClassError{\KOMAClassName}{%
      command `\expandafter\string\csname #2\endcsname' already defined%
    }{%
      You've tried to define the section command `\expandafter\string\csname
      #2\endcsname' newly,\MessageBreak
      but a command or something else with this name already
      exists.\MessageBreak
      Maybe you should use one of \string\DeclareSectionCommand,
      \string\RedeclareSectionCommand,\MessageBreak
      or \string\ProvideSectionCommand.\MessageBreak
      If you'll continue, the command will be ignored.%
    }%
  }%
}
%    \end{macrocode}
% \end{command}^^A \DeclareNewSectionCommand
%
% \begin{command}{\RedeclareSectionCommand}
% \changes{v3.15}{2014/11/21}{added}
% This is much easier than \cs{DeclareNewSectionCommand} because it tests only
% for the main command and uses \cs{DeclareSectionCommand} if the main command
% already exists. So all the additional commands and main helpers do not need
% to exist.
%    \begin{macrocode}
\newcommand*{\RedeclareSectionCommand}[2][]{%
  \@ifundefined{#2}{%
    \ClassError{\KOMAClassName}{%
      command `\expandafter\string\csname #2\endcsname' not defined%
    }{%
      You've tried to re-define the section command `\expandafter\string\csname
      #2\endcsname',\MessageBreak
      but a command with this name does not exists.\MessageBreak
      Maybe you should use one of \string\DeclareSectionCommand,
      \string\DeclareNewSectionCommand,\MessageBreak
      or \string\ProvideSectionCommand.\MessageBreak
      If you'll continue, the command will be irgnored.%
    }%
  }{%
    \DeclareSectionCommand[{#1}]{#2}%
  }%
}
%    \end{macrocode}
% \end{command}^^A \RedeclareSectionCommand
%
% \begin{command}{\ProvideSectionCommand}
% \changes{v3.15}{2014/11/21}{added}
% This is much easier than \cs{DeclareNewSectionCommand} because currently it
% tests only for the command itself but nor for the helpers.
% \begin{description}
% \item[ToDo:] This is dangerous, because
%   e.g. |\ProvideSectionCommand{schapter}| would break \cs{chapter} (because
%   of \cs{@schapter}). So maybe it should do the same tests as
%   \cs{DeclareNewSectionCommand} and at least warn.
% \end{description}
% Basierend auf |\DeclareSectionCommand| wird eine Gliederungsanweisung
% definiert, wenn sie zuvor noch nicht existierte. Wichtig zu bemerken ist,
% dass die ganzen Hilfsanweisungen bereits existieren dürfen.
%    \begin{macrocode}
\newcommand*{\ProvideSectionCommand}[2][]{%
  \@ifundefined{#2}{%
    \DeclareSectionCommand[{#1}]{#2}%
  }{%
    \ClassInfo{\KOMAClassName}{%
      \string\ProvideSectionCommand{#1} ignored%
    }%
  }%
}
%    \end{macrocode}
% \end{command}^^A \ProvideSectionCommand
%
% \begin{command}{\DeclareSectionCommands}
% \changes{v3.15}{2014/12/03}{added}
% \changes{v3.26}{2018/08/29}{using \cs{scr@trim@spaces}}
% Based on \cs{DeclareSectionCommand} this one can be used to define several
% commands at once. The commands are given as a comma-separated list of
% commands. There is one additional option \opt{increaselevel=\meta{number}}
% to add \meta{number} to the \opt{level} at each call of
% \cs{DeclareSectionCommand} but the first one. The definition of this option
% is done inside \cs{DeclareSectionCommand} if \cs{scr@local@levelincrease} is
% defined. Because of the first run, we initialize it with 0. The same has to
% be done for \cs{scr@local@leveloffset} and \cs{scr@local@tocleveloffset}.
% At the end we also have to un-define these local commands. Otherwise later
% usage of \cs{DeclareSectionCommand} would be detected as part of
% \cs{DeclareSectionCommands}.
%    \begin{macrocode}
\newcommand*{\DeclareSectionCommands}[2][]{%
  \edef\reserved@a{#2}%
  \def\scr@local@levelincrease{\z@}%
  \def\scr@local@leveloffset{\z@}%
  \def\scr@local@tocleveloffset{\z@}%
  \@for\reserved@a:=\reserved@a\do{%
    \scr@trim@spaces\reserved@a
    \edef\reserved@a{%
      \unexpanded{\DeclareSectionCommand[{#1}]}{\reserved@a}%
    }%
    \reserved@a
  }%
  \let\scr@local@levelincrease\relax
  \let\scr@local@leveloffset\relax
  \let\scr@local@tocleveloffset\relax
}
%    \end{macrocode}
% \end{command}^^A \DeclareSectionCommands
%
% \begin{command}{\DeclareNewSectionCommands}
% \changes{v3.15}{2014/12/03}{added}
% \changes{v3.26}{2018/08/29}{using \cs{scr@trim@spaces}}
% Because of \opt{increaselevel} we cannot simply use
% \cs{DeclareNewSectionCommand} inside, but have to use
% \cs{DeclareSectionCommand} even for already defined commands (but
% nevertheless report an error).
% \begin{description}
% \item[ToDo:] Is this really the correct error action? Does it really matter,
%   if \opt{increaselevel} would be wrong after already reporting an error?
% \end{description}
%    \begin{macrocode}
\newcommand*{\DeclareNewSectionCommands}[2][]{%
  \edef\reserved@a{#2}%
  \def\scr@local@levelincrease{\z@}%
  \def\scr@local@leveloffset{\z@}%
  \def\scr@local@tocleveloffset{\z@}%
  \@for\reserved@a:=\reserved@a\do{%
    \scr@trim@spaces\reserved@a
    \@ifundefined{\reserved@a}{}{%
      \ClassError{\KOMAClassName}{%
        command `\expandafter\string\csname\reserved@a\endcsname' already
        defined%
      }{%
        You've tried to define the section command 
        `\expandafter\string\csname\reserved@a\endcsname' newly,\MessageBreak
        but a command, token, box or length with this name already
        exists.\MessageBreak
        Maybe you should use one of \string\DeclareSectionCommand,
        \string\RedeclareSectionCommand,\MessageBreak
        or \string\ProvideSectionCommand.\MessageBreak
        Nevertheless, if you'll continue, the command will be defined.%
      }%
    }%
    \edef\reserved@a{%
      \unexpanded{\DeclareSectionCommand[{#1}]}{\reserved@a}%
    }%
    \reserved@a
  }%
  \let\scr@local@levelincrease\relax
  \let\scr@local@leveloffset\relax
  \let\scr@local@tocleveloffset\relax
}
%    \end{macrocode}
% \end{command}^^A \DeclareNewSectionCommands
%
% \begin{command}{\RedeclareSectionCommands}
% \changes{v3.15}{2014/12/03}{added}
% \changes{v3.26}{2018/08/29}{using \cs{scr@trim@spaces}}
% Because of \opt{increaselevel} we cannot simply use
% \cs{RedeclareSectionCommand} inside, but have to use
% \cs{DeclareSectionCommand} even for not yet defined commands (but
% nevertheless report an error).
% \begin{description}
% \item[ToDo:] Is this really the correct error action? Does it really matter,
%   if \opt{increaselevel} would be wrong after already reporting an error?
% \end{description}
%    \begin{macrocode}
\newcommand*{\RedeclareSectionCommands}[2][]{%
  \edef\reserved@a{#2}%
  \def\scr@local@levelincrease{\z@}%
  \def\scr@local@leveloffset{\z@}%
  \def\scr@local@tocleveloffset{\z@}%
  \@for\reserved@a:=\reserved@a\do{%
    \scr@trim@spaces\reserved@a
    \@ifundefined{\reserved@a}{%
      \ClassError{\KOMAClassName}{%
        command `\expandafter\string\csname\reserved@a\endcsname' not defined%
      }{%
        You've tried to re-define the section command 
        `\expandafter\string\csname\reserved@a\endcsname',\MessageBreak
        but a command with this name does not exists.\MessageBreak
        Maybe you should use one of \string\DeclareSectionCommand,
        \string\DeclareNewSectionCommand,\MessageBreak
        or \string\ProvideSectionCommand.\MessageBreak
        Nevertheless, if you'll continue, the command will be defined.%
      }%
    }{}%
    \edef\reserved@a{%
      \unexpanded{\DeclareSectionCommand[{#1}]}{\reserved@a}%
    }%
    \reserved@a
  }%
  \let\scr@local@levelincrease\relax
  \let\scr@local@leveloffset\relax
  \let\scr@local@tocleveloffset\relax
}
%    \end{macrocode}
% \end{command}^^A \RedeclareSectionCommands
%
% \begin{command}{\ProvideSectionCommands}
% \changes{v3.15}{2014/12/03}{added}
% \changes{v3.17}{2015/03/23}{re-implemented because of style depending
%   options}
% \changes{v3.26}{2018/08/29}{using \cs{scr@trim@spaces}}
% This is the real problematic case, because \opt{increaselevel} should be
% used even for commands, that must not be defined. However, maybe a solution
% like this should be used for the two previous commands too.
%    \begin{macrocode}
\newcommand*{\ProvideSectionCommands}[2][]{%
  \edef\reserved@a{#2}%
  \def\scr@local@levelincrease{\z@}%
  \def\scr@local@leveloffset{\z@}%
  \def\scr@local@tocleveloffset{\z@}%
  \@for\reserved@a:=\reserved@a\do{%
    \scr@trim@spaces\reserved@a
    \@ifundefined{\reserved@a}{%
      \edef\reserved@a{%
        \unexpanded{\DeclareSectionCommand[{#1}]}{\reserved@a}%
      }%
      \reserved@a
    }{%
%    \end{macrocode}
% If the command already exists, process options \opt{level}, \opt{toclevel}
% and \opt{increaselevel} and ignore all others using an \opt{@else@} option.
%    \begin{macrocode}
      \DefineFamilyKey[.dsc]{KOMAarg}{@else@}{\FamilyKeyStateProcessed}%
      \FamilyCounterMacroKey[.dsc]{KOMAarg}{increaselevel}[1]%
        {\scr@local@levelincrease}%
      \DefineFamilyKey[.dsc]{KOMAarg}{level}{%
        \FamilySetCounterMacro{KOMAarg}{level}{\reserved@b}{##1}%
        \edef\scr@local@leveloffset{%
          \the\numexpr\scr@local@leveloffset+\scr@local@levelincrease\relax
        }%
      }%
      \DefineFamilyKey[.dsc]{KOMAarg}{toclevel}{%
        \FamilySetCounterMacro{KOMAarg}{toclevel}{\reserved@b}{##1}%
        \edef\scr@local@tocleveloffset{%
          \the\numexpr\scr@local@tocleveloffset+\scr@local@levelincrease\relax
        }%
      }%
      \FamilyExecuteOptions[.dsc]{KOMAarg}{#1}%
      \RelaxFamilyKey[.dsc]{KOMAarg}{toclevel}%
      \RelaxFamilyKey[.dsc]{KOMAarg}{level}%
      \RelaxFamilyKey[.dsc]{KOMAarg}{increaselevel}%
      \RelaxFamilyKey[.dsc]{KOMAarg}{@else@}%
    }%
  }%
  \let\scr@local@levelincrease\relax
  \let\scr@local@leveloffset\relax
  \let\scr@local@tocleveloffset\relax
}
%</body>
%    \end{macrocode}
% \end{command}^^A \ProvideSectionCommands
%
% \begin{command}{\IfSectionCommandStyleIs}
% \changes{v3.27}{2019/02/04}{added}
% Test whether ot not the section command of argument 1 has the style of
% argument 2. If so: use argument 3, of not: use argument 4.
%    \begin{macrocode}
%<*body>
\newcommand*{\IfSectionCommandStyleIs}[2]{%
  \scr@ifundefinedorrelax{#1}{%
    \ClassError{\KOMAClassName}{%
      \expandafter\string\csname #1\endcsname\space not defined}{%
      You cannot compare the section command style of an undefined command.}%
  }{%
    \scr@ifundefinedorrelax{scr@#1@style}{%
      \ClassError{\KOMAClassName}{%
        \expandafter\string\csname #1\endcsname\space not a valid section
        command%
      }{%
        You cannot detect the section command style of a command, that
        has\MessageBreak
        never been defined as a section command by KOMA-Script.%
      }%
    }%
  }%
  \scr@ifundefinedorrelax{scr@dsc@def@style@#2@command}{%
    \ClassError{\KOMAClassName}{%
      unknown section command style `#2'%
    }{}%
  }%
  \Ifstr{\@nameuse{scr@#1@style}}{#2}%
}
%</body>
%    \end{macrocode}
% \end{command}
%
% \begin{option}{bookmarkpackage}
% \changes{v3.26}{2018/06/27}{added}
% \changes{v3.39}{2022/11/11}{initial dot removed from member argument of
%   option storage commands}
% Package \pkg{hyperref} has problems with bookmark level gaps. So if
% either the users omits a heading level or defines a heading with a level
% that distinguish more than one from the previous or following level, there
% would be a problem. One recommended solution for this would be to use
% package \pkg{bookmark}. So if we load this package, we can use the same
% value for bookmark levels and TOC levels even within
% \cls*{scrartcl}. Nevertheless we allow the user to decide not to use
% \pkg{bookmark}, but then it is not my fault, if something strange
% happens.
%    \begin{macrocode}
%<*option>
\KOMA@ifkey{bookmarkpackage}{@scr@autoloadbookmarkpackage}
\@scr@autoloadbookmarkpackagetrue
\KOMA@kav@add{\KOMAClassFileName}{bookmarkpackage}{true}
\AfterAtEndOfPackage{hyperref}{%
  \AtBeginDocument{%
    \@ifpackageloaded{bookmark}{}{%
      \if@scr@autoloadbookmarkpackage
%    \end{macrocode}
% \changes{v3.42}{2024/06/04}{load \pkg{bookmark} only if available}
% Now we test for existing package file \file{bookmark.sty}. If it does not
% exist, we show a warning an deactivate option \opt{bookmarkpackage}.
%    \begin{macrocode}
        \IfFileExists{bookmark.sty}{}{%
          \ClassWarningNoLine{\KOMAClassName}{%
            Using `bookmark' together with `hyperref' is
            recommended,\MessageBreak
            because of handling of possible bookmark level gaps.\MessageBreak
            However, the package seems not to be installed and\MessageBreak
            therefore cannot be loaded automatically.\MessageBreak
            It is recommended to install package `bookmark'!\MessageBreak
            However, you can avoid this warning without\MessageBreak
            installation of package `bookmark', if you'd
            deactivate\MessageBreak
            automatically loading of `bookmark' using KOMA-Script\MessageBreak
            option `bookmarkpackage=false' before
            \string\begin{document}%
          }%
          \@scr@autoloadbookmarkpackagefalse
          \KOMA@kav@replacevalue{\KOMAClassFileName}{bookmarkpackage}{false}%
        }%
      \fi
      \if@scr@autoloadbookmarkpackage
%    \end{macrocode}
% \changes{v3.27}{2019/06/26}{try to translate \pkg{hyperref} driver to
%   \pkg{bookmark} driver option}
% The name of the \pkg{hyperref} driver is in \cs{Hy@driver}. But
% \pkg{bookmark} does not provide the same drivers, so we try to translate
% the curreny known \pkg{hyperref} drivers to \pkg{bookmark}
% drivers. Also we do not load \pkg{bookmark} with a driver option but
% define \pkg{bookmark}'s \cs{BookmarkDriverDefault}.
%    \begin{macrocode}
        \scr@ifundefinedorrelax{Hy@driver}{%
          \ClassWarning{\KOMAClassName}{%
            `hyperref' loaded but `\string\Hy@driver' undefined.\MessageBreak
            This should not happen!\MessageBreak
            Maybe `hyperref' not loaded or unknown version?%
          }%
        }{%
%    \end{macrocode}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} renamed to \cs{Ifstr}}
% Note: \texttt{hpdftex}, \texttt{hluatex}, \texttt{hxetex} and \texttt{hvtex}
% do not need to be translated, because they are autodetected by
% \pkg{bookmark} as well. \texttt{hypertex}, \texttt{hdviwindo},
% \texttt{htex4ht} and \texttt{latex2html} are not known by \pkg{bookmark},
% so we stay with the default (usually \texttt{dvips}).
%    \begin{macrocode}
          \Ifstr{\Hy@driver}{hdvips}{%
            \providecommand*{\BookmarkDriverDefault}{dvips}%
          }{%
            \Ifstr{\Hy@driver}{hdvipdfm}{%
              \providecommand*{\BookmarkDriverDefault}{dvipdfm}%
            }{%
              \Ifstr{\Hy@driver}{hdvipson}{%
                \providecommand*{\BookmarkDriverDefault}{dvipsone}%
              }{%
                \Ifstr{\Hy@driver}{htexture}{%
                  \providecommand*{\BookmarkDriverDefault}{textures}%
                }{%
                }%
              }%
            }%
          }%
        }%
        \ClassInfo{\KOMAClassName}{%
          loading recommended package `bookmark'.\MessageBreak
          Using `bookmark' together with `hyperref' is
          recommended,\MessageBreak
          because of handling of possible bookmark level gaps.\MessageBreak
          You can avoid loading `bookmark' with KOMA-Script
          option\MessageBreak
          `bookmarkpackage=false' before \string\begin{document}
          and\MessageBreak
          you can avoid this message adding:\MessageBreak
          \space\space\string\usepackage
          \scr@ifundefinedorrelax{BookmarkDriverDefault}{}{%
            [\BookmarkDriverDefault]%
          }{bookmark}\MessageBreak
          before \string\begin{document}%
        }%
%    \end{macrocode}
% \changes{v3.33}{2021/02/17}{workaround for catcode changes of
%   \pkg{babel-spanish}}
% For \pkg{babel-spanish} we need to deactivate quoting before loading
% \pkg{bookmark}.
%    \begin{macrocode}
        \@ifundefined{es@quoting}{%
          \RequirePackage{bookmark}%
        }{%
          \ifx\es@quoting\activatequoting
            \ClassInfo{\KOMAClassName}{%
              temporary deactivation of babel-spanish quoting
            }%
            \deactivatequoting
            \RequirePackage{bookmark}%
            \ClassInfo{\KOMAClassName}{%
              reactivation of babel-spanish quoting
            }%
            \activatequoting
          \else
            \RequirePackage{bookmark}%  
          \fi
        }%
%    \end{macrocode}
% \changes{v3.26b}{2019/01/17}{workaround for \cs{@beginmainauxhook} added}
% \changes{v3.38}{2022/10/04}{\pkg{auxhook} not needed with newer
%   \pkg{hyperref}}
% \changes{v3.99}{2024/02/10}{no workaround for old \pkg{hyperref}}
% Sometimes \pkg{auxhook} is not able to recognize that it cannot use a
% patch of \cs{document} to execute \cs{@beginmainauxhook} if it is loaded via
% \cs{AtBeginDocument}. In this case we do it here.
%    \begin{macrocode}
%<*!v4>
        \@ifpackagelater{hyperref}{2022/09/22}{}{%
          \ifx\@beginmainauxhook\relax
          \else
            \if@filesw
              \ClassWarning{\KOMAClassName}{%
                seems someone has broken package `auxhook'.\MessageBreak
                Usually this happens, if `auxhook' is loaded or
                used\MessageBreak
                implicitly or explicitly by patching
                \string\document%
                \scr@ifundefinedorrelax{AtEndPreamble}{}{%
                  \MessageBreak
                  or via etoolbox command \string\AtEndPreamble%
                }.\MessageBreak
                Trying an emergency workaround.\MessageBreak
                You can avoid this warning adding:\MessageBreak
                \space\space\string\usepackage{auxhook}\MessageBreak
                before \string\begin{document}%
              }%
              \@beginmainauxhook
            \fi
          \fi
        }%  
%</!v4>
      \fi  
    }%
  }%
}
%</option>
%    \end{macrocode}
% \end{option}^^A bookmarkpackage
%
%
% \subsection{Preambles of parts or chapters}
%
% The \KOMAScript{} classes \cls*{scrbook} and \cls*{scrreprt} provide
% preambles above and below the headings of parts and chapters. The preamble
% above is printed into the usual distance above the headings. It does not
% increase this distance to avoid \emph{jumping headings}. However here we
% have only the generic code for preambles. The user commands are defined by
% \cs{scr@dsc@style@chapter@command} and \cs{scr@dsc@style@part@command}.
%
% \begin{macro}{\use@preamble}
% \changes{v2.8p}{2001/09/25}{added}
% Used as a general macro to print the stored preamble code and clean the
% storage. Preambles are only printed, if the storage is defined. But an empty
% defined storage would be printed. The argument is the name of the storage,
% e.g., \texttt{chapter@o}, \texttt{chapter@u}, \texttt{part@o},
% \texttt{part@u}.
%    \begin{macrocode}
%<*book|report>
%<*body>
\newcommand*{\use@preamble}[1]{%
  \@ifundefined{#1@preamble}{}{%
    \@nameuse{#1@preamble}%
    \global\expandafter\let\csname#1@preamble\endcsname=\relax
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\set@preamble}
% \changes{v2.8p}{2001/09/25}{added}
% \changes{v3.20}{2016/04/12}{\cs{@ifnextchar} replaced by
%   \cs{kernel@ifnextchar}}
% Macro
% \begin{quote}
%   \cs{set@preamble}\marg{level name}\oarg{position}\oarg{width}\marg{text}
% \end{quote}
% defines a preamble code stored into \cs{\meta{preamble name}@preamble}. The
% \meta{preamble name} is build depending on the \meta{level name} and the
% \meta{position}. The \meta{position} is a combination of vertical and
% horizontal position and used to generate not only the \meta{preamble name}
% but also options for the \cs{parbox} used to encapsulate the \meta{text}
% into a preamble code. The \meta{width} is also used for the \cs{parbox}.
% \begin{description}
% \item[Note:] The definition of a preamble is global and the definition of a
%   new preamble of the same \meta{preamble name} overwrite an existing one.
% \end{description}
% \begin{macro}{\set@@preamble}
% \changes{v2.8p}{2001/09/25}{added}
% \changes{v3.20}{2016/04/12}{\cs{@ifnextchar} replaced by
%   \cs{kernel@ifnextchar}}
% \begin{macro}{\set@@@preamble}
% \changes{v2.8p}{2001/09/25}{added}
% \begin{macro}{\set@@@@preamble}
% \changes{v2.8p}{2001/09/25}{added}
% \changes{v2.8q}{2001/11/27}{missing brace added}
% \changes{v3.12}{2012/10/16}{allow \texttt{o} as alias for \texttt{b}}
% \changes{v3.12a}{2014/02/14}{\texttt{b} fixed}
% We need a lot of helpers because of not using \pkg*{xparse} but having
% several optional arguments. At least one optional argument? If not use an
% empty default for the first one and \len{hsize} for the second one.
%    \begin{macrocode}
\newcommand*{\set@preamble}[1]{%
  \kernel@ifnextchar [%]
  {\set@@preamble{#1}}{\set@@@preamble{#1}[][\hsize]}%
}
%    \end{macrocode}
% Or even two optional argument? If not use default \len{hsize}.
%    \begin{macrocode}
\newcommand*{\set@@preamble}{}
\def\set@@preamble#1[#2]{%
  \kernel@ifnextchar [%]
  {\set@@@preamble{#1}[{#2}]}{\set@@@preamble{#1}[{#2}][\hsize]}%
}
%    \end{macrocode}
% From here we know all optional arguments and have to process the composite
% \meta{position}.
%    \begin{macrocode}
\newcommand{\set@@@preamble}{}
\long\def\set@@@preamble#1[#2][#3]#4{%
  \begingroup
    \def\prmbl@pos{#2}\let\prmbl@hpos\relax\let\prmbl@vpos\relax
    \expandafter \@tfor \expandafter \@tempa
      \expandafter :\expandafter =\prmbl@pos
    \do{%
      \if \@tempa l%
        \set@preamble@hpos{0}{#2}%
      \fi%
      \if \@tempa c%
        \set@preamble@hpos{1}{#2}%
      \fi%
      \if \@tempa r%
        \set@preamble@hpos{2}{#2}%
      \fi%
      \if \@tempa u%
        \set@preamble@vpos{0}{#2}%
      \fi%
      \if \@tempa b%
        \set@preamble@vpos{0}{#2}%
      \fi
      \if \@tempa o%
        \set@preamble@vpos{1}{#2}%
      \fi%
      \if \@tempa t%
        \set@preamble@vpos{1}{#2}%
      \fi
    }%
%    \end{macrocode}
% If the horizontal or vertical position is still unclear use defaults.
%    \begin{macrocode}
    \@ifundefined{prmbl@hpos}{\def\prmbl@hpos{0}}{}%
    \@ifundefined{prmbl@vpos}{\def\prmbl@vpos{0}}{}%
%    \end{macrocode}
% From the vertical position we can build the \meta{preamble name} and the
% vertical position of the \cs{parbox} the horizontal is used for the
% horizontal skip before and after the \cs{parbox}.
%    \begin{macrocode}
    \ifcase\prmbl@hpos
      \ifcase\prmbl@vpos
        \set@@@@preamble{#1@u}{t}{#3}{}{\hfil}{#4}%
      \else
        \set@@@@preamble{#1@o}{b}{#3}{}{\hfil}{#4}%
      \fi
    \or
      \ifcase\prmbl@vpos
        \set@@@@preamble{#1@u}{t}{#3}{\hfil}{\hfil}{#4}%
      \else
        \set@@@@preamble{#1@o}{b}{#3}{\hfil}{\hfil}{#4}%
      \fi
    \else
      \ifcase\prmbl@vpos
        \set@@@@preamble{#1@u}{t}{#3}{\hfil}{}{#4}%
      \else
        \set@@@@preamble{#1@o}{b}{#3}{\hfil}{}{#4}%
      \fi
    \fi
  \endgroup
}
%    \end{macrocode}
% Last but not least add the horizontal aligned \cs{parbox} to the preamble
% storage.
%    \begin{macrocode}
\newcommand{\set@@@@preamble}[6]{%
  \expandafter\gdef\csname #1@preamble\endcsname{%
    \hbox to\hsize{#4\parbox[{#2}]{#3}{#6\par}#5\par}%
  }%
}
%    \end{macrocode}
% \begin{macro}{\set@preamble@hpos}
% \changes{v2.8q}{2001/09/25}{added}
% Either store the horizontal preamble position if not yet defined or if
% already defined but illegal report it as an error.
%    \begin{macrocode}
\newcommand*{\set@preamble@hpos}[2]{%
  \@ifundefined{prbml@hpos}{%
    \def\prmbl@hpos{#1}%
  }{%
    \ifnum \prmbl@hpos=0\relax\else%
      \ClassError{\KOMAClassName}{%
        inconsistent hpos options%
      }{%
        You've said `#2' as position option. But you have
        to\MessageBreak
        use only one of `l', `r', or `c' not two of
        these.\MessageBreak
        If you'll continue only first hpos option will be
        used%
      }%
    \fi
  }%
}
%    \end{macrocode}
% \end{macro}^^A \set@preamble@hpos
% \begin{macro}{\set@preamble@vpos}
% \changes{v2.8q}{2001/09/25}{added}
% Either store the vertical position if not yet defined or if already defined
% but illegal report it as an error.
%    \begin{macrocode}
\newcommand*{\set@preamble@vpos}[2]{%
  \@ifundefined{prbml@vpos}{%
    \def\prmbl@vpos{#1}%
  }{%
    \ifnum \prmbl@vpos=0\relax\else%
      \ClassError{\KOMAClassName}{%
        inconsistent vpos options%
      }{%
        You've said `#2' as position option. But you have
        to\MessageBreak
        use only one of `t', `o', `b', or `u' not two of
        these.\MessageBreak
        If you'll continue only first vpos option will be
        used%
      }%
    \fi
  }%
}
%</body>
%</book|report>
%    \end{macrocode}
% \end{macro}^^A \set@preamble@vpos
% \end{macro}^^A \set@@@@preamble
% \end{macro}^^A \set@@@preamble
% \end{macro}^^A \set@@preamble
% \end{macro}^^A \set@preamble
%
%
% \subsection{Deprecated code for the ToC}
%
% Before using \pkg*{tocbasic} we needed additional code for our extended
% version of \cs{@dottedtocline}. However this has been removed in
% \KOMAScript~v3.36.
%
% \begin{macro}{\bprot@dottedtocline}
% \changes{v2.96a}{2006/11/30}{added}
% \changes{v3.27}{2019/02/25}{deprecated}
% \changes{v3.36}{2022/03/15}{removed}
% \end{macro}
%
%
% \subsection{Rough structuring of books}
%
% Some books have a kind of rough structuring into front matter, main matter
% and back matter. This has consequences for the page numbering and the
% numbering of headings and other elements.
%
% \begin{macro}{\@mainmattertrue,\@mainmatterfalse,\if@mainmatter}
% For the numbering of headings and other elements we only need to know
% whether we are in the main matter or not, because chapters outside the main
% matter do not provide a number. A boolean switch is best for this. By
% default we already start with the main matter.
%    \begin{macrocode}
%<*book>
%<*body>
\newif\if@mainmatter\@mainmattertrue
%    \end{macrocode}
% \end{macro}^^A \@mainmattertrue,\if@mainmatter
%
% \begin{command}{\frontmatter}
% \changes{v2.4e}{1996/07/02}{support for \opt{open\quotechar=any}}
% \changes{v2.5h}{1999/12/29}{support for \opt{twoside}}
% \changes{v3.00}{2008/09/27}{\cs{cleardoublepage} replaced by
%   \cs{cleardoubleoddpage}}
% Switching to the front matter always needs a new odd page, because we change
% the page numbering to lowercase Roman numbers.
%    \begin{macrocode}
\newcommand*\frontmatter{%
  \if@twoside\cleardoubleoddpage\else\clearpage\fi
  \@mainmatterfalse\pagenumbering{roman}%
}
%    \end{macrocode}
% \end{command}^^A \frontmatter
% \begin{command}{\mainmatter}
% \changes{v2.4e}{1996/07/02}{support for \opt{open\quotechar=any}}
% \changes{v2.5h}{1999/12/29}{support for \opt{twoside}}
% \changes{v3.00}{2008/09/27}{\cs{cleardoublepage} replaced by
%   \cs{cleardoubleoddpage}}
% Switching back to main matter always needs a new odd page, because we change
% the page numbering to Arabic numbers.
%    \begin{macrocode}
\newcommand*\mainmatter{%
  \if@twoside\cleardoubleoddpage\else\clearpage\fi
  \@mainmattertrue\pagenumbering{arabic}%
}
%    \end{macrocode}
% \end{command}^^A \mainmatter
% \begin{command}{\backmatter}
% \changes{v3.00}{2008/09/27}{\cs{cleardoublepage} replaced by
%     \cs{cleardoubleoddpage}}
% Only with \opt{open=right} we start a new odd page when starting the back
% matter.
% \begin{description}
% \item[ToDo:] This does not make any sense, because the page numbering is not
%   changed. And it seems to be even wrong for \opt{open=left}! So why does
%   \cls*{book} use this and why shall we do so too?
% \end{description}
%    \begin{macrocode}
\newcommand*\backmatter{%
  \if@openright\cleardoubleoddpage\else\clearpage\fi\@mainmatterfalse
}
%</body>
%</book>
%    \end{macrocode}
% \end{command}
%
%
% \subsection{Appendix}
%
% The classes (except \cls*{scrlttr2}) does provide an appendix switch,
% similar but not identical to the standard classes.
%
% \begin{command}{\appendix}
% Activation of the appendix switch over to numbering of chapters
% resp. sections with uppercase letters.
% \changes{v2.2d}{1995/05/28}{\cls*{scrartcl} doesn't have a chapter
%   counter}
% \changes{v2.3c}{1995/08/06}{alternative section numbering in appendix}
% \changes{v2.8}{2001/06/15}{new \cs{appendixmore}}
% \changes{v2.8e}{2001/07/10}{\cs{@altsecnumformattrue} removed}
% \changes{v2.8o}{2001/09/19}{initial \cs{par} added}
% \changes{v2.95}{2006/07/04}{\cs{newcommand*} replaced by \cs{gdef}}
% \changes{v2.95}{2006/07/04}{simplified usage of \cs{appendixmore}}
% \changes{v3.20}{2016/12/05}{improved warning}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} renamed to \cs{Ifstr}}
% \changes{v3.34}{2021/04/30}{\LaTeX{} hook added}
% \begin{command}{\appendixmore}
% \changes{v2.8}{2001/06/15}{added}
% \cs{appendixmore} is deprecated with \LaTeX{} 2020/10/01 (or newer) but
% still executed as a kind of hook.
%    \begin{macrocode}
%<*body>
\newcommand*\appendix{%
  \Ifstr{\@currenvir}{appendix}{%
    \ClassWarning{\KOMAClassName}{%
      You are using
      `\string\begin{appendix}...\string\end{appendix}'.\MessageBreak
      You should note, that `\string\appendix' is a mostly
      globally\MessageBreak
      working command not an enviroment with only local\MessageBreak
      effects. Therefore `\string\end{appendix}' will neither\MessageBreak
      switch back to normal section numbering nor finish\MessageBreak
      every other effect of `\string\begin{appendix}'.\MessageBreak
      Nevertheless, some effects may end with\MessageBreak
      `\string\end{appendix}' and the document may become\MessageBreak
      inconsistent.\MessageBreak
      Because of this, you should remove `\string\end{appendix}'\MessageBreak
      and replace `\string\begin{appendix}' by command\MessageBreak
      `\string\appendix'%
    }%
  }{}%
  \par
%<*article>
  \setcounter{section}{0}%
  \setcounter{subsection}{0}%
  \gdef\thesection{\@Alph\c@section}%
%</article>
%<*report|book>
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@Alph\c@chapter}%
%</report|book>
}
\IfLTXAtLeastTF{2020/10/01}{%
  \g@addto@macro{\appendix}{\UseOneTimeHook{\KOMAClassName/appendix}}%
}{}
\g@addto@macro{\appendix}{\csname appendixmore\endcsname}
%</body>
%<*prepare>
\IfLTXAtLeastTF{2020/10/01}{\NewHook{\KOMAClassName/appendix}}{}
%</prepare>
%    \end{macrocode}
% \end{command}
% \end{command}
%
%
% \subsection{Parts}
%
% \begin{macro}{\@thepart}
% \changes{v2.8e}{2001/07/10}{removed}
% \end{macro}
%
% The definition of parts depends on the class.
%
% \begin{fontelement}{partnumber,part}
% \changes{v2.8o}{2001/09/14}{pre-added as macro}
% \changes{v2.8p}{2001/09/23}{added}
% \changes{v2.96a}{2006/12/02}{undocumented support for last macro expects an
%   argument}
% \begin{fontelement}{partprefix}
% \changes{v3.18}{2015/06/10}{alias added}
% Definition of the font elements used by \cs{part}. The settings are done by
% option \texttt{headings} later. Everything is prepared before this option is
% used the first time. The elements are defined manually because of
% compatibility with \KOMAScript~v2.8o before font elements has been
% implemented in \KOMAScript~v2.8p.
%    \begin{macrocode}
%<*prepare>
\newcommand*{\size@part}{}
\newcommand*{\scr@fnt@part}{\size@part}
\newcommand*{\size@partnumber}{}
\newcommand*{\scr@fnt@partnumber}{\size@partnumber}
\aliaskomafont{partprefix}{partnumber}
%</prepare>
%    \end{macrocode}
% \end{fontelement}^^A partprefix
% \end{fontelement}^^A partnumber,part
%
%
% \begin{command}{\partheadstartvskip,\partheadendvskip}
% \changes{v2.95a}{2006/07/10}{added}
% \changes{v3.15}{2014/11/24}{\cls*{scrartcl} defines and uses
%   \cs{scr@part@beforeskip} resp. \cs{scr@part@afterskip}}
% \begin{command}{\partheadmidvskip}
% \changes{v2.95a}{2006/07/10}{added}
% \begin{command}{\parheademptypage}
% \changes{v3.02}{2009/01/01}{added}
% These commands are the distance before and after a part heading, between the
% prefix line and the text line of the heading and also the empty retro page
% of a part.
%
% \begin{macro}{\scr@part@sectionbeforeskip,\scr@part@beforeskip}
% \changes{v3.15}{2014/11/24}{added}
% \changes{v3.17}{2015/03/25}{\cs{scr@part@sectionbeforeskip} renamed to
%   \cs{scr@part@beforeskip}}
% \begin{macro}{\scr@part@sectionafterskip,\scr@part@sectionbeforeskip}
% \changes{v3.15}{2014/11/24}{added}
% \changes{v3.17}{2015/03/25}{\cs{scr@part@sectionafterskip} renamed to
%   \cs{scr@part@afterskip}}
% \begin{macro}{\scr@part@sectionindent}
% \changes{v3.15}{2014/11/24}{added}
% \changes{v3.17}{2015/03/24}{removed}
% \end{macro}^^A \scr@part@sectionindent
% \begin{macro}{\scr@part@innerskip}
% \changes{v3.17}{2015/03/24}{added}
% \begin{macro}{\scr@part@style}
% \changes{v3.15}{2014/11/24}{added}
% All the skips and the page style are used by \cs{DeclareSectionCommand}
% resp. the style \texttt{part}. We define it as early as possible. Even the
% style of \cs{part} is already defined here.
% \begin{description}
% \item[ToDo:] Do we need them in the \texttt{prepare} run or can we move then
%   even to the \texttt{body} run?
% \item[ToDo:] Do we really need to define \cs{scr@part@style}?
% \end{description}
%    \begin{macrocode}
%<*prepare>
\newcommand*{\scr@part@beforeskip}{%
%<article>  4ex
%    \end{macrocode}
% \begin{description}
% \item[Note:] The |+\baselineskip| is needed, to stay as compatible as
%   possible with prior definitions of \cs{chapterheadstartvskip}.
% \end{description}
%    \begin{macrocode}
%<report|book>  \z@ \@plus 1fil + \baselineskip
}
%<book|report>\newcommand*{\scr@part@innerskip}{20\p@}
\newcommand*{\scr@part@afterskip}{%
%<article>  3ex
%<report|book>  \z@ \@plus 1fil
}
\newcommand*{\scr@part@style}{part}
%    \end{macrocode}
% \end{macro}^^A \scr@part@style
% \end{macro}^^A \scr@part@afterskip
% \end{macro}^^A \scr@part@innerskip
% \end{macro}^^A \scr@part@beforeskip
%    \begin{macrocode}
\newcommand*{\partheadstartvskip}{%
%<article>  \addvspace{\@tempskipa}%
%<report|book>  \null\vskip-\baselineskip\vskip\@tempskipa
}
\newcommand*{\partheadmidvskip}{%
  \par\nobreak
%<book|report>  \vskip\@tempskipa
}
\newcommand*{\partheadendvskip}{%
%<article>  \vskip\@tempskipa
%<report|book>  \vskip\@tempskipa\newpage
}
%<*report|book>
\newcommand*{\partheademptypage}{%
  \if@twoside\if@openright
      \null%
      \thispagestyle{empty}%
      \newpage
  \fi\fi
}
%</report|book>
%</prepare>
%    \end{macrocode}
% \end{command}^^A \partheademptypage
% \end{command}^^A \partheadmidvskip
% \end{command}^^A \partheadstartvskip,\partheadendvskip
%
%
% \begin{macro}{\scr@startpart}
% \changes{v2.8d}{2001/07/05}{using \cs{partpagestyle}}
% \changes{v2.8p}{2001/09/27}{preamble above heading}
% \changes{v3.13a}{2014/09/11}{using \cs{SecDef}}
% \changes{v3.18}{2015/05/23}{added}
% \changes{v3.19}{2015/08/25}{ignoring empty \cs{partpagestyle}}
% \changes{v3.26}{2018/10/14}{supporting option \opt{afterindent}}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} renamed to \cs{Ifstr}}
% The macro
% \begin{quote}
%   \cs{scr@startpart}\marg{part command name}
% \end{quote}
% is now part of the style \texttt{part}. However, the main parts
% of the code are from the original definition of \cs{part}. It has only one
% argument: The name of the part command, i.e., \texttt{part}. All arguments
% to the part command will be processed later.
%    \begin{macrocode}
%<*body>
\newcommand*{\scr@startpart}[1]{%
  \ExecuteDoHook{heading/preinit/#1}%
%<article>  \par
%<*book|report>
  \if@openright\cleardoublepage\else\clearpage\fi
  \scr@ifundefinedorrelax{#1pagestyle}{}{%
    \Ifstr{#1pagestyle}{}{}{%
      \thispagestyle{\@nameuse{#1pagestyle}}%
    }%
  }%
  \if@twocolumn
    \onecolumn
    \@tempswatrue
  \else
    \@tempswafalse
  \fi
%</book|report>
  \@tempskipa=\glueexpr\@nameuse{scr@#1@beforeskip}\relax
  \@ifundefined{scr@#1@afterindent}{\@afterindentfalse}{%
    \csname scr@#1@afterindent\endcsname
    {\@afterindenttrue}{\@afterindentfalse}{%
      \@afterindenttrue
      \ifdim\@tempskipa<\z@
        \@tempskipa=-\@tempskipa
        \@afterindentfalse
      \fi
    }%
  }%
  \ExecuteDoHook{heading/postinit/#1}%
  \partheadstartvskip
%<*book|report>
  \vbox to\z@{\vss\use@preamble{#1@o}\strut\par}%
  \vskip-\baselineskip\nobreak
%</book|report>
  \expandafter\SecDef\csname @#1\expandafter\endcsname\csname @s#1\endcsname
}
%    \end{macrocode}
%
% \begin{macro}{\scr@@startpart}
% \changes{v2.8q}{2001/11/13}{\cs{@parskipfalse}\cs{@parskip@indent}}
% \changes{v2.95}{2002/08/13}{\cs{centering} and \cs{@parskipfalse}%
%   \cs{@parskip@indent} switched}
% \changes{v2.95}{2004/11/05}{\cs{@parskipfalse} and \cs{@parskip@indent}
%   replaced}
% \changes{v3.18}{2015/05/23}{added}
% \changes{v3.27}{2019/07/08}{support for option \opt{nonumber}}
% The macro
% \begin{quote}
%   \cs{scr@@startpart}\marg{part command name}\oarg{extended optional
%   argument}\marg{part heading}
% \end{quote}
% is now part of the style \texttt{part}. However, the main parts
% of the code are from the original definition of \cs{@part}. Therefore it has
% the mandatory and optional arguments of a non-star variant of, i.e.,
% \cs{part} but an additional first argument.
% \begin{description}
% \item[Note:] \meta{extended optional argument} is named as optional, however
%   here it is already mandatory, because it is a delimited argument.
% \end{description}
%    \begin{macrocode}
\newcommand{\scr@@startpart}{}
\long\def\scr@@startpart#1[#2]#3{%
  \ExecuteDoHook{heading/branch/nostar/#1}%
%    \end{macrocode}
% \changes{v3.10}{2011/08/30}{extended optional argument}
% First of all, depending in \opt{headings=optionto\dots} we have different
% defaults for the options.
%    \begin{macrocode}
  \ifnum \scr@osectarg=\z@
    \@scr@tempswafalse
  \else
    \scr@istest#2=\@nil
  \fi
  \@currentusenumbertrue  
  \if@scr@tempswa
    \setkeys{KOMAarg.section}{tocentry={#3},head={#3},reference={#3},#2}%
  \else
    \ifcase \scr@osectarg\relax
      \setkeys{KOMAarg.section}{tocentry={#2},head={#2},reference={#2}}%
    \or
      \setkeys{KOMAarg.section}{tocentry={#3},head={#2},reference={#3}}%
    \or
      \setkeys{KOMAarg.section}{tocentry={#2},head={#3},reference={#2}}%
    \or
      \setkeys{KOMAarg.section}{tocentry={#2},head={#2},reference={#2}}%
    \fi
  \fi
%    \end{macrocode}
% \changes{v2.8e}{2001/07/10}{using \cs{@maybeautodot}}
% \changes{v3.28}{2019/11/19}{\cs{ifnumbered} renamed to
%   \cs{Ifnumbered}}
% \changes{v3.31}{2020/06/02}{\cs{@maybeautodot}\cs{thepart} removed}
% For numbered headings we have to increase the counter and write a process
% information to the \file{log} and the terminal.
%    \begin{macrocode}
  \Ifnumbered{#1}{%
    \refstepcounter{#1}%
    \expandafter\@maybeautodot\csname the#1\endcsname
    \typeout{#1 \csname the#1\endcsname.}%
%    \end{macrocode}
% \changes{v3.08}{2010/11/01}{using \cs{addparttocentry}}
% \changes{v3.18}{2015/05/20}{using \cs{add\meta{Name}tocentry} for
%   \cs{typeout}}
% \changes{v3.31}{2020/06/06}{using \cs{\dots tocformat}}
% And if the ToC entry is not empty add a numbered entry to the table of
% contents.
% \begin{description}
% \item[ToDo:] Move \cs{autodot} to the definition of \cs{\dots tocformat}.
% \end{description}
%    \begin{macrocode}
    \ifx\@currenttocentry\@empty\else
      \@ifundefined{#1tocformat}{%
        \expandafter\gdef\csname #1tocformat\endcsname
        {\csname the#1\endcsname}%
      }{}%
      \scr@ifundefinedorrelax{add#1tocentry}{%
        \addtocentrydefault{#1}%
      }{%
        \@nameuse{add#1tocentry}%
      }{\csname\@ifundefined{#1tocformat}{the#1}{#1tocformat}\endcsname}%
      {\@currenttocentry}%
    \fi
  }{%
%    \end{macrocode}
% \changes{v3.18}{2015/05/23}{execute counter reset list for not numbered
%   headings}
% For not numbered headings we still have to process the reset list of the
% counter and write a process information to the \file{log} and the terminal.
%    \begin{macrocode}
%<!v4>    \expandafter\ifnum\scr@v@is@ge{3.18}\relax
      \begingroup
        \let\@elt\@stpelt
        \csname cl@#1\endcsname
      \endgroup
%<!v4>    \fi
    \typeout{#1 without number}%
%    \end{macrocode}
% \changes{v3.08}{2010/11/01}{using \cs{addparttocentry}}
% \changes{v3.18}{2015/05/23}{generalized \cs{addparttocentry}}
% And before writing a not numbered entry to the table of contents we have to
% execute also the \pkg{hyperref} hook \cs{hy@insteadofrefstepcounter}.
% \begin{description}
% \item[Note:] \pkg*{scrhack} uses the existence of this command to detect a
%   \KOMAScript{} class. Shouldn't this be changed using, e.g.,
%   \cs{KOMAClassName}?
% \end{description}
%    \begin{macrocode}
    \ifx\@currenttocentry\@empty\else
      \hy@insteadofrefstepcounter{#1}%
      \scr@ifundefinedorrelax{add#1tocentry}{%
        \addtocentrydefault{#1}%
      }{%
        \@nameuse{add#1tocentry}%
      }{}{\@currenttocentry}%
    \fi
  }%
%    \end{macrocode}
% \changes{v2.2c}{1995/05/25}{support for CJK}
% \changes{v2.4b}{1996/03/29}{\cs{size@partnumer} replaced by
%   \cs{size@partnumber}}
% \changes{v2.8p}{2001/09/22}{\cs{sectfont} used before \cs{size@part} or
%   \cs{size@partnumber}}
% \changes{v2.8q}{2002/02/28}{\cs{nobreak} after \cs{sectfont}}
% \changes{v3.18}{2015/05/23}{\cs{size@\dots} replaced by \cs{usekomafont}}
% \changes{v3.21}{2016/06/12}{missing \cs{nobreak} added}
% \changes{v3.25}{2017/09/07}{defining \cs{IfUseNumber} locally}
% \changes{v3.25}{2017/09/07}{\cs{sectfont} replaced by \cs{usekomafont}}
% \changes{v3.25}{2017/09/07}{using \cs{partlineswithprefixformat}}
% \changes{v3.25}{2018/02/12}{using correct fontelement}
% \changes{v3.28}{2019/11/19}{\cs{ifnumbered} renamed to
% \cs{Ifnumbered}}
% Now the main printing code.
% \begin{description}
% \item[ToDo:] This code is almost the same as \cs{scr@@startchapter}. Only
%   the procession of the reset list was there already with
%   \KOMAScript~v3.15. And in \cs{scr@@startchapter} we are using
%   \cs{@make\meta{name}head} here. So maybe we should do the same here and
%   really use the same definition instead of having it twice.
% \end{description}
%    \begin{macrocode}
  \begingroup
    \def\IfUseNumber{\Ifnumbered{#1}}%
    \ExecuteDoHook{heading/begingroup/#1}%
    \setparsizes{\z@}{\z@}{\z@\@plus 1fil}\par@updaterelative
    \raggedpart
    \normalfont\usekomafont{disposition}{\nobreak
      \IfUseNumber{%
        \partlineswithprefixformat{#1}{%
          \usekomafont{#1prefix}{\nobreak\@nameuse{#1format}}%
%<book|report>        \setlength{\@tempskipa}{\@nameuse{scr@#1@innerskip}}%
          \partheadmidvskip
        }{%
          \usekomafont{#1}{\nobreak\interlinepenalty \@M#3\strut\@@par}%
        }%
      }{%
        \partlineswithprefixformat{#1}{}{%
          \usekomafont{#1}{\nobreak\interlinepenalty \@M#3\strut\@@par}%
        }
      }%
%    \end{macrocode}
% \changes{v3.10}{2011/08/30}{using\cs{@mkboth{}{}} if \cs{partmark} is
%   \cs{@gobble}}
% The running head.
%    \begin{macrocode}
      \expandafter\ifx\csname #1mark\endcsname\@gobble
        \@mkboth{}{}%
      \else
        \csname #1mark\expandafter\endcsname\expandafter{\@currentheadentry}%
      \fi
    }%
    \ExecuteDoHook{heading/endgroup/#1}%
  \endgroup
%    \end{macrocode}
% The final distance.
%    \begin{macrocode}
%<*article>
  \nobreak
  \@tempskipa=\glueexpr\@nameuse{scr@#1@afterskip}\relax\relax
  \ifdim\@tempskipa<\z@\@tempskipa-\@tempskipa\fi
  \partheadendvskip
%</article>
%<book|report>  \@nameuse{@end#1}%
%    \end{macrocode}
% \changes{v3.26}{2018/10/14}{using \@cs{@afterheading} with
%   \cls*{scrbook} or \cls*{scrreprt}}
% And \cs{@afterheading} for all classes.
%    \begin{macrocode}
  \@afterheading
}
%    \end{macrocode}
% \end{macro}^^A \scr@@startpart
%
% \begin{macro}{\scr@@startspart}
% \changes{v2.4n}{1997/05/28}{added \cs{chaptermark} resp.
%   \cs{sectionmark} to star variant}
% \changes{v2.6a}{2000/01/20}{using \cs{@mkboth} to delete the running head}
% \changes{v2.8q}{2001/11/13}{\cs{@parskipfalse}\cs{@parskip@indent}}
% \changes{v2.95}{2002/08/13}{\cs{centering} and
%   \cs{@parskipfalse}\cs{@parskip@indent} swapped}
% \changes{v2.95}{2004/11/05}{\cs{@parskipfalse} and\cs{@parskip@indent}
%   replaced}
% \changes{v3.18}{2015/05/23}{added}
% The macro
% \begin{quote}
%   \cs{scr@@startspart}\marg{part command name}\marg{part heading}
% \end{quote}
% is now part of the style \texttt{part}. However, the main parts
% of the code are from the original definition of \cs{@spart}. Therefore it has
% the mandatory argument of a star variant of, i.e., \cs{part} but an
% additional first argument.
%    \begin{macrocode}
\newcommand{\scr@@startspart}[2]{%
  \ExecuteDoHook{heading/branch/star/#1}%
%    \end{macrocode}
% Some users don't learn to use \cs{addpart} for not numbered parts with entry
% to the table of contents but insist in using \cs{addcontentsline}. Despite
% this should not be used, because at least \cs{addparttocentry} should be
% used, we tolerate this by using our \pkg{hyperref} hook.
% \begin{description}
% \item[ToDo:] I don't understand why \pkg{hyperref} uses a group here and
%   locally sets \cs{@mkboth} to \cs{@gobbletwo}. However, I've copied this.
% \end{description}
%    \begin{macrocode}
  \begingroup
    \let\@mkboth\@gobbletwo
    \hy@insteadofrefstepcounter{#1}%
  \endgroup
%    \end{macrocode}
% \changes{v3.21}{2016/06/12}{missing \cs{nobreak} added}
% \changes{v3.25}{2017/09/07}{difining \cs{IfUseNumber} locally}
% \changes{v3.25}{2017/09/07}{\cs{sectfont} replaced by \cs{usekomafont}}
% \changes{v3.25}{2017/09/07}{using \cs{partlineswithprefixformat}}
% Now the printing similar to \cs{scr@@startpart}.
%    \begin{macrocode}
  \begingroup
    \let\IfUseNumber\@secondoftwo
    \ExecuteDoHook{heading/begingroup/#1}%
    \setparsizes{\z@}{\z@}{\z@\@plus 1fil}\par@updaterelative
    \raggedpart
    \normalfont\usekomafont{disposition}{%
      \nobreak
      \partlineswithprefixformat{#1}{}{%
        \usekomafont{#1}{\nobreak\interlinepenalty \@M#2\strut\@@par}%
      }%
      \@mkboth{}{}%
    }%
    \ExecuteDoHook{heading/endgroup/#1}%
  \endgroup
%<*article>
  \nobreak
  \@tempskipa=\glueexpr\csname scr@#1@afterskip\endcsname\relax\relax
  \ifdim\@tempskipa<\z@\@tempskipa-\@tempskipa\fi
  \partheadendvskip
  \@afterheading
%</article>
%<book|report>  \@nameuse{@end#1}%
}
%    \end{macrocode}
% \end{macro}^^A \scr@@startspart
%
% \begin{command}{\partlineswithprefixformat}
% \changes{v3.25}{2017/09/07}{added}
% Command
% \begin{quote}
%   \cs{partlineswithprefixformat}\marg{command name}\marg{number}\marg{text}
% \end{quote}
% is used to print a part heading with a prefix line. \meta{number} is the
% completely formatted prefix line inclusive the vertical distance below. The
% \meta{text} is the text of the heading also formatted and with final
% \cs{@@par}. But if the user uses more paragraphs than this final one, the
% user is responsible to prevent page breaks!
%    \begin{macrocode}
\newcommand{\partlineswithprefixformat}[3]{%
  #2#3%
}
%    \end{macrocode}
% \end{command}^^A \partlineswithprefixformat
%
% \begin{macro}{\scr@@endpart}
% \changes{v2.3g}{1996/01/14}{\cs{@endpart} not defined for \cls*{scrartcl}}
% \changes{v2.4e}{1996/07/02}{supporting option \opt{openany}}
% \changes{v2.6c}{2000/06/10}{\cs{@endpart} respects \opt{open\quotechar=right}}
% \changes{v2.8p}{2001/09/27}{\cs{vbox} added}
% \changes{v2.8p}{2001/09/27}{\cs{@part@preamble} replaced by \cs{use@preamble}}
% \changes{v3.02}{2009/01/01}{using \cs{partheademptypage}}
% \changes{v3.18}{2015/05/23}{added}
% Before \KOMAScript~v3.18 this code was part of \cs{part}. \cls*{scrbook} and
% \cls*{scrreprt} use this macro to finalize the heading, e.g., print a
% preamble, to the final distance or new page and add an empty retro
% page. \cls*{scrartcl} does not have such a command, because with
% \cls*{scrartcl} parts are simple headings not pages. As usual the argument
% is the name of the part command, e.g., \texttt{part}.
%    \begin{macrocode}
%<*book|report>
\newcommand*{\scr@@endpart}[1]{%
  \vbox to\z@{\use@preamble{#1@u}\vss}%
  \@tempskipa=\glueexpr\csname scr@#1@afterskip\endcsname\relax\relax
  \ifdim\@tempskipa<\z@\@tempskipa-\@tempskipa\fi
  \partheadendvskip
  \partheademptypage
  \if@tempswa
    \twocolumn
  \fi
}
%</book|report>
%    \end{macrocode}
% \end{macro}^^A \scr@@endpart
% \end{macro}^^A \scr@startpart
%
%
% \begin{macro}{\hy@insteadofrefstepcounter}
% \changes{v3.18}{2015/05/22}{added to be used by \pkg{hyperref}}
% \begin{macro}{\scr@chapter@before@hyperref@patch}
% \changes{v3.27}{2019/07/24}{added to be used by \pkg*{scrhack}}
% \begin{macro}{\scr@chapter@after@hyperref@patch}
% \changes{v3.27}{2019/07/24}{added to be used by \pkg*{scrhack}}
% \changes{v3.32}{2020/09/11}{\cs{AfterPackage!} replaced by
%   \cs{AfterAtEndOfPackage}}
% Usually \pkg{hyperref} uses \cs{refstepcounter} to process extra codes for
% anchors and bookmarks. However, if a not numbered heading is used,
% \pkg{hyperref} cannot use \cs{refstepcounter} but still has to add
% code. Usually \pkg{hyperref} patchs several commands like \cs{@chapter},
% \cs{@schapter}, \cs{@part}, \cs{@spart} etc. to do so. But I think, it would
% be better to provide a hook for this, \pkg*{hyperref} can change. So we've
% designed \cs{hy@insteadofrefstepcounter}. Unfortunately \pkg{hyperref}
% currently does not care for this, but still does its patches. So we need to
% prevent \pkg{hyperref} from doing so and additionally define
% \cs{hy@insteadofrefstepcounter} ourselves. But because \cs{autoref} is
% nonsense for not numbered headings, I do not use \cs{Hy@chapapp} for
% \cs{Hy@MakeCurrentHrefAuto}.
% \begin{description}
% \item[ToDo:] Explain the Oberdiek support team, what we are doing here and
%   what they should do.
% \end{description}
%    \begin{macrocode}
\newcommand*{\hy@insteadofrefstepcounter}[1]{}
\let\hy@insteadofrefstepcounter\@gobble
\newcommand*{\scr@chapter@before@hyperref@patch}{%
  \let\scr@orig@chapter\@chapter
  \let\scr@orig@schapter\@schapter
  \let\scr@orig@addchap\@addchap
}
\BeforePackage{hyperref}{\scr@chapter@before@hyperref@patch}
\newcommand*{\scr@chapter@after@hyperref@patch}{%
  \let\@chapter\scr@orig@chapter
  \let\@schapter\scr@orig@schapter
  \let\@addchap\scr@orig@addchap
  \ifx\hy@insteadofrefstepcounter\@gobble
    \renewcommand*{\hy@insteadofrefstepcounter}[1]{%
      \Hy@MakeCurrentHrefAuto{##1*}%
      \Hy@raisedlink{%
        \hyper@anchorstart{\@currentHref}\hyper@anchorend
      }%
    }%
  \fi
}
\AfterAtEndOfPackage{hyperref}{\scr@chapter@after@hyperref@patch}
%    \end{macrocode}
% \end{macro}^^A \scr@after@hyperref@patch
% \end{macro}^^A \scr@before@hyperref@patch
% \end{macro}^^A \hy@insteadofrefstepcounter
%
% \begin{command}{\addparttocentry}
% \changes{v3.08}{2010/11/01}{added}
% We do not use \cs{addcontentsline} directly for the entries to the table of
% contents. Using
% \begin{quote}
%   \cs{addparttocentry}\marg{number}\marg{text}
% \end{quote}
% allows easy manipulation of such entries and additional actions. If the
% \meta{number} is completely empty no number should be used. Internally the
% generic \cs{addtocentrydefault} it used, which uses \cs{nonumberline}
% instead of \cs{numberline} in this case. See \file{scrkernel-listsof.dtx}
% for more information.
%    \begin{macrocode}
\newcommand*{\addparttocentry}[2]{%
  \addtocentrydefault{part}{#1}{#2}%
}
%    \end{macrocode}
% \end{command}^^A \addparttocentry
%
%
% \begin{command}{\part}
% \changes{v3.18}{2015/05/23}{indirect definition}
% \begin{macro}{\@part,\@spart,\@endpart}
% \changes{v3.18}{2015/05/23}{indirect definition}
% \begin{command}{\setpartpreamble}
% \changes{v2.8f}{2001/07/12}{added}
% \changes{v2.8p}{2001/09/27}{using \cs{use@preamble}}
% \changes{v3.18}{2015/05/23}{indirect definition}
% \begin{macro}{\part@preamble,\part@u@preamble}
% \changes{v2.8p}{2001/09/27}{added}
% \changes{v2.8p}{2001/09/27}{\cs{part@preamble} renamed to \cs{part@u@preamble}}
% \changes{v3.12a}{2014/02/14}{name fixed}
% \changes{v3.18}{2015/05/23}{indirect definition}
% \begin{macro}{\part@o@preamble}
% \changes{v2.8p}{2001/09/27}{added}
% \changes{v3.12a}{2014/02/14}{name fixed}
% \changes{v3.18}{2015/05/23}{indirect definition}
% All these commands and macros are not defined explicitly but by using
% \cs{DeclareSectionCommand} later. See also
% \cs{scr@dsc@def@style@part@command} and \cs{setpartpreamble}.
% \end{macro}^^A \part@o@preamble
% \end{macro}^^A \part@u@preamble
% \end{command}^^A \setpartpreamble
% \end{macro}^^A \@part,\@spart,\@endpart
% \end{command}^^A \part
%
% \begin{command}{\addpart}
% \changes{v2.8c}{2001/06/29}{added}
% \changes{v2.8d}{2001/07/05}{using \cs{partpagestyle}}
% \changes{v2.95a}{2006/07/10}{support of preamble added}
% \changes{v3.11b}{2012/07/29}{missing negativ \cs{vskip} added}
% \changes{v3.13a}{2014/09/11}{using \cs{SecDef}}
% \changes{v3.15a}{2015/01/22}{distance adaption}
% \changes{v3.18}{2015/05/25}{redefinition}
% \begin{macro}{\@addpart}
% \changes{v2.8c}{2001/06/29}{added}
% \changes{v2.8l}{2001/08/17}{first ToC then output}
% \changes{v2.95}{2004/07/20}{support for running head}
% \changes{v3.00}{2008/07/01}{\pkg{hyperref} workaround}
% \changes{v3.08}{2010/11/02}{using \cs{addparttocentry}}
% \changes{v3.10}{2011/08/30}{extended optional argument}
% \changes{v3.10}{2011/08/30}{using \cs{@mkboth{}{}} if \cs{partmark} is
%   \cs{@gobble}}
% \changes{v3.17}{2015/04/20}{using \cs{addpartmark} but never \cs{@mkboth}}
% \changes{v3.18}{2015/05/25}{redefinition}
% \begin{macro}{\@saddpart}
% \changes{v2.8c}{2001/06/29}{added}
% \changes{v3.18}{2015/05/25}{redefinition}
% The improved not numbered version of \cs{part}. Note: This could also be
% done using the extended optional argument of \cs{part} and option
% \opt{nonumber=true}. So this command becomes more and more deprecated.
%    \begin{macrocode}
\newcommand\addpart{%
  \SecDef\@addpart\@saddpart
}
\newcommand*{\@addpart}{}
\long\def\@addpart[#1]#2{%
  \edef\reserved@a{%
    \unexpanded{%
      \part[{#1}]{#2}%
      \c@secnumdepth=
    }\the\c@secnumdepth\relax
  }%
  \c@secnumdepth=\numexpr \partnumdepth-1\relax
  \reserved@a
}
\newcommand{\@saddpart}[1]{%
  \part*{#1}%
  \addpartmark{}%
}
%    \end{macrocode}
% \end{macro}^^A \@saddpart
% \end{macro}^^A \@addpart
% \end{command}^^A \addpart
%
%
% \begin{macro}{\l@part}
% \changes{v2.97c}{2007/06/21}{\cs{sectfont}\cs{large} replaced by font
%   element \fnt{partentry}}
% \changes{v2.97c}{2007/06/21}{using font element \fnt{partentrypagenumber}}
% \changes{v3.15}{2014/12/23}{using \cs{parttocdepth} and
%     \cs{scr@part@numwidth}}
% \changes{v3.20}{2015/10/06}{replaced by usage of ToC style}
% \begin{macro}{\scr@part@tocindent}
% \changes{v3.15}{2014/12/02}{added}
% \changes{v3.18}{2015/06/09}{indirect by \cs{DeclareSectionCommand}}
% \end{macro}^^A \scr@part@tocindent
% \begin{macro}{\scr@part@tocnumwidth}
% \changes{v3.15}{2014/12/02}{added}
% \changes{v3.18}{2015/06/09}{indirect by\cs{DeclareSectionCommand}}
% These macros are not longer defined explicitly but by
% \cs{DeclareTOCEntryStyle} and indirectly by \cs{DeclareSectionCommand}.
% \end{macro}
% \end{macro}^^A \l@part
%
%
% \begin{counter}{part}
% \begin{command}{\thepart}
% \begin{command}{\partformat}
% \changes{v2.3c}{1995/08/06}{auto dot feature}
% \changes{v2.5f}{1999/02/14}{orphan spaces removed}
% \changes{v2.7h}{2001/04/22}{missing backslash of \cs{autodot} fixed}
% Because we don't use the default Arabic numbering, we still define the
% counter for \cs{part}, it's printing and the formatting explicitly.
%    \begin{macrocode}
\newcounter{part}
\renewcommand*{\thepart}{\@Roman\c@part}
\newcommand*{\partformat}{\partname~\thepart\autodot}
%    \end{macrocode}
% \end{command}^^A \partformat
% \end{command}^^A \thepart
% \end{counter}{part}
%
%
% \begin{command}{\partname}
% \changes{v3.36}{2022/03/15}{explicit definition for English languages}
% The name of a part used for the prefix line and maybe for the ToC entry.
%    \begin{macrocode}
\newcommand*\partname{Part}
\providecaptionname{american,australian,british,canadian,english,newzealand,%
  UKenglish,ukenglish,USenglish,usenglish}\partname{Part}
%    \end{macrocode}
% \end{command}^^A \partname
%
%
% \begin{command}{\partmark}
% \changes{v2.9r}{2004/07/20}{added}
% \changes{v3.18}{2015/05/24}{default changed from \cs{@gobble} to
%   \cs{@mkboth{}{}}}
% Initially \cs{part} should simply empty the left and right mark if an
% automatic running head is used. This is independent from the page style.
%    \begin{macrocode}
\newcommand*{\partmark}[1]{\@mkboth{}{}}
%    \end{macrocode}
% \end{command}^^A \partmark
%
% \begin{command}{\addpartmark}
% \changes{v3.17}{2015/04/20}{added}
% \changes{v3.18}{2015/05/24}{depends on \cs{partnumdepth}}
% To be used to add the marks with \cs{addpart}. Locally we define
% \cs{if@mainmatter}, because it makes life easier for users or package
% authors, who (re-)define \cs{partmark}.
%    \begin{macrocode}
\newcommand*\addpartmark[1]{%
  \begingroup
    \expandafter\let\csname if@mainmatter\expandafter\endcsname
    \csname iffalse\endcsname
    \c@secnumdepth=\numexpr \partnumdepth-1\relax
    \partmark{#1}%
  \endgroup
}
%    \end{macrocode}
% \end{command}^^A \addpartmark
%
%
% \begin{command}{\raggedpart}
% \changes{v2.95a}{2006/07/10}{added}
% Style \texttt{part} does not use \cs{raggedsection} but
% \cs{raggedpart}. However for \cls*{scrartcl} the default is the same. But
% \cls*{scrbook} and \cls*{scrreprt} use \cs{centering} as default.
% \begin{description}
% \item[Note:] Because of using \cs{let} inside a class, \cs{raggedright}
%   should be without hyphenation when we define \cs{raggedpart}.
% \end{description}
%    \begin{macrocode}
\newcommand*{\raggedpart}{}
%<article>\let\raggedpart\raggedsection
%<report|book>\let\raggedpart\centering
%    \end{macrocode}
% \end{command}
%
% \begin{fontelement}{partentry}
% \changes{v2.97c}{2007/06/21}{added}
% \changes{v3.06}{2010/06/09}{\fnt{sectioning} replaced by \fnt{disposition}}
% \cs{part} entries to the table of contents are \cs{large}.
%    \begin{macrocode}
\newkomafont{partentry}{\usekomafont{disposition}\large}
%    \end{macrocode}
% \end{fontelement}
%
% \begin{fontelement}{partentrypagenumber}
% \changes{v2.97c}{2007/06/21}{added}
% The page number of the ToC entry should be unchanged.
%    \begin{macrocode}
\newkomafont{partentrypagenumber}{}
%</body>
%    \end{macrocode}
% \end{fontelement}
%
%
% \subsection{Chapters} 
%
% Classes \cls*{scrbook} and \cls*{scrreprt} provide chapters. These are
% realized by \cs{DeclareSectionCommand} and the \texttt{chapter} style. The
% style is represented by \cs{scr@dsc@def@style@chapter@command} and several
% helpers.
% \begin{description}
% \item[Note:] Package \pkg{hyperref} is not really compatible with
%   \KOMAScript{} classes, because it redefines \cs{@schapter} and
%   \cs{@chapter} incompatible. However, currently \KOMAScript{} has a build
%   in workaround for this. But this workaround itself depends on internal
%   code of \pkg{hyperref}. It would be better, if \pkg{hyperref} would
%   provide an interface for class authors and would redefine internal macros
%   of classes only as fallback if classes do not use this
%   interface. \KOMAScript{} also provides an alternative. \pkg*{hyperref}
%   could redefine a single command \cs{hy@insteadofrefstepcounter}.
% \item[Note:] Any package, that changes internal macros like \cs{@chapter},
%   \cs{@schapter}, \cs{@makechapterhead}, \cs{@makeschapterhead} is
%   incompatible with \KOMAScript{} classes. Neither \KOMAScript{} nor the
%   \KOMAScript{} author is responsible for this. Package authors are welcome
%   to ask for interfaces they need, e.g., a good idea would be to define a
%   own headings style.
% \end{description}
%
% \begin{option}{chapterprefix}
% \changes{v2.8}{2001/06/15}{added}
% \changes{v2.95c}{2006/08/21}{changed to \pkg{keyval} option}
% \changes{v3.18}{2015/05/22}{option correlates with style not level}
% \changes{v3.39}{2022/11/11}{initial dot removed from member argument of
%   option storage commands}
% \begin{macro}{\@chapterprefixtrue,\@chapterprefixfalse,\if@chapterprefix}
% Initially \KOMAScript{} does not use the enormous chapter headings with
% prefix line ``Chapter \meta{number}'' but a more subtle one. However, either
% \opt{chapterprefix} or \opt{headings=twolinechapter} can be used to
% reactivate the prefix line. The current setting is available via the boolean
% switch \cs{if@chapterprefix}.
%    \begin{macrocode}
%<*book|report>
%<prepare>\newif\if@chapterprefix
%<*option>
\KOMA@key{chapterprefix}[true]{%
  \KOMA@set@ifkey{chapterprefix}{@chapterprefix}{#1}%
  \ifx\FamilyKeyState\FamilyKeyStateProcessed
    \KOMA@kav@replacebool{\KOMAClassFileName}%
                         {chapterprefix}{@chapterprefix}%
    \KOMA@kav@remove{\KOMAClassFileName}%
                    {headings}{twolinechapter}%
    \KOMA@kav@remove{\KOMAClassFileName}%
                    {headings}{onelinechapter}%
    \KOMA@kav@xadd{\KOMAClassFileName}%
                  {headings}{%
                    \if@chapterprefix twolinechapter\else onelinechapter\fi
                  }%
  \fi
}
\KOMA@kav@add{\KOMAClassFileName}{headings}{onelinechapter}%
%    \end{macrocode}
% \begin{option}{nochapterprefix}
% \changes{v2.8}{2001/06/15}{added}
% \changes{v2.95c}{2006/08/21}{obsolete}
% \changes{v3.01a}{2008/11/20}{deprecated}
% \changes{v3.99}{2022/11/16}{removed from \KOMAScript~4}
% Deprecated option for compatibility with \KOMAScript~2.9u only.
%    \begin{macrocode}
%<!v4>\KOMA@DeclareDeprecatedOption{nochapterprefix}{chapterprefix=false}
%</option>
%    \end{macrocode}
% \end{option}^^A nochapterprefix
% \end{macro}^^A \if@chapterprefix
% \end{option}^^A chapterprefix
%
%
% \begin{option}{appendixprefix}
% \changes{v2.8}{2001/06/15}{added}
% \changes{v2.95c}{2006/08/21}{re-implementation as \pkg{keyval} option}
% \changes{v3.12}{2013/03/05}{usage of \cs{FamilyKeyState}}
% \changes{v3.17}{2015/03/09}{internal value storage}
% \changes{v3.34}{2021/04/30}{usage of \LaTeX{} hook, if available}
% \changes{v3.34}{2021/04/30}{new value \opt{\quotechar=default} with \LaTeX{}
%   hooks}
% \changes{v3.39}{2022/11/11}{initial dot removed from member argument of
%   option storage commands}
% If the appendix chapters should be with or without prefix line independent
% from the chapters of the main part, option \opt{appendixprefix} can be
% used. This does not set up a new boolean but just activates or deactivates
% the boolean for chapters inside \cs{appendix}. From \KOMAScript{} v3.34 and
% \LaTeX{} 2020/10/01 this is done using a new \LaTeX{} hook. Otherwise still
% \cs{appendixmore} is used. Default is, using the settings for chapters.
%    \begin{macrocode}
%<*option>
\IfLTXAtLeastTF{2020/10/01}{%
  \KOMA@key{appendixprefix}[true]{%
    \Ifstr{#1}{default}{%
      \KOMA@kav@removekey{\KOMAClassFileName}{appendixprefix}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{twolineappendix}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{onelineappendix}%
      \IfHookEmptyTF{\KOMAClassName/appendix}{}{%
        \RemoveFromHook{\KOMAClassName/appendix}[appendixprefix]%
      }%
      \FamilyKeyStateProcessed
    }{%
      \KOMA@set@ifkey{appendixprefix}{@tempswa}{#1}%
      \ifx\FamilyKeyState\FamilyKeyStateProcessed
        \KOMA@kav@xreplacevalue{\KOMAClassFileName}{appendixprefix}{#1}%
        \KOMA@kav@remove{\KOMAClassFileName}{headings}{twolineappendix}%
        \KOMA@kav@remove{\KOMAClassFileName}{headings}{onelineappendix}%
        \IfHookEmptyTF{\KOMAClassName/appendix}{}{%
          \RemoveFromHook{\KOMAClassName/appendix}[appendixprefix]%
        }%
        \if@tempswa
          \KOMA@kav@add{\KOMAClassFileName}{headings}{twolineappendix}%
          \AddToHook{\KOMAClassName/appendix}[appendixprefix]%
                    {\@chapterprefixtrue}%
        \else
          \KOMA@kav@add{\KOMAClassFileName}{headings}{onelineappendix}%
          \AddToHook{\KOMAClassName/appendix}[appendixprefix]%
                    {\@chapterprefixfalse}%
        \fi
      \fi
    }%  
  }%
}{%
  \KOMA@key{appendixprefix}[true]{%
    \KOMA@set@ifkey{appendixprefix}{@tempswa}{#1}%
    \ifx\FamilyKeyState\FamilyKeyStateProcessed
      \KOMA@kav@xreplacevalue{\KOMAClassFileName}{appendixprefix}{#1}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{twolineappendix}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{onelineappendix}%
      \if@tempswa
        \KOMA@kav@add{\KOMAClassFileName}{headings}{twolineappendix}%
        \@ifundefined{appendixmore}{%
          \def\appendixmore{\@chapterprefixtrue}%
        }{%
          \l@addto@macro\appendixmore{\@chapterprefixtrue}%
        }%
      \else
        \KOMA@kav@add{\KOMAClassFileName}{headings}{onelineappendix}%
        \@ifundefined{appendixmore}{%
          \def\appendixmore{\@chapterprefixfalse}%
        }{%
          \l@addto@macro\appendixmore{\@chapterprefixfalse}%
        }%
      \fi
    \fi
  }%  
}
%    \end{macrocode}
% \begin{option}{noappendixprefix}
% \changes{v2.8}{2001/06/15}{added}
% \changes{v2.95c}{2006/08/21}{obsolete}
% \changes{v3.01a}{2008/11/20}{deprecated}
% \changes{v3.99}{2022/11/16}{removed from \KOMAScript~4}
%    \begin{macrocode}
%<!v4>\KOMA@DeclareDeprecatedOption{noappendixprefix}{appendixprefix=false}
%</option>
%    \end{macrocode}
% \end{option}^^A noappendixprefix
% \end{option}^^A appendixprefix
%
%
% \begin{fontelement}{chapter}
% \changes{v2.8o}{2001/09/14}{added}
% \changes{v2.96a}{2006/12/02}{undocumented support for last macro expects an
%   argument}
% \begin{fontelement}{chapterprefix}
% \changes{v2.8o}{2001/09/14}{added by macro}
% \changes{v2.96a}{2006/12/02}{added}
% Definition of the font elements used by \cs{chapter}. The settings are done
% by option \opt{headings} later. Everything is prepared before this option
% is used the first time. The manual definition is used, because of
% compatibility with \KOMAScript{} 2.9u. However, using the old macros is
% deprecated and not supported any longer.
%    \begin{macrocode}
%<*prepare>
\newcommand*{\size@chapter}{}
\newcommand*{\scr@fnt@chapter}{\size@chapter}
\newcommand*{\size@chapterprefix}{\size@chapter}
\newcommand*{\scr@fnt@chapterprefix}{\size@chapterprefix}
%</prepare>
%    \end{macrocode}
% \end{fontelement}^^A chapterprefix
% \end{fontelement}^^A chapter
%
%
% \begin{command}{\chapterheadstartvskip}
% \changes{v3.15}{2014/11/24}{defining and using \cs{scr@chapter@beforeskip}}
% \begin{command}{\chapterheadmidvskip}
% \changes{v3.15}{2014/11/20}{added}
% \changes{v3.17}{2015/03/25}{defining and using \cs{scr@chapter@innerskip}}
% \begin{command}{\chapterheadendvskip}
% \changes{v3.11c}{2013/02/13}{\cs{chapterheadstartvskip} and
%     \cs{chapterheadendvskip} removed from \cls*{scrartcl}}
% \changes{v3.15}{2014/11/24}{defining and using \cs{scr@chapter@afterskip}}
% \changes{v3.15}{2014/12/10}{\cs{vspace} replaced by \cs{vskip} because of
%   \cs{lastskip}}
% These commands are the distance before a chapter heading, between the
% prefix line and the text line of the heading and after the chapter heading.
% \cs{chapterheadmidvskip} is only used with option \opt{chapterprefix}
% resp. \opt{headings=twolinechapter}.
%
% \begin{macro}{\scr@chapter@sectionbeforeskip,\scr@chapter@beforeskip}
% \changes{v3.15}{2014/11/24}{added}
% \changes{v3.17}{2015/03/25}{\cs{scr@chapter@sectionbeforeskip} renamed to
%   \cs{scr@chapter@beforeskip}}
% \begin{macro}{\scr@chapter@innerskip}
% \changes{v3.17}{2015/03/25}{added}
% \begin{macro}{\scr@chapter@sectionafterskip,\scr@chapter@afterskip}
% \changes{v3.15}{2014/11/24}{added}
% \changes{v3.17}{2015/03/25}{\cs{scr@chapter@sectionafterskip} renamed to
%   \cs{scr@chapter@afterskip}}
% \begin{macro}{\scr@chapter@sectionindent}
% \changes{v3.15}{2014/11/24}{added}
% \changes{v3.17}{2015/04/24}{removed}
% \end{macro}^^A \scr@chapter@sectionindent
% \begin{macro}{\scr@chapter@style}
% \changes{v3.15}{2014/11/24}{added}
% All the skips and the page style are used by \cs{DeclareSectionCommand}
% resp. the style \texttt{chapter}. We define it as early as possible. Even the
% style of \cs{chapter} is already defined here.
% \begin{description}
% \item[ToDo:] Do we need them in the \texttt{prepare} run or can we move then
%   even to the \texttt{body} run?
% \item[ToDo:] Do we really need to define \cs{scr@chapter@style}?
% \end{description}
%    \begin{macrocode}
%<*prepare>
\newcommand*{\scr@chapter@beforeskip}{\z@}
\let\scr@chapter@beforeskip\z@
\newcommand*{\scr@chapter@afterskip}{\z@}
\let\scr@chapter@afterskip\z@
\newcommand*{\scr@chapter@innerskip}{\z@}
\let\scr@chapter@innerskip\z@
\newcommand*{\scr@chapter@style}{chapter}
%    \end{macrocode}
% \end{macro}^^A \scr@chapter@style
% \end{macro}^^A \scr@chapter@afterskip
% \end{macro}^^A \scr@chapter@innerskip
% \end{macro}^^A \scr@chapter@beforeskip
%    \begin{macrocode}
\newcommand*{\chapterheadstartvskip}{\vspace{\@tempskipa}}
\newcommand*{\chapterheadendvskip}{\vskip\@tempskipa}
\newcommand*{\chapterheadmidvskip}{\par\nobreak\vskip\@tempskipa}
%</prepare>
%    \end{macrocode}
% \end{command}^^A \chapterheadendvskip
% \end{command}^^A \chapterheadmidvskip
% \end{command}^^A \chapterheadstartvskip
%
%
% \begin{macro}{\scr@startchapter}
% \changes{v3.18}{2015/05/22}{added}
% \changes{v3.19}{2015/08/25}{ignore empty page styles}
% The macro
% \begin{quote}
%   \cs{scr@startchapter}\marg{chapter command name}
% \end{quote}
% is now part of the style \texttt{chapter}. However, the main parts
% of the code are from the original definition of \cs{chapter}. It has only one
% argument: The name of the chapter command, i.e., \texttt{chapter}. All
% arguments to the part command will be processed later.
%    \begin{macrocode}
%<*body>
\newcommand*{\scr@startchapter}[1]{%
  \ExecuteDoHook{heading/preinit/#1}%
  \if@openright\cleardoublepage\else\clearpage\fi
  \scr@ifundefinedorrelax{#1pagestyle}{}{%
    \Ifstr{#1pagestyle}{}{}{%
      \thispagestyle{\@nameuse{#1pagestyle}}%
    }%
  }%
  \global\@topnum\z@
  \@ifundefined{scr@#1@afterindent}{\@afterindentfalse}{%
    \csname scr@#1@afterindent\endcsname
    {\@afterindenttrue}{\@afterindentfalse}{%
      \@afterindenttrue
      \@ifundefined{scr@#1@beforeskip}{\@afterindentfalse}{%
        \ifdim\glueexpr\@nameuse{scr@#1@beforeskip}\relax<\z@
          \@afterindentfalse
        \fi
      }%  
    }%
  }%
  \ExecuteDoHook{heading/postinit/#1}%
  \expandafter\SecDef\csname @#1\expandafter\endcsname\csname @s#1\endcsname
}
%    \end{macrocode}
%
% \begin{macro}{\scr@@startchapter}
% \changes{v3.18}{2015/05/22}{added}
% The macro
% \begin{quote}
%   \cs{scr@@startchapter}\marg{chapter command name}\oarg{extended optional
%   argument}\marg{chapter heading}
% \end{quote}
% is now part of the style \texttt{chapter}. However, the main parts
% of the code are from the original definition of \cs{@chapter}. Therefore it
% has the mandatory and optional arguments of a non-star variant of, i.e.,
% \cs{chapter} but an additional first argument.
% \begin{description}
% \item[Note:] \meta{extended optional argument} is named as optional, however
%   here it is already mandatory, because it is a delimited argument.
% \end{description}
%    \begin{macrocode}
\newcommand*{\scr@@startchapter}{}
\def\scr@@startchapter#1[#2]#3{%
  \ExecuteDoHook{heading/branch/nostar/#1}%
%    \end{macrocode}
% \changes{v3.10}{2011/08/30}{extended optional argument}
% \changes{v3.27}{2019/07/08}{option \opt{nonumber} added}
% First of all, depending in \opt{headings=optionto\dots} we have different
% defaults for the options.
%    \begin{macrocode}
  \ifnum \scr@osectarg=\z@
    \@scr@tempswafalse
  \else
    \scr@istest#2=\@nil
  \fi
  \@currentusenumbertrue  
  \if@scr@tempswa
    \setkeys{KOMAarg.section}{tocentry={#3},head={#3},reference={#3},#2}%
  \else
    \ifcase \scr@osectarg\relax
      \setkeys{KOMAarg.section}{tocentry={#2},head={#2},reference={#2}}%
    \or
      \setkeys{KOMAarg.section}{tocentry={#3},head={#2},reference={#3}}%
    \or
      \setkeys{KOMAarg.section}{tocentry={#2},head={#3},reference={#2}}%
    \or
      \setkeys{KOMAarg.section}{tocentry={#2},head={#2},reference={#2}}%
    \fi
  \fi
%    \end{macrocode}
% \changes{v2.8e}{2001/07/10}{using \cs{@maybeautodot}}
% \changes{v3.08}{2010/11/01}{using \cs{addchaptertocentry}}
% \changes{v3.18}{2015/05/20}{using \cs{add\meta{name}tocentry}}
% \changes{v3.18}{2015/05/22}{using \meta{name} instead of \cs{@chapapp}
%   in \cs{typeout}}
% \changes{v3.28}{2019/11/19}{\cs{ifnumbered} renamed to
%   \cs{Ifnumbered}}
% For numbered headings we have to increase the counter and write a process
% information to the \file{log} and the terminal.
%    \begin{macrocode}
  \Ifnumbered{#1}{%
%<book>    \if@mainmatter
      \@tempswatrue
%<book>    \else\@tempswafalse\fi
  }{\@tempswafalse}%
  \if@tempswa
    \refstepcounter{#1}%
    \expandafter\@maybeautodot\csname the#1\endcsname
    \typeout{#1 \csname the#1\endcsname.}%
    \ifx\@currenttocentry\@empty\else
%    \end{macrocode}
% \changes{v3.31}{2020/06/06}{using \cs{\dots tocformat}}%
% \begin{description}
% \item[ToDo]: Move \cs{autodot} to the definition of \cs{\dots tocformat}.
% \end{description}
%    \begin{macrocode}
      \@ifundefined{#1tocformat}{%
        \expandafter\gdef\csname #1tocformat\endcsname
        {\csname the#1\endcsname}%
      }{}%
      \scr@ifundefinedorrelax{add#1tocentry}{%
        \addtocentrydefault{#1}%
      }{%
        \@nameuse{add#1tocentry}%
      }{\csname\@ifundefined{#1tocformat}{the#1}{#1tocformat}\endcsname}%
      {\@currenttocentry}%
    \fi
  \else
%    \end{macrocode}
% For not numbered headings we still have to process the reset list of the
% counter and write a process information to the \file{log} and the terminal.
%    \begin{macrocode}
%<!v4>    \expandafter\ifnum\scr@v@is@ge{3.15}\relax
      \begingroup
        \let\@elt\@stpelt
        \csname cl@#1\endcsname
      \endgroup
%<!v4>    \fi
    \typeout{#1 without number}%
    \ifx\@currenttocentry\@empty\else
%    \end{macrocode}
% And before writing a not numbered entry to the table of contents we have to
% execute also the \pkg{hyperref} hook \cs{hy@insteadofrefstepcounter}.
% \begin{description}
% \item[Note:] \pkg*{scrhack} uses the existence of this command to detect a
%   \KOMAScript{} class. Shouldn't this be changed using, e.g.,
%   \cs{KOMAClassName}?
% \end{description}
%    \begin{macrocode}
      \hy@insteadofrefstepcounter{#1}%
      \scr@ifundefinedorrelax{add#1tocentry}{%
        \addtocentrydefault{#1}%
      }{%
        \@nameuse{add#1tocentry}%
      }{}{\@currenttocentry}%
    \fi
  \fi
%    \end{macrocode}
% Set the mark for the running head. Here we have to do a one step expansion
% of \cs{@currentheadentry}, because otherwise it would change with every
% \cs{section}, \cs{subsection} \dots
%    \begin{macrocode}
  \csname #1mark\expandafter\endcsname\expandafter{\@currentheadentry}%
%    \end{macrocode}
% \changes{v2.8g}{2001/07/18}{support for \cs{float@addtolists}}
% \changes{v3.43}{2024/10/24}{support for \cs{float@addtolists} removed}
% \changes{v3.28}{2019/11/19}{\cs{iftocfeature} replaced by \cs{Iftocfeature}}
% Depending on feature \texttt{chapteratlist} we have to add a vertical space
% to other directories. And for compatibility we also support the deprecated
% \cs{float@addtolists} but not without warning.
% \begin{description}
% \item[ToDo:] Why do we add the gap here but also use \cs{addcontentsline}
%   in \cs{addchaptertocentry}? Is this a bug? Should this be standardized?
% \end{description}
%    \begin{macrocode}
  \ifdim \@chapterlistsgap>\z@
    \doforeachtocfile{%
      \Iftocfeature{\@currext}{chapteratlist}{%
        \addtocontents{\@currext}{\protect\addvspace{\@chapterlistsgap}}%
      }{}%
    }%
    \expandafter\ifnum \scr@v@is@lt{3.43}\relax
      \@ifundefined{float@addtolists}{}{%
        \scr@float@addtolists@warning
        \float@addtolists{\protect\addvspace{\@chapterlistsgap}}%
      }%
    \fi
  \fi
%    \end{macrocode}
% \changes{v3.18}{2015/05/22}{\cs{@makechapterhead} replaced by
%   \cs{@make\meta{Name}head}}
% \changes{v3.36}{2022/03/16}{warning about deprecated \cs{@makechapterhead}}
% Setting the heading itself is more or less a call of \cs{@make\meta{command
% name}head}.
% \begin{description}
% \item[ToDo:] Using \cs{scr@makechapterhead}\marg{command name} would be the
%   better solution. However in this case, every redefinition of
%   \cs{@makechapterhead} would be ignored. So currently we support such
%   deprecated redefinition of internal macro \cs{@makechapterhead} for
%   \cs{chapter}. But this will be changed in future.
% \end{description}
%    \begin{macrocode}
  \scr@t@st@nd@w@rn@m@k@ch@pt@rh@@d{#1}%
  \if@twocolumn
    \if@at@twocolumn
      \@nameuse{@make#1head}{#3}%
    \else
      \@topnewpage[\@nameuse{@make#1head}{#3}]%
    \fi
  \else
    \@nameuse{@make#1head}{#3}%
  \fi
%    \end{macrocode}
% \changes{v3.18}{2015/05/21}{always using \cs{@afterheading}}
% And finally the end of every outline command.
%    \begin{macrocode}
  \@afterheading
}
%    \end{macrocode}
% \begin{macro}{\scr@t@st@nd@w@rn@m@k@ch@pt@rh@@d}
% \changes{v3.36}{2022/03/16}{added}
% The test and warning used inside \cs{scr@@startchapter}.
%    \begin{macrocode}
%<!v4>\expandafter\ifnum\scr@v@is@ge{3.36}%
  \newcommand*{\scr@t@st@nd@w@rn@m@k@ch@pt@rh@@d}[1]{%
    \Ifstr{#1}{chapter}{%
      \begingroup
        \def\reserved@a{\scr@makechapterhead{chapter}}%
        \ifx\@makechapterhead\reserved@a\else
          \ClassWarningNoLine{\KOMAClassName}{%
            Redefinition of \string\@makechapterhead\space detected.\MessageBreak
            Note: KOMA-Script does use styles for commands like\MessageBreak
            \string\chapter. Currently it internally still provides and\MessageBreak
            uses \string\@makechapterhead, but a completely different\MessageBreak
            definition compared to the standard classes.\MessageBreak
            So redefining or using internal \string\@makechapterhead\MessageBreak
            by users or package authors may already break\MessageBreak
            KOMA-Script and will not work with a coming version,\MessageBreak
            that will remove this deprecated code}%
          \let\scr@t@st@nd@w@rn@m@k@ch@pt@rh@@d\@gobbble
        \fi
      \endgroup
    }{}%
  }
%<!v4>\else
%<!v4>  \let\scr@t@st@nd@w@rn@m@k@ch@pt@rh@@d\@gobble
%<!v4>\fi
%    \end{macrocode}
% \end{macro}^^A \scr@t@st@nd@w@rn@m@k@ch@pt@rh@@d
% \end{macro}^^A \scr@@startchapter
%
% \begin{macro}{\scr@@startschapter}
% \changes{v3.18}{2015/05/22}{added}
% The macro
% \begin{quote}
%   \cs{scr@@startschapter}\marg{chapter command name}\marg{chapter heading}
% \end{quote}
% is now part of the style \texttt{chapter}. However, the main parts
% of the code are from the original definition of \cs{@schapter}. Therefore it
% has the mandatory argument of a star variant of, i.e., \cs{chapter} but an
% additional first argument.
%    \begin{macrocode}
\newcommand*{\scr@@startschapter}[2]{%
  \ExecuteDoHook{heading/branch/star/#1}%
%    \end{macrocode}
% Some users don't learn to use \cs{addchapter} for not numbered parts with
% entry to the table of contents but insist in using
% \cs{addcontentsline}. Despite this should not be used, because at least
% \cs{addchaptertocentry} should be used, we tolerate this by using our
% \pkg{hyperref} hook.
% \begin{description}
% \item[ToDo:] I don't understand why \pkg{hyperref} uses a group here and
%   locally sets \cs{@mkboth} to \cs{@gobbletwo}. However, I've copied this.
% \end{description}
%    \begin{macrocode}
  \begingroup
    \let\@mkboth\@gobbletwo
    \hy@insteadofrefstepcounter{#1}%
  \endgroup
%    \end{macrocode}
% Setting the heading itself is more or less a call of \cs{@makes\meta{command
% name}head}.
% \begin{description}
% \item[ToDo:] Using \cs{scr@makeschapterhead}\marg{command name} would be the
%   better solution. However in this case, every redefinition of
%   \cs{@makeschapterhead} would be ignored. So currently we support such
%   deprecated redefinition of internal macro \cs{@makeschapterhead} for
%   \cs{chapter}. But this will be changed in future.
% \end{description}
%    \begin{macrocode}
  \scr@t@st@nd@w@rn@m@ks@ch@pt@rh@@d{#1}%
  \if@twocolumn
    \if@at@twocolumn
      \@nameuse{@makes#1head}{#2}%
    \else
      \@topnewpage[\@nameuse{@makes#1head}{#2}]%
    \fi
  \else
    \@nameuse{@makes#1head}{#2}%
  \fi
%    \end{macrocode}
% \changes{v3.18}{2015/05/21}{always using \cs{@afterheading}}
% And finally the end of every outline command.
%    \begin{macrocode}
  \@afterheading
}
%    \end{macrocode}
% \begin{macro}{\scr@t@st@nd@w@rn@m@ks@ch@pt@rh@@d}
% \changes{v3.36}{2022/03/16}{added}
% The test and warning used inside \cs{scr@@startschapter}.
%    \begin{macrocode}
%<!v4>\expandafter\ifnum\scr@v@is@ge{3.36}%
  \newcommand*{\scr@t@st@nd@w@rn@m@ks@ch@pt@rh@@d}[1]{%
    \Ifstr{#1}{chapter}{%
      \begingroup
        \def\reserved@a{\scr@makeschapterhead{chapter}}%
        \ifx\@makeschapterhead\reserved@a\else
          \ClassWarningNoLine{\KOMAClassName}{%
            Redefinition of \string\@makeschapterhead\space detected.\MessageBreak
            Note: KOMA-Script does use styles for commands like\MessageBreak
            \string\chapter. Currently it internally still provides and\MessageBreak
            uses \string\@makeschapterhead, but a completely different\MessageBreak
            definition compared to the standard classes.\MessageBreak
            So redefining or using internal \string\@makeschapterhead\MessageBreak
            by users or package authors may already break\MessageBreak
            KOMA-Script and will not work with a coming version,\MessageBreak
            that will remove this deprecated code}%
          \let\scr@t@st@nd@w@rn@m@ks@ch@pt@rh@@d\@gobble
        \fi
      \endgroup
    }{}%
  }
%<!v4>\else
%<!v4>  \let\scr@t@st@nd@w@rn@m@ks@ch@pt@rh@@d\@gobble
%<!v4>\fi
%    \end{macrocode}
% \end{macro}^^A \scr@t@st@nd@w@rn@m@ks@ch@pt@rh@@d
% \end{macro}^^A \scr@@startschapter
%
%% \begin{macro}{\@at@twocolumntrue,\@at@twocolumnfalse,\if@at@twocolumn}
% \changes{v2.7b}{2001/01/05}{added}
% \begin{macro}{\scr@topnewpage}
% \changes{v2.7b}{2001/01/05}{added}
% \changes{v2.8q}{2001/11/27}{braces fixed}
% \begin{macro}{\@topnewpage}
% \changes{v2.7b}{2001/01/05}{added}
% \changes{v2.7g}{2001/04/17}{missing \cs{long} added}
% In the code above we use \cs{if@at@twocolumn} to detect, if we already
% inside the optional argument of \cs{twocolumn}. A clean definition of this
% feature would redefine \LaTeX{} command \cs{twocolumn}. Here we use the ugly
% hack to redefine the internal macro \cs{@topnewpage} of the \LaTeX{} kernel
% instead. This macro is used to print the optional argument. So we can add
% switching on and of the boolean switch before and after it. With this,
% packages can simply use chapter commands also inside the optional argument
% of \cs{twocolumn} instead of need to use an internal command like
% \cs{@makechapterhead}.
%    \begin{macrocode}
\newif\if@at@twocolumn
\newcommand*\scr@topnewpage{}
\let\scr@topnewpage\@topnewpage
\long\def\@topnewpage[#1]{%
  \@at@twocolumntrue\scr@topnewpage[{#1}]\@at@twocolumnfalse
}
%    \end{macrocode}
% \end{macro}^^A \@topnewpage
% \end{macro}^^A \scr@topnewpage
% \end{macro}^^A \if@at@twocolumn
%
% \begin{macro}{\scr@makechapterhead}
% \changes{v2.7b}{2001/01/05}{support for chapter preamble}
% \changes{v2.8p}{2001/09/25}{support for different kinds of preambles}
% \changes{v3.15}{2014/12/10}{\cs{nobreak} removed}
% \changes{v3.18}{2015/05/22}{added}
% \begin{macro}{\scr@@makechapterhead}
% \changes{v3.18}{2015/05/22}{added}
% \changes{v2.8q}{2002/04/18}{\cs{raggedsection} also for the prefix}
% \changes{v2.9p}{2003/06/28}{setting \cs{parfillskip} to \cs{fill}}
% \changes{v2.96a}{2006/12/02}{distance after the prefix line only if prefix line}
% \changes{v2.97b}{2007/03/09}{\cs{endgraf} fixed}
% \changes{v3.15}{2014/11/20}{re-implementation}
% \changes{v3.15}{2014/12/09}{\cs{raggedchapter} replaced by \cs{raggedsection}}
% \changes{v3.17}{2015/04/03}{using \cs{usefontofkomafont} for initial font
%   changed}
% Macros
% \begin{quote}\raggedright
%   \cs{scr@makechapterhead}\marg{chapter command name}\marg{heading}\\
%   \cs{scr@@makechapterhead}\marg{chapter command name}\marg{heading}
% \end{quote}
% are used inside the definition of style \texttt{chapter} and replace
% \cs{@makechapterhead} resp. \cs{@@makechapterhead} there. So they have one
% more argument than \cs{@makechapterhead} of the standard classes.
%    \begin{macrocode}
\newcommand*{\scr@makechapterhead}[2]{%
  \use@chapter@o@preamble{#1}%
  \@nameuse{@@make#1head}{#2}%
  \use@preamble{#1@u}%
}
\newcommand*{\scr@@makechapterhead}[2]{%
%    \end{macrocode}
% \changes{v3.22}{2016/12/21}{\cs{@afterindentrue} if \opt{beforeskip} is
%   positive}
% \changes{v3.26}{2018/09/18}{using \opt{afterindent}}
% Depending on \opt{afterindent} we set \cs{if@afterindent} and set the
% initial vertical skip using \cs{@tempskipa} and \cs{chapterheadstartvskip}.
%    \begin{macrocode}
  \@tempskipa=\glueexpr \csname scr@#1@beforeskip\endcsname\relax\relax
  \csname scr@#1@afterindent\endcsname
  {\@afterindenttrue}{\@afterindentfalse}{%
    \ifdim\@tempskipa<\z@\@tempskipa-\@tempskipa\else
%<!v4>      \expandafter\ifnum\scr@v@is@ge{3.22}
        \@afterindenttrue
%<!v4>      \fi
    \fi
  }%
  \chapterheadstartvskip
%    \end{macrocode}
% \changes{v3.19}{2015/08/02}{using \cs{chapterlinesformat} and
%   \cs{chapterlineswithprefixformat}}
% \changes{v3.19}{2015/08/02}{using font element \fnt{disposition} instead of
%   \cs{sectfont}}
% \changes{v3.19a}{2015/10/03}{missing local definition of \cs{IfUseNumber}
%   added}
% \changes{v3.20}{2015/12/23}{setting \cs{parfillskip} to \texttt{\cs{z@} plus
%   1fil}}
% \changes{v3.27}{2019/02/02}{setting of \cs{parindent} and \cs{parfillskip}
%   moved}
% \changes{v3.27}{2019/02/02}{\cs{ExecuteDoHook} added}
% \changes{v3.28}{2019/11/19}{\cs{ifnumbered} renamed to
%   \cs{Ifnumbered}}
% For the heading itself a group is used. Using group braces instead of
% \cs{begingroup} makes it possible to do a very ugly hack inside a
% redefinition of \cs{chapterheadstartvskip}. Nevertheless, such hacks are not
% recommended.
%    \begin{macrocode}
  {%
    \def\IfUseNumber{\Ifnumbered{#1}}%
%<book>    \if@mainmatter\else\let\IfUseNumber\@secondoftwo\fi
    \ExecuteDoHook{heading/begingroup/#1}%  
    \if@chapterprefix
      \let\IfUsePrefixLine\@firstoftwo
    \else
      \let\IfUsePrefixLine\@secondoftwo
    \fi
    \setlength{\parindent}{\z@}\setlength{\parfillskip}{\z@ plus 1fil}%
    \normalfont\usekomafont{disposition}{%
      \usekomafont{#1}{%
        \settoheight{\@tempskipa}{%
          {\usekomafont{#1prefix}{%
              \vrule \@width\z@ \@height\csname scr@#1@innerskip\endcsname}}%
        }%
        \raggedchapter
        \IfUseNumber{%
          \IfUsePrefixLine{%
            \chapterlineswithprefixformat{#1}%
            {{\usekomafont{#1prefix}{\csname #1format\endcsname%
                  \setlength{\@tempskipa}{\csname scr@#1@innerskip\endcsname}%
                  \chapterheadmidvskip}}}%
            {\interlinepenalty \@M#2\@@par}%
          }{%
            \chapterlinesformat{#1}%
            {\csname #1format\endcsname}%
            {\interlinepenalty \@M#2\@@par}%
          }%
        }{%
          \IfUsePrefixLine{%
            \chapterlineswithprefixformat{#1}%
            {}%
            {\interlinepenalty \@M#2\@@par}%
          }{%
            \chapterlinesformat{#1}%
            {}%
            {\interlinepenalty \@M#2\@@par}%
          }%
        }%
      }%
    }%
    \ExecuteDoHook{heading/endgroup/#1}%
  }%
  \nobreak\par\nobreak
%    \end{macrocode}
% \changes{v3.26}{2018/09/20}{no absolute value}
% Last but not least the final skip.
% \begin{description}
% \item[Note:] \cs{@afterheading} will be added by \cs{scr@@startchapter}.
% \end{description}
%    \begin{macrocode}
  \@tempskipa=\glueexpr \csname scr@#1@afterskip\endcsname\relax\relax
  \chapterheadendvskip
}
%    \end{macrocode}
% \end{macro}^^A \scr@makechapterhead
% \end{macro}^^A \scr@@makechapterhead
%
% \begin{macro}{\scr@makeschapterhead}
% \changes{v3.18}{2015/05/22}{added}
% \begin{macro}{\scr@@makeschapterhead}
% \changes{v2.9p}{2003/06/28}{to \cs{parfillskip} to \cs{fill}}
% \changes{v2.9q}{2004/03/24}{same braces like \cs{@@makechapterhead}}
% \changes{v3.15}{2014/12/09}{\cs{raggedchapter} replaced by
%  \cs{raggedsection}}
% \changes{v3.18}{2015/05/22}{added}
% \changes{v3.19}{2015/08/02}{same changes like \cs{scr@@makechapterhead}}
% \changes{v3.20}{2015/12/23}{setting \cs{parfillskip} to \texttt{\cs{z@} plus
%   1fil}}
% \changes{v3.25}{2017/09/07}{\cs{IfUseNumber} fixed}
% \changes{v3.26}{2018/09/20}{no absolute of \opt{afterskip}}
% Macros
% \begin{quote}\raggedright
%   \cs{scr@makeschapterhead}\marg{chapter command name}\marg{heading}\\
%   \cs{scr@@makeschapterhead}\marg{chapter command name}\marg{heading}
% \end{quote}
% are used inside the definition of style \texttt{chapter} and replace
% \cs{@makeschapterhead} resp. \cs{@@makeschapterhead} there. So they have one
% more argument than \cs{@makeschapterhead} of the standard classes.
%    \begin{macrocode}
\newcommand*{\scr@makeschapterhead}[2]{%
  \use@chapter@o@preamble{#1}%
  \@nameuse{@@makes#1head}{#2}%
  \use@preamble{#1@u}%
}
\newcommand*{\scr@@makeschapterhead}[2]{%
%    \end{macrocode}
% \changes{v3.22}{2016/12/21}{\cs{@afterindentrue} if positive
%   \opt{beforeskip}}
% \changes{v3.26}{2018/09/18}{supporting \opt{afterindent}}
% This is a simplified version of \cs{scr@@makechapterhead}.
%    \begin{macrocode}
  \@tempskipa=\glueexpr \csname scr@#1@beforeskip\endcsname\relax\relax
  \csname scr@#1@afterindent\endcsname
  {\@afterindenttrue}{\@afterindentfalse}{%
    \ifdim\@tempskipa<\z@\@tempskipa-\@tempskipa\else
%<!v4>      \expandafter\ifnum\scr@v@is@ge{3.22}
        \@afterindenttrue
%<!v4>      \fi
    \fi
  }%
  \chapterheadstartvskip
  {%
    \let\IfUseNumber\@secondoftwo
    \ExecuteDoHook{heading/begingroup/#1}%  
    \if@chapterprefix
      \let\IfUsePrefixLine\@firstoftwo
    \else
      \let\IfUsePrefixLine\@secondoftwo
    \fi
    \setlength{\parindent}{\z@}\setlength{\parfillskip}{\z@ plus 1fil}%
    \normalfont\usekomafont{disposition}{%
      \usekomafont{#1}{%
        \raggedchapter
        \IfUsePrefixLine{%
          \chapterlineswithprefixformat{#1}%
          {}%
          {\interlinepenalty \@M#2\@@par}%
        }{%
          \chapterlinesformat{#1}%
          {}%
          {\interlinepenalty \@M#2\@@par}%
        }%
      }%
    }%
    \ExecuteDoHook{heading/endgroup/#1}%
  }%
  \nobreak\par\nobreak
  \@tempskipa=\glueexpr \csname scr@#1@afterskip\endcsname\relax\relax
  \chapterheadendvskip
}
%    \end{macrocode}
% \end{macro}^^A \scr@@makeschapterhead
% \end{macro}^^A \scr@makeschapterhead
%
% \begin{command}{\chapterlinesformat,\chapterlineswithprefixformat}
% \changes{v3.19}{2015/08/02}{added}
% Commands
% \begin{quote}\raggedright
%   \cs{chapterlinesformat}\marg{command name}\marg{number}\marg{text}\\
%   \cs{chapterlineswithprefixformat}\marg{command name}\marg{number}\marg{text}
% \end{quote}
% are used to print a chapter heading either a free standing one or a free
% stranding one with prefix line. \meta{number} is the completely formatted
% prefix line inclusive the vertical distance below. The \meta{text} is the
% text of the heading also formatted and with final \cs{@@par}. But if the
% user used more paragraphs than this is final one, the user is responsible to
% prevent page breaks!
%    \begin{macrocode}
\newcommand{\chapterlinesformat}[3]{%
  \@hangfrom{#2}{#3}%
}
\newcommand{\chapterlineswithprefixformat}[3]{%
  #2#3%
}
%    \end{macrocode}
% \end{command}^^A \chapterlinesformat,\chapterlineswithprefixformat
%
% \begin{macro}{\use@chapter@o@preamble}
% \changes{v3.15}{2014/12/08}{distances adapted}
% \changes{v3.15a}{2015/02/02}{avoiding \texttt{overfull \cs{vbox}}}
% \changes{v3.18}{2015/05/20}{using command depending distances and warnings}
% \changes{v3.18}{2015/05/22}{has an argument and prints the preamble of the
%   command instead of the style}
% Used by \cs{scr@makechapterhead} and \cs{scr@makeschapterhead} to set the
% preamble above chapter headings.
%    \begin{macrocode}
\newcommand*{\use@chapter@o@preamble}[1]{%
  {%
    \settoheight{\@tempdima}{%
      \@tempskipa=\glueexpr \csname scr@#1@beforeskip\endcsname\relax\relax
      \ifdim\@tempskipa<\z@\@tempskipa-\@tempskipa\fi
      \vbox{\chapterheadstartvskip}%
    }%
    \settodepth{\@tempdimb}{%
      \@tempskipa=\glueexpr \csname scr@#1@beforeskip\endcsname\relax\relax
      \ifdim\@tempskipa<\z@\@tempskipa-\@tempskipa\fi
      \vbox{\chapterheadstartvskip}%
    }%
    \addtolength{\@tempdima}{\@tempdimb}%
    \setbox\z@\vbox{%
      \use@preamble{#1@o}%
    }%
    \setlength{\@tempdimb}{\ht0}%
    \addtolength{\@tempdimb}{\dp0}%
    \vbox to \z@{%
      \vskip-\baselineskip
      \vbox to \@tempdima{%
        \vfill
        \box\z@
      }%
      \vss
    }\vskip-\parskip\vskip-\baselineskip
    \ifdim \@tempdimb>\@tempdima%
      \addtolength{\@tempdimb}{-\@tempdima}%
      \ifdim\@tempdimb<\vfuzz
        \ClassInfo{\KOMAClassName}{%
          preamble before #1 is \the\@tempdimb\space to high.\MessageBreak
          Tolerated without warning because of\MessageBreak
          \string\vfuzz\space = \the\vfuzz\space >= \the\@tempdimb
        }%
      \else
      \ClassWarning{\KOMAClassName}{%
        preamble before #1 is \the\@tempdimb\space to high.\MessageBreak
        To avoid the overfull \string\vbox\space you may
        change\MessageBreak
        the `beforeskip' value using
        \string\RedeclareSectionCommand\MessageBreak
        at the preamble of your document.\MessageBreak
        You may also change \string\setchapterpreamble\MessageBreak 
        before the command, which generates the message
        about\MessageBreak
        an overfull \string\vbox
      }%
      \fi
    \fi%
  }%
}
%    \end{macrocode}
% \end{macro}^^A \use@chapter@o@preamble
% \end{macro}^^A \scr@startchapter
%
% \begin{command}{\addchaptertocentry}
% \changes{v3.08}{2010/11/01}{added}
% \changes{v3.12}{2013/09/24}{support for \cs{if@chaptertolists}}
% \changes{v3.12}{2013/09/24}{using\cs{addxcontentsline}}
% \changes{v3.28}{2019/11/19}{\cs{iftocfeature} replaced by
%   \cs{Iftocfeature}}
% \changes{v3.43}{2024/10/24}{\cs{float@addtolists} warning removed}
% We do not use \cs{addcontentsline} directly for the entries to the table of
% contents. Using
% \begin{quote}
%   \cs{addchaptertocentry}\marg{number}\marg{text}
% \end{quote}
% allows easy manipulation of such entries and additional actions. If the
% \meta{number} is completely empty no number should be used. Internally the
% generic \cs{addtocentrydefault} it used, which uses \cs{nonumberline}
% instead of \cs{numberline} in this case. See \file{scrkernel-listsof.dtx}
% for more information.
% \begin{description}
% \item[ToDo:] Here we also do the entries to other directories. But we have
%   also code for this in \cs{scr@@startchapter} at least for the vertical
%   space. Shouldn't we combine both places to one place? And shouldn't this
%   be done here only, because of handling of user calls of
%   \cs{addchaptertocentry}?
% \end{description}
%    \begin{macrocode}
\newcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{#1}{#2}%
  \if@chaptertolists
    \doforeachtocfile{%
      \Iftocfeature{\@currext}{chapteratlist}{%
        \addxcontentsline{\@currext}{chapteratlist}[{#1}]{#2}%
      }{}%
    }%
  \fi
}
%    \end{macrocode}
% \end{command}^^A \addchaptertocentry
%
% \begin{macro}{\@chapter,\@makechapterhead,\@@makechapterhead,
%               \@schapter,\@makeschapterhead,\@@makeschapterhead}
% \changes{v3.18}{2015/05/22}{indirect by \cs{DeclareSectionCommand}}
% \begin{command}{\setchapterpreamble}
% \changes{v2.7b}{2001/01/05}{added}
% \changes{v3.18}{2015/05/22}{indirect by \cs{DeclareSectionCommand}}
% \begin{macro}{\chapter@u@preamble,\chapter@o@preamble}
% \changes{v2.8p}{2001/09/25}{added}
% \changes{v3.18}{2015/05/22}{indirect by \cs{DeclareSectionCommand}}
% All these commands and macros are not defined explicitly but by using
% \cs{DeclareSectionCommand} later. See also
% \cs{scr@dsc@def@style@chapter@command} and \cs{setchapterpreamble}.
% \end{macro}^^A \chapter@u@preamble,\chapter@o@preamble
% \end{command}^^A \setchapterpreamble
% \end{macro}^^A \@chapter … \@@makeschapterhead
%
% \begin{command}{\addchap}
% \changes{v2.0e}{1994/08/10}{\cs{sectdef} replaced by h \cs{secdef}}
% \changes{v2.0e}{1994/08/10}{\cs{addcontensline} replaced by
%   \cs{addcontentsline}}
% \changes{v2.0e}{1994/08/10}{\cs{@makechapterhead} replaced by
%   \cs{@makeschapterhead}}
% \changes{v2.0e}{1994/08/10}{\cs{afterheadings} replaced by
%   \cs{@afterheading}}
% \changes{v2.1a}{1994/10/29}{argument of \cs{chaptermark} removed from
%   \cls*{scrbook}}
% \changes{v2.0e}{1994/10/12}{several \cs{markboth} replaced by
%   \cs{sectionmark}}
% \changes{v2.2b}{1995/03/20}{several \cs{chaptermark} replaced by
%   \cs{@mkboth}}
% \changes{v2.8d}{2001/07/05}{using \cs{chapterpagestyle}}
% \changes{v3.08}{2010/11/01}{using \cs{addchaptertocentry}}
% \changes{v3.10}{2011/08/30}{extended optional argument}
% \changes{v3.13a}{2014/09/11}{using \cs{SecDef}}
% \changes{v3.18}{2015/05/22}{redefinition}
% \begin{macro}{\@addchap}
% \changes{v3.12}{2013/02/26}{using \cs{addchapmark}}
% \changes{v3.17}{2015/04/20}{expanding \cs{scr@ds@head} in \cs{addchapmark}}
% \changes{v3.18}{2015/05/22}{redefinition}
% \begin{macro}{\@saddchap}
% \changes{v3.12}{2013/02/26}{using \cs{addchapmark}}
% \changes{v3.18}{2015/05/22}{redefinition}
% Since \KOMAScript{} 3.18 using
% \begin{quote}
%   \cs{addchap}\oarg{head or ToC entry}\marg{heading}
% \end{quote}
% is very similar to
% |\chapter[nonumber=true,…]{…}| but also works if the extended optional
% argument is not activated. And
% \begin{quote}
%   \cs{addchap*}\marg{heading}
% \end{quote}
% is just a \cs{chapter*}\marg{heading} with additional |\addchapmark{}| to
% empty the running head.
% \begin{description}
% \item[Note:] Because of simply manipulating \cnt{secnumdepth} this should
%   also work with packages like \pkg{hyperref} as long as they support
%   chapters without numbering.
% \end{description}
%    \begin{macrocode}
\newcommand\addchap{%
  \SecDef\@addchap\@saddchap
}%
\newcommand*{\@addchap}{}%
\long\def\@addchap[#1]#2{%
  \edef\reserved@a{%
    \unexpanded{%
      \chapter[{#1}]{#2}%
      \c@secnumdepth=
    }\the\c@secnumdepth\relax
  }%
  \c@secnumdepth=\numexpr \chapternumdepth-1\relax
  \reserved@a
}
\newcommand{\@saddchap}[1]{%
  \chapter*{#1}%
  \addchapmark{}%
}%
%    \end{macrocode}
% \changes{v2.7e}{2001/04/16}{braces fixed}
% \changes{v2.7f}{2001/04/17}{braces fixed}
% \changes{v2.8q}{2001/11/17}{using \cs{AfterPackage} to test for
%   \pkg{hyperref} version}
% \changes{v3.27a}{2019/11/11}{warning fixed}
% \begin{description}
% \item[ToDo:] Unfortunately \pkg{hyperref} until version 6.71a has a big fat
%   bug. It redefines the internal \KOMAScript{} macro \cs{@addchap} without
%   testing the definition.  From version 6.71b this is done only, if a very
%   old \KOMAScript{} is used. However, I would prefer removing all such code
%   from \pkg{hyperref}. Currently we warn, if such an old \pkg{hyperref} is
%   used. But this will be removed in future.
% \end{description}
%    \begin{macrocode}
%<*book>
\AfterPackage{hyperref}{%
  \@ifpackagelater{hyperref}{2001/02/19}{}{%
    \ClassWarningNoLine{\KOMAClassName}{%
      You are using an old version of the hyperref package!\MessageBreak%
      This version has a buggy hack in many drivers,\MessageBreak%
      causing \string\addchap\space to behave strangely.\MessageBreak%
      Please update hyperref to at least version 6.71b%
    }%
  }%
}
%</book>
%</body>
%    \end{macrocode}
% \end{macro}^^A \@saddchap
% \end{macro}^^A \@addchap
% \end{command}^^A \addchap
%
%
% \begin{macro}{\l@chapter}
% \changes{v2.9k}{2003/01/02}{supporting option \opt{tocleft}}
% \changes{v2.96b}{2006/11/30}{prevent page break between chapter entry and
%   parent}
% \changes{v2.96b}{2006/11/30}{prevent page break between chapter entry and
%   child}
% \changes{v2.97c}{2007/06/21}{\cs{sectfont} replaced by font element
%   \fnt{chapterentry}}
% \changes{v2.97c}{2007/06/21}{using font element
%   \fnt{chapterentrypagenumber}}
% \changes{v2.97c}{2007/06/21}{re-implemented to support left aligned entries}
% \changes{v3.15}{2014/12/22}{using \cs{chaptertocdepth} and
%   \cs{scr@chapter@tocnumwidth}}
% \changes{v3.15}{2014/12/10}{initial skip with \cs{addvspace} instead of
%   \cs{vskip}}
% \changes{v3.20}{2015/10/06}{re-implemented using ToC styles}
% \begin{macro}{\scr@chapter@tocindent}
% \changes{v3.15}{2014/12/02}{added}
% \changes{v3.18}{2015/05/20}{indirect definition by \cs{DeclareSectionCommand}}
% \changes{v3.20}{2015/11/06}{really used}
% \end{macro}^^A \scr@chapter@tocindent
% \begin{macro}{\scr@chapter@tocnumwidth}
% \changes{v3.15}{2014/12/02}{added}
% \changes{v3.18}{2015/05/20}{indirect definition by \cs{DeclareSectionCommand}}
% \end{macro}
% \begin{option}{chapterentrydots}
% \changes{v3.15}{2014/12/10}{added}
% Option
% \begin{quote}
%   \opt{chapterentrydots=\meta{boolean}}
% \end{quote}
% is the main difference of ToC entry style \texttt{chapter} to ToC entry
% style \texttt{tocline}. It activates the dots between the entry text and the
% page number.
% \begin{description}
% \item[ToDo:] This should be moved to \file{scrkernel-tocstyle.dtx}.
% \end{description}
%    \begin{macrocode}
%<*option>
\KOMA@ifkey{chapterentrydots}{@chapterentrywithdots}
%</option>
%    \end{macrocode}
% \end{option}^^A chapterentrydots
% \begin{command}{\raggedchapterentry}
% \changes{v2.97c}{2007/06/21}{added}
% \changes{v3.20}{2016/02/23}{removed}
% \changes{v3.21}{2016/06/06}{see \file{scrkernel-tocstyle.dtx},
%   style \texttt{tocline}, option \opt{raggedentrytext}}
% The initial setting of option \opt{raggedtext} of chapter ToC entry style
% \texttt{tocline} resp. \texttt{chapter}.
% \begin{description}
% \item[ToDo:] Do we really need this definition? Every definition but
%   \cs{raggedright} results in justified text. So not defining it would be
%   almost the same. It just would not reserve the macro. But this could be
%   done in \file{scrkernel-tocstyle.dtx}.
% \end{description}
%    \begin{macrocode}
%<*body>
\newcommand*{\raggedchapterentry}{}
%    \end{macrocode}   
% \end{command}^^A \raggedchapterentry
% \end{macro}^^A \l@chapter
%
% \begin{macro}{\l@chapteratlist}
% \changes{v2.96a}{2006/12/03}{added}
% This is used for chapter entries to other directories by the table of
% contents. By default it uses \cs{l@chapter}. But users can redefine it
% using, i.e., \cs{DeclareTOCStyleEntry}. However in this case they have to
% setup all the attributes/options on their own.
%    \begin{macrocode}
\newcommand*{\l@chapteratlist}{\l@chapter}
%    \end{macrocode}   
% \end{macro}^^A \l@chapteratlist
%
% \begin{counter}{chapter}
% \begin{command}{\thechapter}
% \begin{command}{\chapterformat}
% \changes{v2.3c}{1995/08/06}{auto dot feature}
% \changes{v2.7}{2000/01/03}{white space replaced by \cs{enskip}}
% \changes{v2.8}{2001/06/15}{\cs{chapappifprefix} added}
% \changes{v2.8o}{2001/09/19}{\cs{chapappifprefix} replaced by
%   \cs{chapappifchapterprefix}}
% \changes{v2.96a}{2006/12/02}{\cs{mbox} added}
% \changes{v3.17}{2015/03/08}{\cs{enskip} not in prefix line mode}
% \begin{command}{\chaptermarkformat}
% \changes{v2.3a}{1995/07/08}{space after chapter number increased}
% \changes{v2.3c}{1995/08/06}{auto dot feature}
% \changes{v2.8}{2001/06/15}{\cs{chapappifprefix} added}
% \changes{v2.8o}{2001/09/19}{\cs{chapappifprefix} replaced by
%   \cs{chapappifchapterprefix}}
% \changes{v3.03a}{2009/04/03}{tilde replaced by \cs{nobreakspace} for better
%   \pkg{babel-spanish} support}
% The counter would also be defined by \cs{DeclareSectionCommand}. But because
% of handling \cs{frontmatter} and \cs{backmatter} and prefix lines we cannot
% use the default definition of the formatting. So we define it already before
% calling \cs{DeclareSectionCommand} for \cs{chapter}.
%    \begin{macrocode}
\newcounter{chapter}
\renewcommand*{\thechapter}{\@arabic\c@chapter}
\newcommand*{\chapterformat}{%
  \mbox{\chapappifchapterprefix{\nobreakspace}\thechapter\autodot
    \IfUsePrefixLine{}{\enskip}}%
}
\newcommand*\chaptermarkformat{\chapappifchapterprefix{\ }%
  \thechapter\autodot\enskip}
%    \end{macrocode}
% \end{command}^^A \chaptermarkformat
% \end{command}^^A \chapterformat
% \end{command}^^A \thechapter
% \end{counter}^^A chapter
%
%
% \begin{command}{\chaptername}
% \changes{v3.36}{2022/03/15}{explicit definition for English languages}
% The name of a chapter, e.g., used for the prefix line and maybe for the ToC
% entry.
%    \begin{macrocode}
\newcommand*\chaptername{Chapter}
\providecaptionname{american,australian,british,canadian,english,newzealand,%
  UKenglish,ukenglish,USenglish,usenglish}\chaptername{Chapter}
%    \end{macrocode}
% \end{command}^^A \chaptername
%
%
% \begin{command}{\appendixname}
% \changes{v3.36}{2022/03/15}{explicit definition for English languages}
% The same for appendix chapters.
%    \begin{macrocode}
\newcommand*\appendixname{Appendix}
\providecaptionname{american,australian,british,canadian,english,newzealand,%
  UKenglish,ukenglish,USenglish,usenglish}\appendixname{Appendix}
%    \end{macrocode}
% \end{command}^^A \appendixname
%
%
% \begin{command}{\chapappifprefix}
% \changes{v2.8}{2001/06/15}{added}
% \changes{v2.8o}{2001/09/19}{deprecated}
% \changes{v3.36}{2022/03/18}{removed}
% \begin{description}
% \item[Note:] Command \cs{chapappifprefix} has been removed in \KOMAScript{}
% v3.36, because it is deprecated from \KOMAScript{} v2.8o and there never has
% been a compatibility switch for versions before \KOMAScript{} v2.9.
% \end{description}
% \end{command}^^A \chapappifprefix
%
%
% \begin{command}{\chapappifchapterprefix}
% \changes{v2.8o}{2001/09/19}{added}
% \changes{v3.18}{2015/06/09}{using \cs{IfChapterUsePrefixLine}}
% \begin{command}{\IfChapterUsesPrefixLine}
% \changes{v3.18}{2015/06/09}{added}
% \begin{command}{\chapapp}
% \changes{v2.8}{2001/06/15}{added}
% \begin{macro}{\@chapapp}
% The \LaTeX{} standard classes provide an internal macro \cs{@chapapp} that
% is either \cs{chaptername} or \cs{appendixname} depending on whether we are
% inside or outside the appendix. For compatibility with lots of packages
% \KOMAScript{} does provide this too. But for the user level this is
% cumbersome. So \KOMAScript{} does also provide a \emph{read only specified}
% user command \cs{chapapp}. \KOMAScript{} itself uses \cs{chapapp} inside
% \begin{quote}
%   \cs{chapappifchapterprefix}\marg{then string}
% \end{quote}
% that outputs \cs{then string} prefixed by \cs{chapapp} only, if
% \opt{chapterprefix} (resp. \opt{headings=twolinechapter}) has been activated
% and therefore a chapter prefix line has to be used. A more general version
% \cs{chapterifchapterprefix} is
% \begin{quote}
%   \cs{IfChapterUsesPrefixLine}\marg{then code}\marg{else code}
% \end{quote}
% that executes \cs{then code} if a chapter prefix line has to be used and
% \cs{else code} otherwise (both without extra prefix).
%    \begin{macrocode}
\newcommand*{\chapappifchapterprefix}[1]{%
  \IfChapterUsesPrefixLine{\chapapp#1}{}%
}
\newcommand*{\IfChapterUsesPrefixLine}{%
  \if@chapterprefix\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}
\newcommand*{\chapapp}{\@chapapp}
\newcommand*\@chapapp{\chaptername}
%    \end{macrocode}
% \end{macro}^^A \@chapapp
% \end{command}^^A \chapapp
% \end{command}^^A \IfChapterUsePrefixLine
% \end{command}^^A \chapappifchapterprefix
%
%
% \begin{command}{\chaptermark}
% Before selecting a page style with automatic running head, \cs{chapter}
% should simply not change the left or right mark.
% \begin{description}
% \item[ToDo:] Do we really need this or would the later
%   \cs{DeclareSectionCommand} be enough to do the initial definition?
% \end{description}
%    \begin{macrocode}
\newcommand*\chaptermark[1]{}
%    \end{macrocode}
% \end{command}
%
% \begin{command}{\addchapmark}
% \changes{v3.12}{2013/03/26}{added}
% \cs{addchap} and marks for running heads are a little bit more
% complicated. Currently we are using a local redefinition of
% \cs{if@mainmatter} and setting of \cnt{secnumdept} to switch of numbering
% and then using \cs{chaptermark}. So the \cs{chaptermark} definition should
% always respect at least one of these settings.
% \begin{description}
% \item[Note:] The ToDo note at \cs{chaptermark} is not relevant for this
%   definition.
% \end{description}
%    \begin{macrocode}
\newcommand*\addchapmark[1]{%
  \begingroup
    \expandafter\let\csname if@mainmatter\expandafter\endcsname
    \csname iffalse\endcsname
    \c@secnumdepth=\numexpr \chapternumdepth-1\relax
    \chaptermark{#1}%
  \endgroup
}
%    \end{macrocode}
% \end{command}^^A \addchapmark
%
%
% \begin{command}{\raggedchapter}
% \changes{v3.15}{2014/12/09}{added}
% Style \texttt{chapter} does not use \cs{raggedsection} but
% \cs{raggedchapter}. However, the initial value is \cs{raggedsection}, so a
% change of \cs{raggedsection} would also change the initial behave of
% \cs{raggedchapter}. Nevertheless the alignment of chapters in independent
% from the alignment of sections, if wanted.
%    \begin{macrocode}
\newcommand*{\raggedchapter}{\raggedsection}
%    \end{macrocode}
% \end{command}
%
%
% \begin{fontelement}{chapterentry}
% \changes{v2.97c}{2007/06/21}{added}
% \changes{v3.06}{2010/06/09}{font element \fnt{sectioning} replaced by
%   \fnt{disposition}}
% \cs{chapter} entries to the table of contents initially using
% \fnt{disposition}, the basic font of all outlines.
%    \begin{macrocode}
\newkomafont{chapterentry}{\usekomafont{disposition}}
%    \end{macrocode}
% \end{fontelement}
%
% \begin{fontelement}{chapterentrypagenumber}
% \changes{v2.97c}{2007/06/21}{added}
% The page number of \cs{chapter} entries to the table of contents is
% initially the same as the entry at whole.
%    \begin{macrocode}
\newkomafont{chapterentrypagenumber}{}
%    \end{macrocode}
% \end{fontelement}
%
% \begin{fontelement}{chapterentrydots}
% \changes{v3.15}{2014/12/10}{added}
% \changes{v3.27}{2019/10/28}{\cs{normalfont} removed}
% If the optional dots for \cs{chapter} entries to the table of contents are
% used, we can change the font of the dots also. It is used for the
% \texttt{linefill} of \pkg*{tocbasic} ToC entry style \texttt{chapter}, that
% uses ToC entry style \texttt{tocline} where the line filler is always
% prefixed by \cs{normalfont}\cs{normalsize}. So the setting is relative to
% this. The color is relative to the color of \fnt{chapterentry}.
%    \begin{macrocode}
\newkomafont{chapterentrydots}{}
%</body>
%</book|report>
%    \end{macrocode}
% \end{fontelement}
%
%
% \subsection{Section, subsections and more}
%
% The outline levels of sections, subsections, subsubsection, paragraphs and
% subparagraphs are all very similar. There are only some special treatments
% of sections with \cls*{scrartcl}, because there \cs{section} is a high-level
% command.
%
% \begin{fontelement}{section,subsection,subsubsection,
%                     paragraph,subparagraph}
% \changes{v2.8o}{2001/09/14}{added}
% Definition of the font elements used by \cs{section}, \cs{subsection},
% \cs{subsubsection}, \cs{paragraph} and \cs{subparagraph}. The settings are
% done by option \texttt{headings} later. Everything is prepared before this
% option is used the first time. The definition of the font element is done
% manually to stay compatible with old \KOMAScript{} versions using another
% internal macro than the default of \cs{newkomafont}.
%    \begin{macrocode}
%<*prepare>
\newcommand*{\size@section}{}
\newcommand*{\scr@fnt@section}{\size@section}
\newcommand*{\size@subsection}{}
\newcommand*{\scr@fnt@subsection}{\size@subsection}
\newcommand*{\size@subsubsection}{}
\newcommand*{\scr@fnt@subsubsection}{\size@subsubsection}
\newcommand*{\size@paragraph}{}
\newcommand*{\scr@fnt@paragraph}{\size@paragraph}
\newcommand*{\size@subparagraph}{}
\newcommand*{\scr@fnt@subparagraph}{\size@subparagraph}
%</prepare>
%    \end{macrocode}
% \end{fontelement}^^A section … subparagraph
%
%
% \begin{macro}{\l@section}
% \changes{v2.9k}{2003/01/02}{supporting option \opt{tocleft}}
% \changes{v2.96b}{2006/11/30}{preventing page break between section entry and
%   parent}
% \changes{v2.96b}{2006/11/30}{preventing page break between section entry and
%   child}
% \changes{v2.97c}{2007/06/21}{\cs{sectfont} replaced by font element
%   \fnt{sectionentry}}
% \changes{v2.97c}{2007/06/21}{using font element
%   \fnt{sectionentrypagenumber}}
% \changes{v2.97c}{2007/06/21}{re-implemented to support left aligned entries}
% \changes{v3.15}{2014/11/24}{using \cs{sectiontocdepth},
%   \cs{scr@section@tocindent} and \cs{scr@section@tocnumwidth}}
% \changes{v3.20}{2015/10/06}{re-implemented using ToC styles}
% \begin{option}{sectionentrydots}
% \changes{v3.15}{2014/12/10}{added}
% With \cls*{scrartcl} option
% \begin{quote}
%   \opt{sectionentrydots=\meta{boolean}}
% \end{quote}
% is the main difference of ToC entry style \texttt{section} to ToC entry
% style \texttt{tocline}. It activates the dots between the entry text and the
% page number.
% \begin{description}
% \item[ToDo:] This should be moved to \file{scrkernel-tocstyle.dtx}.
% \end{description}
%    \begin{macrocode}
%<*article>
%<*option>
\KOMA@ifkey{sectionentrydots}{@sectionentrywithdots}
%</option>
%    \end{macrocode}
% \end{option}^^A sectionentrydots
% \begin{command}{\raggedsectionentry}
% \changes{v2.97c}{2007/06/21}{added}
% \changes{v3.20}{2016/02/23}{removed}
% The initial setting of option \opt{raggedtext} of section ToC entry style
% \texttt{tocline} resp. \texttt{section}.
% \begin{description}
% \item[ToDo:] Do we really need this definition? Every definition but
%   \cs{raggedright} results in justified text. So not defining it would be
%   almost the same. It just would not reserve the macro. But this could be
%   done in \file{scrkernel-tocstyle.dtx}.
% \end{description}
%    \begin{macrocode}
%<*body>
\newcommand*{\raggedsectionentry}{}
%</body>
%</article>
%    \end{macrocode}   
% \end{command}^^A \raggedsectionentry
% \end{macro}^^A \l@section
%
%
% \begin{command}{\addsec}
% \changes{v2.0e}{1994/10/12}{several \cs{markboth} replaced by
%   \cs{sectionmark}}
% \changes{v2.2b}{1995/03/20}{\cs{sectionmark} replaced by \cs{@mkboth}}
% \changes{v2.3h}{1995/01/21}{also available with \cls*{scrbook} and
%   \cls*{scrreprt}}
% \changes{v2.4g}{1996/11/04}{\cs{section*} moved because of page breaks in
%   the table of contents}
% \changes{v2.5d}{1998/01/03}{\cs{@mkboth} replaced by conditional
%   \cs{markright}}
% \changes{v2.7a}{2001/01/04}{supporting \pkg{hyperref}}
% \changes{v3.13a}{2014/09/11}{using \cs{SecDef}}
% \begin{macro}{\@addsec}
% \changes{v3.10}{2011/08/30}{using \cs{addsectiontocentry}}
% \changes{v3.10}{2011/08/30}{extended optional argument}
% \changes{v3.12}{2013/02/26}{using \cs{addsecmark}}
% \changes{v3.18}{2015/05/22}{redefinition}
% \begin{macro}{\@saddsec}
% \changes{v3.12}{2013/02/26}{using \cs{addsecmark}}
% \changes{v3.17}{2015/04/20}{expanding \cs{scr@ds@head} in \cs{addsecmark}}
% Since \KOMAScript{} 3.18 using
% \begin{quote}
%   \cs{addsec}\oarg{head or ToC entry}\marg{heading}
% \end{quote}
% is very similar to
% |\section[nonumber=true,…]{…}| but also works if the extended optional
% argument is not activated. And
% \begin{quote}
%   \cs{addsec*}\marg{heading}
% \end{quote}
% is just a \cs{section*}\marg{heading} with additional |\addsecmark{}| to
% empty the running head.
% \begin{description}
% \item[Note:] Because of simply manipulating \cnt{secnumdepth} this should
%   also work with packages like \pkg{hyperref} as long as they support
%   sections without numbering.
% \end{description}
%    \begin{macrocode}
%<*body>
\newcommand\addsec{\SecDef\@addsec\@saddsec}
\newcommand*{\@addsec}{}
\def\@addsec[#1]#2{%
  \edef\reserved@a{%
    \unexpanded{%
      \section[{#1}]{#2}%
      \c@secnumdepth=
    }\the\c@secnumdepth\relax
  }%
  \c@secnumdepth=\numexpr \sectionnumdepth-1\relax
  \reserved@a
}
\newcommand*{\@saddsec}[1]{%
  \section*{#1}\addsecmark{}%
}
%    \end{macrocode}
% \end{macro}^^A \@saddsec
% \end{macro}^^A \@addsec
% \end{command}^^A \addsec
%
%
% \begin{command}{\minisec}
% \changes{v2.8q}{2002/02/28}{\cs{nobreak} after \cs{sectfont} added}
% \changes{v2.9o}{2003/01/31}{\cs{nobreak} after group end}
% \changes{v2.96a}{2006/11/30}{initial value of \cs{parfillskip} depending on
%   \opt{version}}
% \changes{v2.96a}{2006/12/03}{not needed \cs{nobreak} removed}
% \changes{v3.13a}{2014/08/07}{handling of distances of immediate following
% headings like \cs{@startsection}}
% \changes{v3.26}{2018/05/14}{missing \cs{interlinepenalty}\cs{@M} added}
% \begin{fontelement}{minisec}
% \changes{v2.8o}{2001/09/14}{using font element \fnt{minisec}}
% \changes{v3.21}{2016/06/12}{missing \cs{nobreak} added}
% \cs{minisec} is a very special additional outline of \KOMAScript. It is
% always free standing but without numbering. Something like
% \cs{sectionlinesformat} is not supported currently. Also entries to the
% table of contents or marks for an automatic running head are not
% supported. However a dedicated font element is provided.
%    \begin{macrocode}
\newkomafont{minisec}{}
\newcommand\minisec[1]{%
%<!v4>  \expandafter\ifnum\scr@v@is@lt{3.13a}\relax
%<!v4>    \@afterindentfalse \vskip 1.5ex
%<!v4>  \else
    \if@noskipsec \leavevmode \fi
    \par
    \@afterindentfalse
    \if@nobreak
      \everypar{}%
    \else
      \addpenalty\@secpenalty\addvspace{1.5ex}%
    \fi
%<!v4>  \fi
  {\parindent \z@
%<!v4>    \expandafter\ifnum\scr@v@is@gt{2.96}\relax
      \setlength{\parfillskip}{\z@ plus 1fil}%
%<!v4>    \fi
    \raggedsection\normalfont\sectfont\nobreak
    \usekomafont{minisec}{\nobreak\interlinepenalty \@M #1\par\nobreak}%
  }\nobreak
  \@afterheading
}
%    \end{macrocode}
% \end{fontelement}^^A minisec
% \end{command}^^A \minisec
%
%
% \begin{command}{\addsectiontocentry,\addsubsectiontocentry,
%                 \addsubsubsectiontocentry,
%                 \addparagraphtocentry,\addsubparagraphtocentry}
% \changes{v3.10}{2011/08/30}{added}
% We do not use \cs{addcontentsline} directly for the entries to the table of
% contents. Using
% \begin{quote}\raggedright
%   \cs{addsectiontocentry}\marg{number}\marg{text}\\
%   \cs{addsubsectiontocentry}\marg{number}\marg{text}\\
%   \cs{addsubsubsectiontocentry}\marg{number}\marg{text}\\
%   \cs{addparagraphtocentry}\marg{number}\marg{text}\\
%   \cs{addsubparagraphtocentry}\marg{number}\marg{text}\\
% \end{quote}
% allows easy manipulation of such entries and additional actions. If the
% \meta{number} is completely empty no number should be used. Internally the
% generic \cs{addtocentrydefault} it used, which uses \cs{nonumberline}
% instead of \cs{numberline} in this case. See \file{scrkernel-listsof.dtx}
% for more information.
% \begin{description}
% \item[ToDo:] Should this be moved to \file{scrkernel-listsof.dtx} or even to
%   \file{scrkernel-tocstyle.dtx}?
% \item[ToDo:] Are these really needed or would the implicit definition by the
%   later \cs{DeclareSectionCommand} be enough as it is already used for
%   \cs{addsubsubsectiontocentry}?
% \end{description}
%    \begin{macrocode}
\newcommand*{\addsectiontocentry}[2]{%
  \addtocentrydefault{section}{#1}{#2}%
}
\newcommand*{\addsubsectiontocentry}[2]{%
  \addtocentrydefault{subsection}{#1}{#2}%
}
\newcommand*{\addparagraphtocentry}[2]{%
  \addtocentrydefault{paragraph}{#1}{#2}%
}
\newcommand*{\addsubparagraphtocentry}[2]{%
  \addtocentrydefault{subparagraph}{#1}{#2}%
}
%    \end{macrocode}
% \end{command}
%
%
% \begin{counter}{section}
% \begin{command}{\thesection}
% \changes{v2.97e}{2007/11/23}{chapter number prefix only in main matter}
% \changes{v3.03b}{2009/06/09}{\cs{relax} replaced by white space for better
%   support of \pkg{hyperref}}
% \changes{v3.27}{2019/06/26}{using \cs{scr@v@is@gt}}
% \begin{command}{\sectionmarkformat}
% \changes{v2.3a}{1995/07/08}{space after number increased}
% \changes{v2.3c}{1995/08/06}{auto dot feature}
% \begin{counter}{subsection}
% \begin{command}{\thesubsection}
% \begin{command}{\subsectionmarkformat}
% \changes{v2.3a}{1995/07/08}{space after number increased}
% \changes{v2.3c}{1995/08/06}{auto dot feature}
% \begin{counter}{subsubsection}
% \begin{command}{\thesubsubsection}
% \begin{counter}{paragraph}
% \begin{command}{\theparagraph}
% \begin{counter}{subparagraph}
% \begin{command}{\thesubparagraph}
% The counter would also be defined by \cs{DeclareSectionCommand}. But because
% of handling \cs{frontmatter} and \cs{backmatter} and prefix lines we cannot
% use the default definition of the formatting of \cnt{section}. So we define
% it already before calling \cs{DeclareSectionCommand} for \cs{section}.
% \begin{description}
% \item[ToDo:] Do we really need the explicit definition of \cnt{subsection},
%   \cnt{subsubsection}, \cnt{paragraph}, and \cnt{subparagraph} of would the
%   implicit definition by \cs{DeclareSectionCommand} be enough for these.
% \end{description}
%    \begin{macrocode}
%<*book|report>
\newcounter{section}[chapter]
\renewcommand*\thesection{%
%<*book>
%<!v4>  \expandafter\ifnum\scr@v@is@gt{2.97d}% 
    \if@mainmatter\thechapter.\fi
%<!v4>  \else
%</book>
  \thechapter.%
%<book&!v4>  \fi
  \@arabic\c@section
}
%</book|report>
%<*article>
\newcounter{section}
\renewcommand*{\thesection}{\@arabic\c@section}
%</article>
\newcommand*\sectionmarkformat{\thesection\autodot\enskip}
\newcounter{subsection}[section]
\renewcommand*{\thesubsection}{\thesection.\@arabic\c@subsection}
%<*article>
\newcommand*\subsectionmarkformat{\thesubsection\autodot\enskip}
%</article>
\newcounter{subsubsection}[subsection]
\renewcommand*{\thesubsubsection}{%
  \thesubsection.\@arabic\c@subsubsection
}
\newcounter{paragraph}[subsubsection]
\renewcommand*{\theparagraph}{\thesubsubsection.\@arabic\c@paragraph}
\newcounter{subparagraph}[paragraph]
\renewcommand*{\thesubparagraph}{%
  \theparagraph.\@arabic\c@subparagraph
}
%    \end{macrocode}
% \begin{macro}{\@seccntformat}
% \changes{v2.3c}{1995/08/06}{CJK support extended}
% \changes{v2.9p}{2004/01/07}{\cs{protect} added}
% \changes{v2.97}{2007/01/24}{workaround for the ugly \cs{protect}}
% \changes{v3.17}{2015/02/23}{using \cs{\dots format} if exists}
% \changes{v3.17}{2015/03/31}{using \cs{protect} for \cs{\dots format}}
% \begin{command}{\othersectionlevelsformat}
% \changes{v2.7}{2000/01/03}{added}
% \changes{v2.7}{2000/01/03}{\cs{quad} replaced by \cs{enskip}}
% \changes{v2.7i}{2001/05/17}{\cs{let} replaced by \cs{def}}
% \changes{v2.97}{2007/01/24}{three arguments instead of only one}
% With \KOMAScript~2 \cs{othersectionlevelsformat} has had only one argument,
% the \cs{…format} command of the level. But with there has been expansion
% issues. So we have had to change the definition to
% \begin{quote}
%   \cs{othersectionlevelsformat}\marg{level name}\marg{ignore
%   command}\marg{the command}
% \end{quote}
% Users who use or redefine \cs{othersectionlevelsformat} should always ignore
% the \meta{ignore command}. It is used to internally handle deprecated
% definitions of \cs{othersectionlevelsformat} with only one
% argument. \meta{level name} is the name of the level command, e.g.,
% |subsubsection|. \meta{the command} is the \cs{the…} command corresponding
% with \marg{level name}. See the definition of \cs{@seccntformat} for more
% information about the internal usage of \cs{othersectionlevelsformat}.
% \begin{description}
% \item[ToDo:] Can we somehow change this for \KOMAScript~4 to no longer
%   support the second argument (unless a low compatibility level is used)?
% \end{description}
%    \begin{macrocode}
\newcommand*{\othersectionlevelsformat}[3]{#3\autodot\enskip}
\renewcommand*{\@seccntformat}[1]{%
%<!v4>  \expandafter\ifnum\scr@v@is@lt{3.17}\relax
%<!v4>    \protect\othersectionlevelsformat{#1}{%
%<!v4>      \expandafter\aftergroup\noexpand\@gobble}{\csname the#1\endcsname}%
%<!v4>  \else
    \scr@ifundefinedorrelax{#1format}{%
      \protect\othersectionlevelsformat{#1}{%
        \expandafter\aftergroup\noexpand\@gobble}{\csname the#1\endcsname}%
    }{\expandafter\protect\csname #1format\endcsname}%
%<!v4>  \fi
}
%    \end{macrocode}
% \end{command}^^A \othersectonlevelsformat
% \end{macro}^^A \@seccntformat
% \end{command}^^A \thesubparagraph
% \end{counter}^^A subparagraph
% \end{command}^^A \theparagraph
% \end{counter}^^A paragraph
% \end{command}^^A \thesubsubsection
% \end{counter}^^A subsubsection
% \end{command}^^A \subsectionformat
% \end{command}^^A \thesubsection
% \end{counter}^^A subsection
% \end{command}^^A \sectionformat
% \end{command}^^A \thesection
% \end{counter}^^A section
%
%
% \begin{command}{\addsecmark}
% \changes{v3.12}{2013/03/26}{added}
% \cs{addsec} and marks for running heads are a little bit
% complicated. Currently we are using a local setting of \cnt{secnumdept} to
% switch of numbering and then using \cs{sectionmark}. So the \cs{sectionmark}
% definition should always respect \cnt{secnumdepth}.
%    \begin{macrocode}
\newcommand*\addsecmark[1]{%
  \begingroup
    \c@secnumdepth=\numexpr \sectionnumdepth-1\relax
    \sectionmark{#1}%
  \endgroup
}
%    \end{macrocode}
% \end{command}^^A \addsecmark
%
%
% \begin{fontelement}{sectionentry}
% \changes{v2.97c}{2007/06/21}{added}
% \changes{v3.06}{2010/06/09}{font element \fnt{sectioning} replaced by
%   \fnt{disposition}}
% With \cls*{scrartcl} \cs{section} entries to the table of contents initially
% using \fnt{disposition}, the basic font of all outlines.
%    \begin{macrocode}
%<*article>
\newkomafont{sectionentry}{\usekomafont{disposition}}
%    \end{macrocode}
% \end{fontelement}
%
% \begin{fontelement}{sectionentrypagenumber}
% \changes{v2.97c}{2007/06/21}{added}
% The page number of \cs{section} entries to the table of contents is
% initially the same as the entry at whole with \cls*{scrartcl}.
%    \begin{macrocode}
\newkomafont{sectionentrypagenumber}{}
%    \end{macrocode}
% \end{fontelement}
%
% \begin{fontelement}{sectionentrydots}
% \changes{v3.15}{2014/12/10}{added}
% \changes{v3.27}{2019/10/28}{\cs{normalfont} removed}
% If the optional dots for \cs{section} entries to the table of contents are
% used, we can change the font of the dots also. It is used for the
% \texttt{linefill} of \pkg*{tocbasic} ToC entry style \texttt{section}, that
% uses ToC entry style \texttt{tocline} where the line filler is always
% prefixed by \cs{normalfont}\cs{normalsize}. So the setting is relative to
% this. The color is relative to the color of \fnt{sectionentry}.
%    \begin{macrocode}
\newkomafont{sectionentrydots}{}
%</article>
%    \end{macrocode}
% \end{fontelement}
%
%
% \begin{command}{\partnumdepth,\chapternumdepth,
%                 \sectionnumdepth,\subsectionnumdepth,\subsubsectionnumdepth,
%                 \paragraphnumdepth,\subparagraphnumdepth,
%                 \parttocdepth,\chaptertocdepth
%                 \sectiontocdepth,\subsectiontocdepth,\subsubsectiontocdepth,
%                 \paragraphtocdepth,\subparagraphtocdepth}
% \changes{v3.12}{2013/12/16}{added}
% \changes{v3.15}{2014/12/09}{indirect definition by
%   \cs{DeclareSectionCommand}}
% The section numbering levels, to be compared with counter
% \cnt{secnumdepth}, and the table of contents entry levels, to be compared
% with counter \cs{tocdepth}, of all outline commands defined by the
% \KOMAScript{} classes.
% \begin{description}
% \item[Note:] These are specified to be \emph{read only}. They are defined by
%   \cs{DeclareSectionCommand}.
% \end{description}
% \end{command}^^A \partnumdepth … \subparagraphtocdepth
%
% \begin{command}{\part}
% \changes{v3.18}{2015/05/25}{indirect definition by \cs{DeclareSectionCommand}}
% \changes{v3.26}{2018/06/27}{definition moved behind \cs{section} because of
%   bookmark level workaround}
% \begin{command}{\chapter}
% \changes{v2.8d}{2001/07/05}{using \cs{chapterpagestyle}}
% \changes{v3.18}{2015/05/20}{indirect definition by \cs{DeclareSectionCommand}}
% \begin{command}{\section}
% \changes{v2.8p}{2001/09/22}{\cs{sectfont} before \cs{size@section}}
% \changes{v2.8q}{2002/02/28}{\cs{nobreak} after \cs{sectfont} added}
% \changes{v2.96a}{2006/11/30}{initial value of \cs{parfillskip} depending on
%   option \opt{version}}
% \changes{v2.96a}{2006/12/03}{not needed \cs{nobreak} removed}
% \changes{v3.13a}{2014/09/11}{using \cs{scr@startsection}}
% \changes{v3.15}{2014/11/24}{indirect definition by \cs{DeclareSectionCommand}}
% \begin{command}{\subsection}
% \changes{v2.8p}{2001/09/22}{\cs{sectfont} before \cs{size@subsection}}
% \changes{v2.8q}{2002/02/28}{\cs{nobreak} after \cs{sectfont} added}
% \changes{v2.96a}{2006/11/30}{initial value of \cs{parfillskip} depending on
%   option \opt{version}}
% \changes{v2.96a}{2006/12/03}{not needed \cs{nobreak} removed}
% \changes{v3.13a}{2014/09/11}{using \cs{scr@startsection}}
% \changes{v3.15}{2014/11/24}{indirect definition by \cs{DeclareSectionCommand}}
% \begin{command}{\subsubsection}
% \changes{v2.3d}{1995/08/19}{with \cls*{scrartcl} also numbered and added to
%   ToC}
% \changes{v2.8p}{2001/09/22}{\cs{sectfont} added before
%   \cs{size@subsubsection}}
% \changes{v2.8q}{2002/02/28}{\cs{nobreak} added after \cs{sectfont}}
% \changes{v2.96a}{2006/11/30}{initial value of \cs{parfillskip} depending on
%   option \opt{version}}
% \changes{v2.96a}{2006/12/03}{not needed \cs{nobreak} removed}
% \changes{v3.13a}{2014/09/11}{using \cs{scr@startsection}}
% \changes{v3.15}{2014/11/24}{indirect definition by \cs{DeclareSectionCommand}}
% \begin{command}{\paragraph}
% \changes{v2.8p}{2001/09/22}{\cs{sectfont} added before
%   \cs{size@paragraph}}
% \changes{v2.8q}{2002/02/28}{\cs{nobreak} added after \cs{sectfont}}
% \changes{v2.96a}{2006/12/03}{not needed \cs{nobreak} removed}
% \changes{v3.15}{2014/11/24}{indirect definition by \cs{DeclareSectionCommand}}
% \begin{command}{\subparagraph}
% \changes{v2.8p}{2001/09/22}{\cs{sectfont} added before
%   \cs{size@subparagraph}}
% \changes{v2.8q}{2002/02/28}{\cs{nobreak} added after \cs{sectfont}}
% \changes{v2.96a}{2006/12/03}{not needed \cs{nobreak} removed}
% \changes{v3.13a}{2014/09/11}{using \cs{scr@startsection}}
% \changes{v3.15}{2014/11/24}{indirect definition by \cs{DeclareSectionCommand}}
% \changes{v3.15}{2014/11/24}{\cs{parindent} replaced by \cs{scr@parindent}}
% Commands \cs{part}, \cs{chapter}, \cs{section}, \cs{subsection},
% \cs{subsubection}, \cs{paragraph} and \cs{subparagraph} are defined using
% \cs{DeclareSectionCommand}. The definition of \cs{part} must be \emph{after}
% \cs{section} because of the bookmark level workaround (see definition of
% \cs{DelcareSectionCommand}. The declaration of \cs{chapter} uses only those
% settings that do not depend on options, because settings by class options
% must no be overwritten here. So all settings that do depend on class options
% have to be initialized before the options (see the \texttt{prepare} code of
% such options above). For the lower levels this is only the case for the font.
%    \begin{macrocode}
%<*book|report>
\DeclareSectionCommand[%
  style=chapter,%
  level=\z@,%
  pagestyle=plain,%
  tocstyle=chapter,%
  tocindent=\z@,%
  tocnumwidth=1.5em%
]{chapter}
%</book|report>
\DeclareSectionCommand[%
  style=section,%
  level=1,%
  indent=\z@,%
  beforeskip=-3.5ex \@plus -1ex \@minus -.2ex,%
  afterskip=2.3ex \@plus.2ex,%
  tocstyle=section,%
%<book|report>  tocindentfollows=chapter,%
%<article>  tocindent=0pt,%
%<book|report>  tocindent=1.5em,%
%<article>  tocnumwidth=1.5em%
%<book|report>  tocnumwidth=2.3em%
]{section}
\DeclareSectionCommand[%
  style=part,%
%<article>  level=\z@,%
%<report|book>  level=\m@ne,%
%<report|book>  pagestyle=plain,%
  tocstyle=part,%
  toclevel=\m@ne,%
  tocindent=\z@,%
  tocnumwidth=2em%
]{part}
\DeclareSectionCommand[%
  style=section,%
  level=2,%
  indent=\z@,%
  beforeskip=-3.25ex\@plus -1ex \@minus -.2ex,%
  afterskip=1.5ex \@plus .2ex,%
%<article>  tocstyle=subsection,%
%<book|report>  tocstyle=section,%
%<article>  tocindent=1.5em,%
%<book|report>  tocindent=3.8em,%
%<article>  tocnumwidth=2.3em%
%<book|report>  tocnumwidth=3.2em%
]{subsection}
\DeclareSectionCommand[%
  style=section,%
  level=3,%
  indent=\z@,%
  beforeskip=-3.25ex\@plus -1ex \@minus -.2ex,%
  afterskip=1.5ex \@plus .2ex,%
%<article>  tocstyle=subsection,%
%<book|report>  tocstyle=section,%
%<article>  tocindent=3.8em,%
%<book|report>  tocindent=7.0em,%
%<article>  tocnumwidth=3.2em%
%<book|report>  tocnumwidth=4.1em%
]{subsubsection}
\DeclareSectionCommand[%
  style=section,%
  level=4,%
  indent=\z@,%
  beforeskip=3.25ex \@plus1ex \@minus.2ex,%
  afterskip=-1em,%
%<article>  tocstyle=subsection,%
  tocindentfollows=subsubsection,%
%<book|report>  tocstyle=section,%
%<article>  tocindent=7.0em,%
%<book|report>  tocindent=10em,%
%<article>  tocnumwidth=4.1em%
%<book|report>  tocnumwidth=5em%
]{paragraph}
\DeclareSectionCommand[%
  style=section,%
  level=5,%
  indent=\scr@parindent,%
  beforeskip=3.25ex \@plus1ex \@minus .2ex,%
  afterskip=-1em,%
%<article>  tocstyle=subsection,%
%<book|report>  tocstyle=section,%
%<article>  tocindent=10em,%
%<book|report>  tocindent=12em,%
%<article>  tocnumwidth=5em%
%<book|report>  tocnumwidth=6em%
]{subparagraph}
%<!v4>\expandafter\ifnum\scr@v@is@lt{3.15}\relax
%<!v4>  \let\scr@subparagraph@sectionindent\parindent
%<!v4>\else
  \def\scr@subparagraph@sectionindent{\scr@parindent}%
%<!v4>\fi
%</body>
%    \end{macrocode}
% \end{command}^^A \subparagraph
% \end{command}^^A \paragraph
% \end{command}^^A \subsubsection
% \end{command}^^A \subsection
% \end{command}^^A \section
% \end{command}^^A \chapter
% \end{command}^^A \part
%
%
% \subsection{Fonts}
%
% The font settings of dedicated elements of the the corresponding section
% levels already have been done. But there are also some elements that do not
% correspond with a single section level.
%
% \begin{fontelement}{disposition,sectioning}
% \changes{v2.95b}{2006/07/30}{added}
% \changes{v3.39}{2023/04/04}{also with \pkg*{scrextend} as documented}
% \begin{macro}{\sectfont}
% \changes{v2.8c}{2001/06/29}{\cs{normalcolor} added}
% \changes{v3.12}{2013/11/11}{earlier definition to support option
%   \opt{headings\quotechar=standardclasses}}
% \changes{v3.20}{2016/01/29}{\cs{sffamily} replaced by \cs{@gr@gsffamily}}
% \changes{v3.39}{2023/04/04}{\cs{@gr@gsfamily} replaced by \cs{maybesffamily}}
% This element is not only used by all section levels before the dedicated
% element of the single level is used. It is also used by other elements like
% the \fnt{chaptertocentry}. The command \cs{sectfont} instead of the
% \cs{newkomafont} default is used for compatibility only. So it is specified
% as internal. Using it as user command is deprecated!
%    \begin{macrocode}
%</class&!letter>
%<*(class&!letter)|(package&extend)>
%<*prepare>      
\newcommand*{\sectfont}{\normalcolor\maybesffamily\bfseries}
%</prepare>
%</(class&!letter)|(package&extend)>
%<*book|report|article|extend>
%<*body>
\newcommand*{\scr@fnt@disposition}{\sectfont}
\aliaskomafont{sectioning}{disposition}
%</body>
%</book|report|article|extend>
%    \end{macrocode}
% \end{macro}^^A \sectfont
% \end{fontelement}^^A disposition, sectioning
%
%
% \begin{option}{headings}
% \changes{v2.3h}{1996/01/20}{Size of \cs{chapter} decreased by one grade
%   step}
% \changes{v2.3h}{1996/01/20}{using \cs{chapterheadstartvskip} and
%   \cs{chapterheadendvskip}}
% \changes{v2.7c}{2000/01/19}{vertical distance after chapters slightly
%   changed and glue added}
% \changes{v2.98c}{2008/03/10}{added}
% \changes{v3.10}{2011/08/30}{extended optional argument}
% \changes{v3.12}{2013/03/05}{using \cs{FamilyKeyState}}
% \changes{v3.12}{2013/08/26}{\cs{KOMA@options} replaced by \cs{KOMAoptions}}
% \changes{v3.12}{2013/11/11}{value \opt{\quotechar=standardclasses} added}
% \changes{v3.15}{2014/11/24}{using \cs{scr@chapter@beforeskip} and
%   \cs{scr@chapter@afterskip}}
% \changes{v3.17}{2015/03/09}{using internal value storage}
% \changes{v3.18}{2015/05/20}{values of \cs{scr@chapter@beforeskip} negative}
% \changes{v3.39}{2022/11/11}{initial dot removed from member argument of
%   option storage commands}
% Option \opt{headings} is the main switch for the headings of all outlines.
% It provides the following values:
%    \begin{macrocode}
%<*class&!letter>
%<*option>
\KOMA@key{headings}{%
  \KOMA@set@ncmdkey{headings}{@tempa}{%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=big}:] use big sized headings. This also changes the distances
%   of chapter and redefined \cs{chapterheadstartvskip} and
%   \cs{chapterheadendvskip} (\cls*{scrbook} and \cls*{scrreprt} only).
% \end{description}
%    \begin{macrocode}
    {big}{0},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=normal}:] use medium sized headings. This also changes the
%   distances of chapter and redefined \cs{chapterheadstartvskip} and
%   \cs{chapterheadendvskip} (\cls*{scrbook} and \cls*{scrreprt} only).
% \end{description}
%    \begin{macrocode}
    {normal}{1},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=small}:] use small sized headings. This also changes the
%   distances of chapter and redefined \cs{chapterheadstartvskip} and
%   \cs{chapterheadendvskip} (\cls*{scrbook} and \cls*{scrreprt} only).
% \end{description}
%    \begin{macrocode}
    {small}{2},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=openany}:] same as \opt{open=any} for \cls*{scrbook} and
%   \cls*{scrreprt}.
% \end{description}
%    \begin{macrocode}
%<*book|report>
    {openany}{3},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=openright}:] same as \opt{open=right} for \cls*{scrbook} and
%   \cls*{scrreprt}.
% \end{description}
%    \begin{macrocode}
    {openright}{4},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=openleft}:] same as \opt{open=left} for \cls*{scrbook} and
%   \cls*{scrreprt}.
% \end{description}
%    \begin{macrocode}
    {openleft}{5},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=twolinechapter}:] use chapter headings with two lines. The first
%   line is a kind of prefix line with the number and a prefix text only. The
%   second one is the heading text.
% \end{description}
%    \begin{macrocode}
    {twolinechapter}{6},{chapterprefix}{6},{chapterwithprefix}{6},%
    {chapterwithprefixline}{6},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=onelinechapter}:] use chapter headings without the prefix line.
% \end{description}
%    \begin{macrocode}
    {onelinechapter}{7},{nochapterprefix}{7},{chapterwithoutprefix}{7},%
    {chapterwithoutprefixline}{7},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=twolineappendix}:] use chapter headings with two lines in the
%   appendix. This overwrites \opt{headings=onelinechapter} for the chapters
%   of the appendix.
% \end{description}
%    \begin{macrocode}
    {twolineappendix}{8},{appendixprefix}{8},{appendixwithprefix}{8},%
    {appendixwithprefixline}{8},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=onelineappendix}:] use chapter headings without prefix line in
%   the appendix. This overwrites \opt{headings=twolinechapter} for the
%   chapters of the appendix.
% \end{description}
%    \begin{macrocode}
    {onelineappendix}{9},{noappendixprefix}{9},{appendixwithoutprefix}{9},%
    {appendixwithoutprefixline}{9},%
%</book|report>
%    \end{macrocode}
% \begin{description}
% \item[\opt{=optiontotocandhead}:] activates the extended option handling of
%   all outline commands and uses an unspecified optional argument as default
%   of for the running head and the ToC entry.
% \end{description}
%    \begin{macrocode}
    {optiontotocandhead}{10},{optiontoheadandtoc}{10},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=optiontotoc}:] activates the extended option handling of all
%   outline commands and uses an unspecified optional argument as default for
%   the ToC entry only.
% \end{description}
%    \begin{macrocode}
    {optiontotoc}{11},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=optiontohead}:] activated the extended option handling of all
%   outline commands and uses an unspecified optional argument as default fot
%   the running head.
% \end{description}
%    \begin{macrocode}
    {optiontohead}{12},%
%    \end{macrocode}
% \begin{description}
% \item[\opt{=standardclasses}:] reconfigure headings (at least the fonts) to
%   look more like the headings of the standard classes than the defaults of
%   \KOMAScript.
% \end{description}
%    \begin{macrocode}
    {standardclasses}{13}%
  }{#1}%
  \ifx\FamilyKeyState\FamilyKeyStateProcessed
    \ifcase \@tempa\relax% big
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{big}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{normal}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{small}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{standardclasses}%
      \KOMA@kav@add{\KOMAClassFileName}{headings}{big}%
%    \end{macrocode}
% First of all we handle the three size values.
% \begin{description}
% \item[Note:] These are redefining \cs{chapterheadstartvkip} and
%   \cs{chapterheadendvskip}.
% \item[Note:] The |+\baselineskip+\parskip| is needed to stay compatible with
%   old version of \KOMAScript{} not providing \cs{DeclareSectionCommand}. They
%   are regarded inside setting of the preamble to print heading with
%   |beforeskip=0pt| at the very beginning of the text area.
% \end{description}
%    \begin{macrocode}
%<*book|report>
      \renewcommand*{\scr@chapter@beforeskip}{-3.3\baselineskip-\parskip}%
      \renewcommand*{\scr@chapter@afterskip}{%
        1.725\baselineskip \@plus .115\baselineskip \@minus .192\baselineskip
      }%
      \renewcommand*{\scr@chapter@innerskip}{.5\baselineskip}%
      \renewcommand*{\chapterheadstartvskip}{\vspace{\@tempskipa}}%
      \renewcommand*{\chapterheadendvskip}{%
%<!v4>        \expandafter\ifnum\scr@v@is@lt{3.15}\vspace\@tempskipa\else
          \vskip\@tempskipa
%<!v4>        \fi
      }%
      \renewcommand*{\chapterheadmidvskip}{\par\nobreak\vskip\@tempskipa}%
%</book|report>
      \setkomafont{part}{\Huge}%
      \setkomafont{partnumber}{\huge}%
%<book|report>      \setkomafont{chapter}{\huge}%
%<book|report>      \setkomafont{chapterprefix}{\size@chapter}%
      \setkomafont{section}{\Large}%
      \setkomafont{subsection}{\large}%
      \setkomafont{subsubsection}{\normalsize}%
      \setkomafont{paragraph}{\normalsize}%
      \setkomafont{subparagraph}{\normalsize}%
    \or % normal
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{big}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{normal}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{small}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{standardclasses}%
      \KOMA@kav@add{\KOMAClassFileName}{headings}{normal}%
%<*book|report>
      \renewcommand*{\scr@chapter@beforeskip}{-3\baselineskip-\parskip}%
      \renewcommand*{\scr@chapter@afterskip}{%
        1.5\baselineskip \@plus .1\baselineskip \@minus .167\baselineskip
      }%
      \renewcommand*{\scr@chapter@innerskip}{.5\baselineskip}%
      \renewcommand*{\chapterheadstartvskip}{\vspace{\@tempskipa}}%
      \renewcommand*{\chapterheadendvskip}{%
%<!v4>        \expandafter\ifnum\scr@v@is@lt{3.15}\vspace\@tempskipa\else
          \vskip\@tempskipa
%<!v4>        \fi
      }%
      \renewcommand*{\chapterheadmidvskip}{\par\nobreak\vskip\@tempskipa}%
%</book|report>
      \setkomafont{part}{\huge}%
      \setkomafont{partnumber}{\huge}%
%<*book|report>
      \setkomafont{chapter}{\LARGE}%
      \setkomafont{chapterprefix}{\size@chapter}%
      \setkomafont{section}{\Large}%
      \setkomafont{subsection}{\large}%
%</book|report>
%<*article>
      \setkomafont{section}{\large}%
      \setkomafont{subsection}{\normalsize}%
%</article>
      \setkomafont{subsubsection}{\normalsize}%
      \setkomafont{paragraph}{\normalsize}%
      \setkomafont{subparagraph}{\normalsize}%
    \or % small
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{big}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{normal}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{small}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{standardclasses}%
      \KOMA@kav@add{\KOMAClassFileName}{headings}{small}%
%<*book|report>
      \renewcommand*{\scr@chapter@beforeskip}{-2.8\baselineskip-\parskip}%
      \renewcommand*{\scr@chapter@afterskip}{%
        1.35\baselineskip \@plus 0.09\baselineskip \@minus .15\baselineskip
      }%
      \renewcommand*{\scr@chapter@innerskip}{.5\baselineskip}%
      \renewcommand*{\chapterheadstartvskip}{\vspace{\@tempskipa}}%
      \renewcommand*{\chapterheadendvskip}{%
%<!v4>        \expandafter\ifnum\scr@v@is@lt{3.15}\vspace\@tempskipa\else
          \vskip\@tempskipa
%<!v4>        \fi
      }%
      \renewcommand*{\chapterheadmidvskip}{\par\nobreak\vskip\@tempskipa}%
%</book|report>
      \setkomafont{part}{\LARGE}%
      \setkomafont{partnumber}{\LARGE}%
%<*book|report>
      \setkomafont{chapter}{\Large}%
      \setkomafont{chapterprefix}{\size@chapter}%
      \setkomafont{section}{\large}%
%</book|report>
%<*article>
      \setkomafont{section}{\normalsize}%
%</article>
      \setkomafont{subsection}{\normalsize}%
      \setkomafont{subsubsection}{\normalsize}%
      \setkomafont{paragraph}{\normalsize}%
      \setkomafont{subparagraph}{\normalsize}%
%    \end{macrocode}
% \cls*{scrbook} and \cls*{scrreprt} also provide settings that are delegated
% to option \opt{open}.
%    \begin{macrocode}
    \or % openany
%<*book|report>
      \KOMAoptions{open=any}%
%</book|report>
    \or % openright
%<*book|report>
      \KOMAoptions{open=right}%
%</book|report>
    \or % openleft
%<*book|report>
      \KOMAoptions{open=left}%
%</book|report>
%    \end{macrocode}
% \cls*{scrbook} and \cls*{scrreprt} also provide settings that are delegated
% to option \opt{chapterprefix} and \opt{appendixprefix}.
%    \begin{macrocode}
    \or % twolinechapter
%<*book|report>
      \KOMAoptions{chapterprefix=true}%
%</book|report>
    \or % onelinechapter
%<*book|report>
      \KOMAoptions{chapterprefix=false}%
%</book|report>
    \or % twolineappendix
%<*book|report>
      \KOMAoptions{appendixprefix=true}%
%</book|report>
    \or % onelineappendix
%<*book|report>
      \KOMAoptions{appendixprefix=false}%
%</book|report>
%    \end{macrocode}
% We have also options to handle the extended optional argument.
%    \begin{macrocode}
    \or % optiontotocandhead
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{optiontotocandhead}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{optiontotoc}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{optiontohead}%
      \KOMA@kav@add{\KOMAClassFileName}{headings}{optiontotocandhead}%
      \scr@activate@xsection{3}%
    \or % optiontotoc
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{optiontotocandhead}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{optiontotoc}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{optiontohead}%
      \KOMA@kav@add{\KOMAClassFileName}{headings}{optiontotoc}%
      \scr@activate@xsection{2}%
    \or % optiontohead
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{optiontotocandhead}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{optiontotoc}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{optiontohead}%
      \KOMA@kav@add{\KOMAClassFileName}{headings}{optiontohead}%
      \scr@activate@xsection{1}%
%    \end{macrocode}
% Last but not least the option to activate the standard class emulation of
% headings.
%    \begin{macrocode}
    \or % standardclasses
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{big}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{normal}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{small}%
      \KOMA@kav@remove{\KOMAClassFileName}{headings}{standardclasses}%
      \KOMA@kav@add{\KOMAClassFileName}{headings}{standardclasses}%
%<*book|report>
      \renewcommand*{\scr@chapter@beforeskip}{-50\p@}%
      \renewcommand*{\scr@chapter@afterskip}{40\p@}%
      \renewcommand*{\scr@chapter@innerskip}{20\p@}%
      \renewcommand*{\chapterheadstartvskip}{\vspace{\@tempskipa}}%
      \renewcommand*{\chapterheadendvskip}{\vskip\@tempskipa}%
      \renewcommand*{\chapterheadmidvskip}{\par\nobreak\vskip\@tempskipa}%
      \setkomafont{part}{\Huge}%
      \setkomafont{partnumber}{\huge}%
      \setkomafont{chapter}{\Huge}%
      \setkomafont{chapterprefix}{\huge}%
%</book|report>
%<*article>
      \setkomafont{part}{\huge}%
      \setkomafont{partnumber}{\Large}%
%</article>
      \setkomafont{section}{\Large}%
      \setkomafont{subsection}{\large}%
      \setkomafont{subsubsection}{\normalsize}%
      \setkomafont{paragraph}{\normalsize}%
      \setkomafont{subparagraph}{\normalsize}%
      \renewcommand*{\sectfont}{\bfseries}%
%<book|report>    \KOMAoptions{open=right,chapterprefix=true}%
    \fi
  \fi
}
\KOMA@kav@add{\KOMAClassFileName}{headings}{big}
%<book|report>\KOMA@kav@add{\KOMAClassFileName}{headings}{onelinechapter}
%    \end{macrocode}
% \end{option}^^A headings
%
% \begin{option}{bigheadings,normalheadings,smallheadings}
% \changes{v2.3h}{1996/01/20}{added}
% \changes{v2.4g}{1996/11/04}{option name fixed}
% \changes{v2.98c}{2008/03/10}{obsolete}
% \changes{v3.01a}{2008/11/20}{deprecated}
% \changes{v3.99}{2022/11/16}{removed from \KOMAScript~4}
% \begin{macro}{\@bigheadings,\@normalheadings,\@smallheadings}
% \changes{v2.98c}{2008/03/10}{removed}
% \end{macro}^^A \@bigheadings … \@smallheadings
% We also have some deprecated options.
%    \begin{macrocode}
%<*!v4>
\KOMA@DeclareDeprecatedOption{bigheadings}{headings=big}
\KOMA@DeclareDeprecatedOption{normalheadings}{headings=normal}
\KOMA@DeclareDeprecatedOption{smallheadings}{headings=small}
%</!v4>
%</option> 
%    \end{macrocode}
% \end{option}^^A smallheading,normalheadings,bigheadings
%
%    \begin{macrocode}
%</class&!letter>
%    \end{macrocode}
% 
% \Finale
% \PrintChanges
% 
\endinput
% Local Variables:
% mode: doctex
% ispell-local-dictionary: "en_US"
% eval: (flyspell-mode 1)
% TeX-master: t
% TeX-engine: luatex-dev
% eval: (setcar (or (cl-member "Index" (setq-local TeX-command-list (copy-alist TeX-command-list)) :key #'car :test #'string-equal) (setq-local TeX-command-list (cons nil TeX-command-list))) '("Index" "mkindex %s" TeX-run-index nil t :help "makeindex for dtx"))
% End:
