1

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]):

enter image description here

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:original changed (changes marked in red for illustration only):change

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

0

You can redefine \chapterlinesformat, eg.

\documentclass[a4paper,fontsize=12pt,oneside]{scrbook}
\addtokomafont{chapter}{\rmfamily\mdseries\scshape}
\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}

enter image description here

  • That nails it, thanks so much, esdd! – Tom Jul 31 at 19:49

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.