% Section heading formatting for the SKS @document klammer.
%
% The \sect command dispatches between book and article document classes.
% Section klammers (@s1-@s7) emit \sA-\sE, which expand to the appropriate
% LaTeX sectioning command based on \documentisbook. The \defineheading
% utility eliminates duplication across heading levels.

\usepackage[bf,sf,big,raggedright,compact,newlinetospace,explicit,pagestyles,clearempty]{titlesec}
\usepackage{etoolbox}
\usepackage{ifthen}

% --- Structure dispatch ---

% Select first or second argument based on document class:
\newcommand{\sect}[2]{\ifthenelse{\equal{\documentisbook}{true}}{#1}{#2}}

% Sectioning commands, shifted by structure:
\newcommand{\sA}{\sect{\chapter}{\section}}
\newcommand{\sB}{\sect{\section}{\subsection}}
\newcommand{\sC}{\sect{\subsection}{\subsubsection}}
\newcommand{\sD}{\sect{\subsubsection}{\paragraph}}
\newcommand{\sE}{\sect{\paragraph}{\subparagraph}}

% Counters, shifted by structure:
\newcommand{\snA}{\sect{\thechapter}{\thesection}}
\newcommand{\snB}{\sect{\thesection}{\thesubsection}}
\newcommand{\snC}{\sect{\thesubsection}{\thesubsubsection}}
\newcommand{\snD}{\sect{\thesubsubsection}{\theparagraph}}
\newcommand{\snE}{\sect{\theparagraph}{\thesubparagraph}}

% Font sizes, shifted by structure:
\newcommand{\fsect}[2]{\ifthenelse{\equal{\documentisbook}{true}}{\sfont{#1}}{\sfont{#2}}}
\newcommand{\fA}{\fsect{1.6}{1.2}}
\newcommand{\fB}{\fsect{1.2}{1.1}}
\newcommand{\fC}{\fsect{1.1}{1.1}}
\newcommand{\fD}{\fsect{1.1}{1.1}}
\newcommand{\fE}{\fsect{1.1}{1.1}}

% --- Shared heading parameters ---

\newlength{\beforeheading}\setlength{\beforeheading}{4pt}
\newlength{\afterheading}\setlength{\afterheading}{-4pt}
\newcommand{\numsep}{8pt}

% Outdent section numbers into the left margin:
\newlength{\sectionoutdent}
\newcommand{\numleft}[1]{%
  \ifthenelse{\equal{#1}{}}%
    {}%
    {\setlength{\sectionoutdent}{0pt-\widthof{#1}-8pt}%
     \hspace*{\sectionoutdent}}}

% --- Heading definition utility ---

% Define titleformat and titlespacing for a heading level.
%   #1 = LaTeX sectioning command (e.g., \section, \chapter)
%   #2 = font command (e.g., \fA)
%   #3 = counter (e.g., \thesection, \thechapter)
\newcommand{\defineheading}[3]{%
  \titlespacing*{name=#1}{0pt}{\beforeheading}{\afterheading}%
  \titleformat{name=#1}[block]%
    {#2\numleft{#3}}%
    {#2#3}%
    {\numsep}%
    {##1}%
  \titlespacing*{name=#1,numberless}{0pt}{\beforeheading}{\afterheading}%
  \titleformat{name=#1,numberless}[block]%
    {#2}%
    {}%
    {0pt}%
    {##1}%
}

% --- Heading definitions ---
%
% Register \titleformat for the actual LaTeX sectioning commands, not for
% \sA-\sE.  When \sA{title} is used in the document, TeX expands \sA to
% \section or \chapter, and titlesec intercepts that command.  Formatting
% registered for \sA would never be reached.

% In book structure, chapters start on recto (odd) pages.  titlesec
% replaces \chapter's internals and ignores the book class's openright
% default, using \clearpage instead of \cleardoublepage.  The
% \chapterbreak hook restores recto-start behavior.
\ifthenelse{\equal{\documentisbook}{true}}{%
  \newcommand{\chapterbreak}{\cleardoublepage}
  \titleclass{\chapter}{top}
  \defineheading{\chapter}{\fA}{\thechapter}
  \defineheading{\section}{\fB}{\thesection}
  \defineheading{\subsection}{\fC}{\thesubsection}
  \defineheading{\subsubsection}{\fD}{\thesubsubsection}
  \defineheading{\paragraph}{\fE}{\theparagraph}
}{%
  \defineheading{\section}{\fA}{\thesection}
  \defineheading{\subsection}{\fB}{\thesubsection}
  \defineheading{\subsubsection}{\fC}{\thesubsubsection}
  \defineheading{\paragraph}{\fD}{\theparagraph}
  \defineheading{\subparagraph}{\fE}{\thesubparagraph}
}

% --- Counter resets ---

\makeatletter
\@addtoreset{paragraph}{section}
\@addtoreset{paragraph}{subsection}
\@addtoreset{paragraph}{subsubsection}

\@addtoreset{subparagraph}{section}
\@addtoreset{subparagraph}{subsection}
\@addtoreset{subparagraph}{subsubsection}
\@addtoreset{subparagraph}{paragraph}
\makeatother

\setcounter{secnumdepth}{6}
\setcounter{tocdepth}{6}

% --- Part ---

\newcounter{partnumber}
\stepcounter{partnumber}

\newcommand{\partspace}{\hspace*{64pt}}
\newcommand{\partindent}[1]{\partspace\large\bfseries #1}
