# Ways to modify chapter titles in KOMA-class

I am using KOMA-class. Let's say I want my chapter titles to look like this:

However the closest thing I can get is this:

The code I used is this:

\documentclass{scrbook}

\usepackage{kantlipsum} %providing dummy text

\RedeclareSectionCommand[style=section,indent=0pt,beforeskip=2\baselineskip]{chapter}
\setkomafont{chapter}{\normalfont\large\scshape}
\renewcommand*{\raggedsection}{\centering}

\begin{document}

\kant[1]

\chapter{An Interesting Chapter Title}

\kant[2]

\begin{center}
\vspace{\baselineskip}
\\
\rule{2em}{1pt}
\end{center}

\noindent\kant[3]

\end{document}


1. The font for the chapter title.
2. Centering.
3. Not starting a new page.
4. Vertical space before the title.

But I don't know how to add characters or even a new line after the actual chapter title is printed.

I would also like to know about further ways than those I already used, KOMA-classes provide to change chapter titles (whether it makes sense to use them is another thing).

Edit: As schtandard remarked, one has to make a decision how the title should look when it takes several lines. So let's see for example if we can get the following to work:

My code (which is certainly terrible):

    \begin{center}
\vspace{\baselineskip}
\scshape\large
\parbox{11cm}{\centering 1. An Interesting Chapter Title Which is Way Too Long to Fit in One Line}
\\[\baselineskip]
\rule{2em}{1pt}
\end{center}


Note that here the box has a fixed length of 11cm which is fine for taking the picture, but in practice, one should be able to specify a maximal width so that the box becomes shorter if the title is short.

• Do you want to change the section or chapter titles? – Johannes_B Mar 18 '18 at 9:59
• @Johannes_B In my current document, it's the chapter titles, but I guess it will be rather similar. – John Dorian Mar 18 '18 at 10:02
• Do you only have chapter headings that fit in one line or do some of them break into several lines? If so, where should the ornaments go in that case? – schtandard Mar 18 '18 at 10:44
• @schtandard Good question, I did not think about that one yet. To make it more difficult, let's say the two lines of the chapter title should be put in a box with a defined maximal width and the ornaments should be left and right to that box, vertically centered. (Just trying to see what is possible here.) – John Dorian Mar 18 '18 at 10:51
• Then how do you want your titles to appear in the ToC? – remco Mar 18 '18 at 10:59

If you use style=section for chapters then you have to redefine \sectionlinesformat to change the layout for chapter titles:

\documentclass{scrbook}
\usepackage{kantlipsum} %providing dummy text

\RedeclareSectionCommand[style=section,indent=0pt,beforeskip=-2\baselineskip]{chapter}
\setkomafont{chapter}{\normalfont\large\scshape}
\renewcommand*{\raggedchapter}{\centering}

\usepackage{varwidth}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
\ifstr{#1}{chapter}
{%
\raggedchapter
\begin{varwidth}{\dimexpr\textwidth-6em\relax}
\raggedchapter#3#4%
\end{varwidth}%
\par\nobreak
\strut\rule{2em}{1pt}%
\par
}
{\@hangfrom{\hskip#2#3}{#4}}% original definition for other section levels
}
\makeatother

\begin{document}
\kant[1]
\chapter{An Interesting Chapter Title}
\kant[2]
\chapter{An Interesting Chapter Title Which is Way Too Long to Fit in One Line}
\kant[3]
\end{document}


• Thank you. I think I really have to start reading also the Advanced part of the KOMA-script documentation. I will have a closer look at \chapterlinesformat. (I only changed the style of the chapters so as to avoid blank pages.) – John Dorian Mar 18 '18 at 14:04
• Is it possible to get rid of the indent in the very first paragraph of each chapter? – John Dorian Mar 18 '18 at 14:31
• Use beforeskip=-2\baselineskip (note the -). I have changed it in my answer. – esdd Mar 18 '18 at 14:40
\documentclass{scrbook}

\usepackage{etoolbox}

\usepackage{kantlipsum} %providing dummy text

\makeatletter
% \RedeclareSectionCommand[beforeskip=2\baselineskip]{chapter} % <-- this seems too little
\setkomafont{chapter}{\normalfont\large\scshape}
\renewcommand*{\raggedchapter}{\centering}
\renewcommand*{\chapterformat}{\thechapter.\ }
\patchcmd\scr@startchapter{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}

\renewcommand\chapterlinesformat[3]{%
\raggedchapter
\parbox{\dimexpr\linewidth-6em}{%
\raggedchapter
#2#3
}%
\@@par
\else
\parbox{\linewidth}{%
\raggedchapter%
{\let\@@par\relax
#2#3%
}%
}%
\@@par
\fi
\raggedchapter\strut\rule{2em}{.4pt}\par%
}
\makeatother

\begin{document}

\kant[1]

\chapter{An Interesting Chapter Title}

\kant[2]

\chapter{An Interesting Chapter Title Which is Way Too Long to Fit in One Line}

\kant[3]

\end{document}


What is happening here?

• Chapter is not redeclared to be of type section.
• The pagebreaks are instead avoided by removing the appropriate code from \scr@startchapger using \patchcmd from etoolbox.
• We now use \chapterlinesformat to format the chapter heading (#2 contains the formatted chapter number, #3 the formatted chapter title).
• We first check if the heading is longer than one line and then typeset it accordingly.
• #3 contains a \@@par. Since we want to have the second ornament on the same line as the title, we need to deactivate it before typesetting in the case of a single line heading.
• The \strut on the line with the \rule makes sure it has the correct distance from the heading in the case of multiple lines.

Please note that as a consequence of centering your headings, the second (and subsequent) lines of the chapter title may flow below the chapter number.

• I am still trying to understand your code. For now I only realized that I can make the ornaments look misplaced if I call my chapter like this: \chapter{AnInterestingChapterTitleWhich isWayTooLongtoFitinOneLine} Do you have an idea about this? – John Dorian Mar 18 '18 at 12:34
• @JohnDorian what do you mean? For me, the ornaments appear in the exact same spot as in the image above (for chapter 2). – schtandard Mar 18 '18 at 12:40
• I mean that they are very far away from the title and don't always have a distance of, say 1em, to the title, as they have in one-line titles. – John Dorian Mar 18 '18 at 12:44
• That's right. I do not know if it is possible to determine the width of the text inside the \parbox (maybe somebody else does), so I decided to just place the ornaments 1em from the parbox (which should usually be fine, I think). – schtandard Mar 18 '18 at 12:51

A bit of a hack, but one thing that worked for me for single line titles was adding

\newcommand{\mychapter}[1]{%