[Tex/LaTex] Same \chapterformat for \chapter and \chapter* without titlesec

chapterskoma-scriptsectioning

I've done some styling for my chapter titles, based upon tikz and the titlesec package.

Unfortunately, the KOMA-Script packages gives me numerous warnings about titlesec. In order to get rid of the titlesec-package, I've tried to accomplish the same chapter style with the build-in \chapterformat of KOMA-Script. This works quite well, until I noticed that the \chapter*-like headings (e.g. TOC and so on) were not affected.

After doing some research, I've found the – trivial – reason for this behaviour. \chapterformat affects only the number part of the title. All the \chapter* have no chapter number, so \chapterformat does not work.

The Question

Is it possible to format both \chapter and \chapter* with pure KOMA-Script, or does this require titlesec or a similar package?


MWE

\documentclass{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\usepackage{tikz}
\usepackage[T1]{fontenc}

\renewcommand{\chapterformat}{
    \begin{tikzpicture}[remember picture,overlay]
        \draw[fill=gray,draw=none] (0,0) rectangle (1cm,1cm);
    \end{tikzpicture}
    \thechapter
}

\begin{document}
\chapter{The Chapter}
\blindtext
\chapter*{The Chapter without number}
\blindtext
\end{document}

compiled MWE

MWE, compiled with pdflatex/miktex 2.9/KOMAScript 3.18

Just for info

This is maybe not really part of the question, however that's the current \chapter style in my document. \chapter* should have the same position and background, without the orange bar. The one above is only a MWE for demo purposes.

This is the styled chapter

Best Answer

Since KOMA-Script version 3.19 the command \chapterlinesformat can be redefined to change the layout of chapter titles which have no prefix line. Now it is possible to position the chapter title in a graĆ½ box on top of the page without using scrlayer-scrpage. And chapter titles with more than one line are possible.

\documentclass[twoside,open=any]{scrreprt}[2015/10/03]
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
%\providecommand*\Ifthispageodd{\ifthispageodd}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\usepackage[T1]{fontenc}
\usepackage{libertine}

\usepackage{xcolor}

\RedeclareSectionCommand[
  beforeskip=1sp minus 1sp,
  afterskip=4\baselineskip plus .2\baselineskip minus .2\baselineskip,
  font=\Huge\scshape\color{white}
]{chapter}

\renewcommand\chapterformat{%
  \makebox[0pt][r]{\thechapter\enskip
    \textcolor{orange}{\smash{\rule[-\dp\strutbox]{2.5pt}{\baselineskip}}}%
}}

\newlength\chapterleftmargin
\newcommand\chaptervmargin{1.5em}

\makeatletter
\renewcommand\chapterlinesformat[3]{%
  \vspace*{\dimexpr-1in-\headsep-\headheight-\topmargin-1ex}%
  \Ifthispageodd
    {\setlength\chapterleftmargin{\dimexpr1in+\hoffset+\oddsidemargin\relax}}
    {\setlength\chapterleftmargin{\dimexpr\paperwidth-\textwidth-1in-\hoffset-\oddsidemargin\relax}}
    \hspace*{-\chapterleftmargin}%
    \makebox[0pt][l]{%
      \colorbox{gray}{%
        \parbox[t][\dimexpr\totalheight+\chaptervmargin*2\relax][c]{\dimexpr\paperwidth-2\fboxsep\relax}{%
          \makebox[\dimexpr\chapterleftmargin-\fboxsep\relax][r]{#2}%
          \Ifstr{#2}{}
            {\parbox[t]{\textwidth}{\raggedchapter#3}}
            {\enskip\parbox[t]{\dimexpr\textwidth-.5em\relax}{\raggedchapter#3}}%
        }%
      }%
    }%
}
\makeatother

\begin{document}
\tableofcontents
\chapter{The chapter}
\KOMAScriptVersion

\Blindtext
\chapter*{The Chapter without number}
\blindtext
\chapter{A long long long long chapter title with more than one line}
\blindtext
\Blinddocument
\end{document}

Run twice (because of \Ifthispageodd) to get

enter image description here