% Header and footer definitions for the SKS @document klammer.
%
% Three page style sets, one per document structure:
%
%   plain structure:   No headers.  Centered page number in footer.
%   article structure: No headers.  Centered page number in footer.
%                      First page has no header (same as regular pages).
%   book structure:    Verso header: chapter number + title.
%                      Recto header: section number + title.
%                      Page number in outer header margin.
%                      No footer.  Chapter-opening pages get centered
%                      page number in footer and no header.
%
% Special styles used across structures:
%   \pagestyle{empty}  — title pages, copyright pages, part pages, blank pages
%
% The style names use a "sks" prefix to avoid clashing with LaTeX's
% built-in page style names:
%   \pagestyle{sksplain}, \pagestyle{sksarticle}, \pagestyle{sksbook}
%
% titlesec's pagestyles option (loaded by titles.sty) provides \newpagestyle,
% \sethead, \setfoot, and the automatic marks \chaptertitle, \sectiontitle,
% \subsectiontitle.

\usepackage{ifthen}

% --- Font for header/footer text ---

\newcommand{\hdrfont}[1]{{\sffamily\scalefont{.9}{#1}}}

% --- Running head content ---
%
% These commands produce the running head text, using titlesec's automatic
% mark variables.  They suppress output when the counter is zero (before
% the first heading of that level has appeared).
%
% The \sect dispatch from titles.sty shifts heading levels between book
% and article classes: in book class, \sA expands to \chapter and the
% top-level counter is \thechapter; in article class, \sA expands to
% \section and the top-level counter is \thesection.  The running head
% commands follow the same shift.

\ifthenelse{\equal{\documentisbook}{true}}{%
  \newcommand{\runningchapter}{%
    \ifthenelse{\equal{0}{\thechapter}}{}{\hdrfont{\thechapter~~\chaptertitle}}}
  \newcommand{\runningsection}{%
    \ifthenelse{\equal{0}{\thesection}}{}{\hdrfont{\thesection~~\sectiontitle}}}
}{%
  \newcommand{\runningchapter}{%
    \ifthenelse{\equal{0}{\thesection}}{}{\hdrfont{\thesection~~\sectiontitle}}}
  \newcommand{\runningsection}{%
    \ifthenelse{\equal{0}{\thesubsection}}{}{\hdrfont{\thesubsection~~\subsectiontitle}}}
}

% --- Plain structure ---
%
% No headers, centered page number in footer, no rules.

\newpagestyle{sksplain}{%
  \ifdefined\documentbottom
    \setfoot{\hdrfont{\documentbottom}}{}{\hdrfont{\thepage}}%
  \else
    \setfoot{}{\hdrfont{\thepage}}{}%
  \fi
}

% --- Article structure ---
%
% Same as plain: no headers, centered page number in footer, no rules.
% Defined as a separate style so that it can be extended independently
% (e.g., adding author/title running heads for longer articles).

\newpagestyle{sksarticle}{%
  \ifdefined\documentbottom
    \setfoot{\hdrfont{\documentbottom}}{}{\hdrfont{\thepage}}%
  \else
    \setfoot{}{\hdrfont{\thepage}}{}%
  \fi
}

% --- Book structure ---
%
% Twoside layout with running heads and page numbers in the outer margin.
% Verso (even): chapter title in header.
% Recto (odd): section title in header.
% No footer on regular pages.

\newpagestyle{sksbook}{%
  \sethead[\hdrfont{\thepage}]%
          []%
          [\runningchapter]%
          {\runningsection}%
          {}%
          {\hdrfont{\thepage}}%
  \ifdefined\documentbottom
    \setfoot[\hdrfont{\documentbottom}]%
            []%
            []%
            {}%
            {}%
            {\hdrfont{\documentbottom}}%
  \fi
}

% --- Book opening style ---
%
% \chapter internally calls \thispagestyle{plain}, so we must override
% LaTeX's built-in plain style for book structure.  For plain and article
% structures, the override is harmless (plain is not used as the main
% page style, and this matches the centered-page-number convention).

\ifthenelse{\equal{\documentisbook}{true}}{%
  \renewpagestyle{plain}{%
    \ifdefined\documentbottom
      \setfoot{\hdrfont{\documentbottom}}{}{\hdrfont{\thepage}}%
    \else
      \setfoot{}{}{\hdrfont{\thepage}}%
    \fi
  }
}{%
  \renewpagestyle{plain}{%
    \ifdefined\documentbottom
      \setfoot{\hdrfont{\documentbottom}}{}{\hdrfont{\thepage}}%
    \else
      \setfoot{}{\hdrfont{\thepage}}{}%
    \fi
  }
}

% --- Noheader (compatibility) ---
%
% Used by toc.sty for TOC chapter entries.  No running heads, centered
% page number in footer.

\newpagestyle{noheader}{%
  \ifdefined\documentbottom
    \setfoot{\hdrfont{\documentbottom}}{}{\hdrfont{\thepage}}%
  \else
    \setfoot{}{\hdrfont{\thepage}}{}%
  \fi
}

\write0{Headers and page styles defined}
