複数行のパートタイトルを目次の中央に配置する

複数行のパートタイトルを目次の中央に配置する

困惑しています。複数行のパートタイトルを目次の中央に配置するにはどうすればよいでしょうか?

\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. はじめに . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

                        Part I
             A moderately long part title
    
  2. 第2章 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

  3. 第3章 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
  4. 第4章 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

                       Part II
     A Very very very long part title which runs 
     over multiple lines in the table of contents
    
  5. 第 5 章 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

  6. 第 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}

ここに画像の説明を入力してください

関連情報