如何提升 Koma 腳本中的章節標題,使其與章節前綴對齊?

如何提升 Koma 腳本中的章節標題,使其與章節前綴對齊?

我目前有這個:

\documentclass{scrbook}
\usepackage{mwe}
\renewcommand*{\chapterformat}{\mbox{\chapappifchapterprefix{\nobreakspace}\scalebox{3}{\thechapter}\enskip}}

\begin{document}
\chapter{\baselineskip{-1em}This chapter caption has multiple lines and does not fit into a single line}
\lipsum[1]
\end{document}

前綴和標題僅與第一行對齊

但我想讓章節標題與前綴的基線對齊,類似:

我想要擁有什麼

我知道長章節標題很痛苦。但有時,它們不適合放在一行中,我認為第一個結果有點令人不安。

答案1

使用 a\Longstack將適用於兩行章節名稱,並手動插入分隔符號。但是,如果您達到三行,則沒有任何方法適合查看。

\documentclass{scrbook}
\usepackage{mwe}
\renewcommand*{\chapterformat}{\mbox{\chapappifchapterprefix{\nobreakspace}\scalebox{3}{\thechapter}\enskip}}
\usepackage{lipsum}
\usepackage[usestackEOL]{stackengine}
\begin{document}
\chapter{\Longstack[l]{This chapter caption has multiple lines\\ and does not fit into a single line}}
\lipsum[1]
\end{document}

在此輸入影像描述

另請注意,\Longstack不會將右邊緣標題文字與右側邊距對齊。它的出現在這裡純屬偶然。

最後,如果您希望使用目錄,則需要使用可選參數\chapter

\chapter[This chapter caption has multiple lines and does not fit into a single line]%
  {\Longstack[l]{This chapter caption has multiple lines\\ and does not fit into a single line}}

以避免堆疊出現在目錄中。

答案2

使用迷你頁自動換行的解決方案:

小型頁面需要寬度規格。因此,我定義了一個\chapmark包含大部分定義的新命令\chapterformat,我可以測量其長度。為此,我使用了套件的兩個功能calc:命令\widthof和計算長度的可能性。基線方向由b迷你頁的可選參數保護。

\Chapter命令(大寫 C)的定義方式可讓您使用可選參數中給出的 KOMA-Script 的增強功能,但請注意,它不能與chapterprefix設定為 true 的 KOMA-Script 選項一起使用。\chapter那就必須使用預設值,請參見。下面是範例中第 11 章和第 12 章的輸出。

此外,目錄的輸出不受小型頁面的影響,請另參閱下面的輸出。

另外,我使用\RaggedRightfromragged2e作為替換,\raggedright以便更好地對齊。

除了\NewDocumentCommand使用 from之外xparse,我還可以使用經典方法以及對套件或\newcommand中的空字串進行測試。(x)ifthenetoolbox

\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}

\usepackage{calc} % provides advanced length computation and command "\widthof"
\usepackage{ragged2e}% better text alignment
\usepackage{xparse}% advanced command definitions

\renewcommand*{\raggedchapterentry}{\RaggedRight}% for chapter TOC entries
\renewcommand*{\raggedsection}{\RaggedRight}% for alignment in titles

\newcommand*{\chapmark}{%
  \scalebox{1.5}{\chapappifchapterprefix{\nobreakspace}}\scalebox{3}{\thechapter}\enskip%
}
\renewcommand*{\chapterformat}{%
  \begin{minipage}[b]{\widthof{\chapmark}}
    \chapmark
  \end{minipage}%
}

\NewDocumentCommand\Chapter{o m}{% note the uppercase "C"
  \IfValueTF{#1}% optional argument given or not
  {% with optional argument:
    \chapter[#1]{%
      \begin{minipage}[b]{\textwidth-\widthof{\chapmark}}
      #2
      \end{minipage}}%
  }{% without optional argument:
    \chapter[#2]{%
      \begin{minipage}[b]{\textwidth-\widthof{\chapmark}}
      #2
      \end{minipage}}%
  }
}

\begin{document}
\Chapter{This chapter caption is too long to fit into a single line} % ch. 1

\Chapter[TOC entry for caption with 3 lines] % ch. 2
{This chapter caption is longer than the first one and does not even fit into
two lines}

\setcounter{chapter}{9}

\Chapter{Another chapter caption that is too long to fit into a single line} % ch. 10

\KOMAoption{chapterprefix}{true}

\Chapter[Another TOC entry for caption with 3 lines] % ch. 11
{This chapter caption is also longer than the first one and does not even fit into
two lines}

\chapter{% ch. 12
This caption produced with \textmd{\textbackslash chapter} is too long to fit into
a single line}

\tableofcontents

\end{document}

第一章標題的輸出

第二章標題的輸出

第三章標題的輸出

第四章標題的輸出

第五章標題的輸出

目錄的輸出

相關內容