3

based on https://github.com/latextemplates/scientific-thesis-template I am redefining the chapterformat of a scrbook class document to get the chapter number in a coloured box centred on the chapter text. However, once the chapter text extends over multiple lines, the box is only centred on the first line (see following MWE). Has someone an idea how I could center the box with the chapternumber on with respect to the whole chaptertext?

\documentclass[
a4paper,
twoside,
bibliography=totoc,
headsepline,
cleardoublepage=empty,
parskip=half,
draft=false
]{scrbook}

\usepackage{xcolor}
\usepackage{blindtext}

% Chapter Style
\setkomafont{section}{\normalsize}

\definecolor{darkblue}{rgb}{0,0,.5}
\definecolor{black}{rgb}{0,0,0}

\newkomafont{chapterformat}{\fontsize{38}{42}\normalfont\bfseries\color{number}}
\newkomafont{chapterformattext}{\normalfont\small\itshape}
\newkomafont{sectionformat}{\Large\color{number}}
\colorlet{numberbackground}{darkblue}
\colorlet{disposition}{darkblue}
\colorlet{number}{white}
\addtokomafont{disposition}{\color{disposition}}
\addtokomafont{chapter}{\scshape}
\renewcommand*{\chapterformat}{%
    {%
        \usekomafont{chapterformat}{%
            \raisebox{-\dimexpr\fboxsep+\fboxrule+0.5em\relax}{%
                \colorbox{numberbackground}{%
                    \parbox[b][2em][c]{2em}{\centering
                        \thechapter
                    }%
                }%
            }%
            
        }%
    }%
    \enskip
}
\begin{document}
    
    \chapter{This is working}
    \blindtext
    
    \chapter{This is a heading extending over two lines}
    \blindtext

    
\end{document}

It works with one line:

How it should look like

But alignment is off with multiple lines:

How it looks with two lines

Thanks and looking forward to your good ideas!

1 Answer 1

1

In your MWE the chapter number is not verticaly centered with the chapter text, even if the text fits in one line. There is more space above the text than below.

If you want to use \fontsize{38}{42} for the chapter numbers, you need a scalable font. If the chapter should be bold and small caps you need a font which provides such a font shape.

You can redefine \chapterlinesformat to change the vertical alignment of chapter number and chapter text.

With font lmodern (scalable):

\documentclass[
a4paper,
twoside,
bibliography=totoc,
headsepline,
cleardoublepage=empty,
parskip=half,
draft=false
]{scrbook}
\usepackage[T1]{fontenc}
\usepackage{lmodern}% <- added: scalable font

\usepackage{xcolor}
\usepackage{blindtext}% only for dummy text

% Chapter Style
\setkomafont{section}{\normalsize}

\definecolor{darkblue}{rgb}{0,0,.5}
\definecolor{black}{rgb}{0,0,0}

\newkomafont{chapterformat}{\fontsize{38}{42}\normalfont\bfseries\color{number}}
\newkomafont{chapterformattext}{\normalfont\small\itshape}
\newkomafont{sectionformat}{\Large\color{number}}
\colorlet{numberbackground}{darkblue}
\colorlet{disposition}{darkblue}
\colorlet{number}{white}
\addtokomafont{disposition}{\color{disposition}}
%\addtokomafont{chapter}{\scshape}% font does not provide bold small caps
\renewcommand*{\chapterformat}{%
  {%
    \usekomafont{chapterformat}{%
      \colorbox{numberbackground}{%
        \parbox[c][2em][c]{2em}{\centering
          \thechapter
        }%
      }%
    }%
  }%
  \enskip
}
%
\newbox{\chapternumberbox}
\makeatletter
\renewcommand{\chapterlinesformat}[3]{%
  \sbox\chapternumberbox{#2}%
  \@hangfrom{\usebox\chapternumberbox}{%
    \parbox[c]{\dimexpr\textwidth-\wd\chapternumberbox\relax}{\raggedchapter#3}\par%
  }%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{This is working}
\blindtext
\chapter{This is a heading extending over two lines}
\blindtext
\end{document}

enter image description here

enter image description here

Or using kpfonts (scalable font, provides sans serif bold small caps):

\documentclass[
a4paper,
twoside,
bibliography=totoc,
headsepline,
cleardoublepage=empty,
parskip=half,
draft=false
]{scrbook}
\usepackage[T1]{fontenc}% <- added
\usepackage{kpfonts}% <- added: scalable font, , provides sans serif bold small caps

\usepackage{xcolor}
\usepackage{blindtext}% only for dummy text

% Chapter Style
\setkomafont{section}{\normalsize}

\definecolor{darkblue}{rgb}{0,0,.5}
\definecolor{black}{rgb}{0,0,0}

\newkomafont{chapterformat}{\fontsize{38}{42}\normalfont\bfseries\color{number}}
\newkomafont{chapterformattext}{\normalfont\small\itshape}
\newkomafont{sectionformat}{\Large\color{number}}
\colorlet{numberbackground}{darkblue}
\colorlet{disposition}{darkblue}
\colorlet{number}{white}
\addtokomafont{disposition}{\color{disposition}}
\addtokomafont{chapter}{\scshape}
\renewcommand*{\chapterformat}{%
  {%
    \usekomafont{chapterformat}{%
      \colorbox{numberbackground}{%
        \parbox[c][2em][c]{2em}{\centering
          \thechapter
        }%
      }%
    }%
  }%
  \enskip
}
%
\newbox{\chapternumberbox}
\makeatletter
\renewcommand{\chapterlinesformat}[3]{%
  \sbox\chapternumberbox{#2}%
  \@hangfrom{\usebox\chapternumberbox}{%
    \parbox[c]{\dimexpr\textwidth-\wd\chapternumberbox\relax}{\raggedchapter#3}\par%
  }%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{This is working}
\blindtext
\chapter{This is a heading extending over two lines}
\blindtext
\end{document}

enter image description here

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .