# Insert code before and after chapter titles in KOMA-Script

I’m trying to add code before and after centered chapter titles in scrbook. I tried to do this by redefining \chapterlinesformat.

\documentclass[twoside=semi]{scrbook}

\renewcommand{\raggedchapter}{\centering}
\renewcommand{\chapterlinesformat}[3]{Before~#3~After}

\begin{document}
\chapter*{Test}
\end{document}


But this doesn’t work.

I guss this is because #3 is formatted and contains \@@par at its end. (I learned this from an answer to an earlier question.)

This problem is very similar (nearly identical) to another one. But the approaches given there won’t work for me:

• The first answer uses titlesec, which shouldn’t be used together with KOMA-Script.
• The second answer requires additional code after the table of content. But in my use case, all the configuration must take place in the preamble (or, more specifically, in the class file).

My current idea is to implement the second approach by redefining \tableofcontents to include the necessary additional code. But this seems rather hacky to me.

As you (and the KOMA-Script documentation) note, the argument #3 contains \@@par at its very end, which is what's bothering you. KOMA-Script does not provide a hook to insert text after the title.

The easiest hack is to just remove the \@@par and insert it after the inserted code.

\documentclass{scrbook}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\renewcommand*\raggedchapter{\centering}

\makeatletter
\def\@strip@trailing@@par#1\@@par{#1}
\newcommand*\@appendkomachaptertitle[2]{\@strip@trailing@@par #1#2\@@par}
\renewcommand*\chapterlinesformat[3]{Before~\@appendkomachaptertitle{#3}{~After}}
\makeatother

\begin{document}

\chapter*{Test}

\end{document}