
我很困惑。如何使多行部分標題在目錄中居中?
\documentclass[a4paper,12pt,openright]{book}
\usepackage{tocloft}% toc spacing and ragged right (below)
\cftpagenumbersoff{part}
\renewcommand{\cfttoctitlefont}{\hfill\Large}
\renewcommand{\cftaftertoctitle}{\hfill}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\chapter{Chapter 2}
\part{A moderately long part title}
\chapter{Chapter 3}
\chapter{Chapter 4}
\chapter{Chapter 5}
\part{A Very very very long part title which runs over multiple lines in the table of contents}
\chapter{Chapter 6}
\chapter{Chapter 7}
\chapter{Chapter 8}
\end{document}
理想情況下,我希望目錄佈局為:
Contents
介紹 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 1
Part I A moderately long part title
第2章 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 10
- 第3章 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 20
第4章。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 30
Part II A Very very very long part title which runs over multiple lines in the table of contents
第5章。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 40
- 第6章。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 50
如果有人之前問過這個問題,我很抱歉,但我花了幾個小時試圖組合解決方案,但無濟於事。
答案1
我的觀點是,使用補丁\@part
比在這裡使用命令更容易tocloft
。
\documentclass[a4paper,12pt,openright]{book}
\usepackage{xpatch}
\usepackage{tocloft}% toc spacing and ragged right (below)
%\cftpagenumbersoff{part}
\makeatletter
\xpatchcmd{\@part}{%
\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
% Rather use an `\addcontentsline` to get rid off the restrictions of `\contentsline` etc.
\addtocontents{toc}{\begingroup\Large \mdseries\protect\centering\partname\ \thepart\par\large\protect\centering#1\par\endgroup}
}{}{}
\makeatother
\renewcommand{\cfttoctitlefont}{\hfill\Large}
\renewcommand{\cftaftertoctitle}{\hfill}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\chapter{Chapter 2}
\part{A moderately long part title}
\chapter{Chapter 3}
\chapter{Chapter 4}
\chapter{Chapter 5}
\part{A Very very very long part title which runs over multiple lines in the table of contents}
\chapter{Chapter 6}
\chapter{Chapter 7}
\chapter{Chapter 8}
\end{document}
不同的設定\parbox
\documentclass[a4paper,12pt,openright]{book}
\usepackage{xpatch}
\usepackage{tocloft}% toc spacing and ragged right (below)
\DeclareRobustCommand{\wrapmytitles}[1]{%
\leavevmode
\centering
\parbox{0.7\linewidth}{\centering #1}%
}
\makeatletter
\xpatchcmd{\@part}{%
\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
\addtocontents{toc}{\begingroup\Large \mdseries\protect\centering\partname\ \thepart\par\large\wrapmytitles{#1}\endgroup}
}{}{}
\makeatother
\renewcommand{\cfttoctitlefont}{\hfill\Large}
\renewcommand{\cftaftertoctitle}{\hfill}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\chapter{Chapter 2}
\part{A moderately long part title}
\chapter{Chapter 3}
\chapter{Chapter 4}
\chapter{Chapter 5}
\part{A Very very very long part title which runs over multiple lines in the table of contents}
\chapter{Chapter 6}
\chapter{Chapter 7}
\chapter{Chapter 8}
\end{document}