![如何提升 Koma 腳本中的章節標題,使其與章節前綴對齊?](https://rvso.com/image/286992/%E5%A6%82%E4%BD%95%E6%8F%90%E5%8D%87%20Koma%20%E8%85%B3%E6%9C%AC%E4%B8%AD%E7%9A%84%E7%AB%A0%E7%AF%80%E6%A8%99%E9%A1%8C%EF%BC%8C%E4%BD%BF%E5%85%B6%E8%88%87%E7%AB%A0%E7%AF%80%E5%89%8D%E7%B6%B4%E5%B0%8D%E9%BD%8A%EF%BC%9F.png)
我目前有這個:
\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 章的輸出。
此外,目錄的輸出不受小型頁面的影響,請另參閱下面的輸出。
另外,我使用\RaggedRight
fromragged2e
作為替換,\raggedright
以便更好地對齊。
除了\NewDocumentCommand
使用 from之外xparse
,我還可以使用經典方法以及對套件或\newcommand
中的空字串進行測試。(x)ifthen
etoolbox
\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}