KOMA 章节格式:双行文本对齐

KOMA 章节格式:双行文本对齐

基于https://github.com/latextemplates/scientific-thesis-template我正在重新定义 scrbook 类文档的 chapterformat,以便将章节编号放在以章节文本为中心的彩色框中。但是,一旦章节文本延伸到多行,该框将仅以第一行为中心(参见以下 MWE)。有人知道我如何将带有 chapternumber 的框相对于整个 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}

它只需一行:

它应该是什么样子

但多行对齐不正确:

两条线看起来如何

谢谢并期待您的好主意!

答案1

在您的 MWE 中,即使文本适合一行,章节编号也不会与章节文本垂直居中。文本上方的空间比下方的空间大。

如果要用于\fontsize{38}{42}章节编号,则需要可缩放字体。如果章节需要粗体和小写,则需要提供此类字体形状的字体。

您可以重新定义\chapterlinesformat以更改章节编号和章节文本的垂直对齐方式。

带字体lmodern(可扩展):

\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}

在此处输入图片描述

在此处输入图片描述

或者使用kpfonts(可缩放字体,提供无衬线粗体小写字母):

\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}

在此处输入图片描述

在此处输入图片描述

相关内容