# Formatting of multiline chapter headings using KOMA

I need (publisher requirement) to format the chapter headings in a document in such a way, that the chapter number is aligned with the text on the page, but if the chapter is longer than one line, the following lines should have a horizontal space of 5mm from the beginning of the first line.

Maybe best explained in this picture:

The green arrow should be 5mm, but the blue arrow might not be 5mm.

Since I use KOMA, I can't use titlesec. Instead I tried to achieve this, by modifying an example from the KOMA guide:

\documentclass{scrbook}

\renewcommand*{\chapterformat}{\makebox[5mm][l]{\thechapter}}

\begin{document}
\chapter{A very long chapter title asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd}
kjdhfaksdjhf
\end{document}


This adds an \mbox of length 5mm around the chapter number, but the problem is now, that (in the picture) also the blue arrow is always 5mm long. When the chapter number gets bigger (2 digits), the 5mm are too short.

Now the question: is there a way to change the horizontal space only on the second/third/... line (when the KOMA-script class is used).

The distance between the chapter number and the first letter of the chapter heading should be the same no matter what the number is. That means no matter whether the number is one digit or two digits.

-

One way of doing this would be to patch the command that typesets the chapter heading \@@makechapterhead and insert the desired space. This way it is automated and won't form part of the ToC entries. Patching is possible using the etoolbox package.

In the MWE below, you need to specify the lengths \chapnumwidth and \chaplabelsep using \setlength commands, and the patch will take care of the rest. Using/setting lengths this way makes for a more generalized solution that can be modified in the future with very little hassle.

\documentclass{scrbook}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\newsavebox{\@chapnumbox}% Box to store chapter number
{\size@chapter{#1}}% <search>
{\sbox{\@chapnumbox}{\size@chapter{\thechapter}}% Store chapter number
\size@chapter{\hspace*{\dimexpr\chaplabelsep-\chapnumwidth+\wd\@chapnumbox\relax}#1}}% <replace>
{}% <success>
{}% <failure>
\makeatother
\renewcommand*{\chapterformat}{\makebox[\chapnumwidth][l]{\thechapter}}
\newlength{\chapnumwidth} \setlength{\chapnumwidth}{5mm}% Chapter number/label width
\newlength{\chaplabelsep} \setlength{\chaplabelsep}{2em}% Chapter number/label separation
\begin{document}
\chapter{A very long chapter title asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd}
kjdhfaksdjhf

\setcounter{chapter}{21}

\chapter{A very long chapter title asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd asd}
kjdhfaksdjhf

\end{document}


The format for patching is

\patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}


which replaces <search> in <cmd> with <replace> and executes <success> if successful, or <failure> otherwise. You'll notice that the patching in the MWE saves the chapter number (in the appropriate font size/format) in a box

\sbox{\@chapnumbox}{\size@chapter{\thechapter}}% Store chapter number


\hspace*{dimexpr\chaplabelsep-\chapnumwidth+\wd\@chapnumbox\relax}

before the chapter heading (#1). This means that your chapter heading will start exactly \chaplabelsep from the chapter label, since the removed \chapnumwidth is the actual \chapterformat width. Of course, you can modify as needed.
In summary, the width from the left margin for the chapter heading is specified by the \chapterformat command (as you've already done), while the first line is adjusted using the patched form of \@@makechapterhead which restores the \chapterformat width and subsequently advances it by the width of the chapter number/label and \chaplabelsep.