7

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

  • Welcome to TeX.SX! It's (well-known) that one should not use KOMA and titlesec together. Either KOMA xor titlesec, then use a different class, report, for example. – user31729 Jul 27 '15 at 17:07
  • @ChristianHupfer Does this mean, that there is no way to have a styled \chapter* in KOMA? By the way, what exactly is the reason for this advice? I've already used them both together and I've never experienced any problems, only the warnings from KOMA. Is this some kind of a 'It could crash with the next version'-problem? – Stefan Braun Jul 27 '15 at 20:20
  • 1
    Well, as you wrote: It could crash with the next version. KOMA and titlesec both do 'strange' things to the sectioning commands and their headings and both do it differently, as far as I know. I think, many of the stuff done with titlesec can be done with KOMA macros too, but I have to admit, that I am no fan of neither KOMA nor titlesec, so others should provide real answers. This is of course only a comment – user31729 Jul 27 '15 at 20:23
10

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.