Formatting of multi-line Chapter heading with scrreprt: vertical alignment

I am not too much into Latex and after a long time of fruitless tinkering, I would like to ask a question here.

I am writing a large document, using the scrbook KOMA-script. For chapter heading formatting, I am using a style like this ([https://texblog.org/2012/07/03/fancy-latex-chapter-styles/][1]):

For single line headings, this looks fine. However, for multi-line chapter headings I would like to change the style such that the grey bar extends to all lines and the chapter number is vertically aligned in the middle of the grey bar.

original: changed (changes marked in red for illustration only):

MWE:

\documentclass[a4paper,fontsize=12pt,oneside]{scrbook}

\usepackage{xcolor}
\renewcommand*{\chapterformat}{%
\thechapter \enskip
\textcolor{gray!50}{\rule[-\dp\strutbox]{2pt}{\baselineskip}}\enskip
}

\begin{document}
\chapter{Single-line Chapter}
\chapter{Multi-line Chapter Multi-line Chapter Multi-line Chapter Multi-line Chapter }
\end{document}


It would be great if someone could help me. Thanks a lot in advance!

Kind regards,
Tom

You can redefine \chapterlinesformat, eg.

\documentclass[a4paper,fontsize=12pt,oneside]{scrbook}
\usepackage{xcolor}
\renewcommand*{\chapterformat}{\thechapter}

\newsavebox\chapternumberbox
\newsavebox\chaptertextbox

\let\originalchapterlinesformat\chapterlinesformat
\renewcommand\chapterlinesformat[3]{%
\IfArgIsEmpty{#2}
{\originalchapterlinesformat{#1}{#2}{#3}}
{\ifstr{#1}{chapter}
{%
\savebox\chapternumberbox{#2}%
\savebox\chaptertextbox{%
\parbox{\dimexpr\textwidth-\wd\chapternumberbox-1em-2pt\relax}{\raggedright#3}%
}%
\raisebox{\dimexpr-.25\dp\strutbox}
{\usebox\chapternumberbox}%
\enskip
\textcolor{gray!50}{%
\rule[\dimexpr-\dp\chaptertextbox-.5ex\relax]{2pt}
{\dimexpr\ht\chaptertextbox+\dp\chaptertextbox+1ex\relax}%
}%
\enskip
\usebox\chaptertextbox
\par
}
{\originalchapterlinesformat{#1}{#2}{#3}}%
}%
}

\begin{document}
\tableofcontents
\chapter{Single-line Chapter}
\chapter{Multi-line Chapter Multi-line Chapter Multi-line Chapter Multi-line Chapter }
\end{document}


• That nails it, thanks so much, esdd! – Tom Jul 31 '19 at 19:49
• How to change your code so that the chapter number is aligned with the first line of the title in case of a multiline chapter title? I tried several different values for the \raisebox command but could not make it work. Thanks! – Paul Nov 27 '19 at 22:23
• @Paul Use option t \parbox[t]{...}{...} for \chaptertextbox and do not raise \chapternumberbox. If you need more help, then ask a new question. – esdd Dec 3 '19 at 16:10