Centralizando uma parte de múltiplas linhas no toc

Centralizando uma parte de múltiplas linhas no toc

Estou perplexo. Como posso centralizar um título de parte de várias linhas no toc?

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

Idealmente, gostaria que o toc fosse apresentado como:

                       Contents             
  1. Introdução . . . . . . . . . . . . . . . . . . . . . . . . . 1

                        Part I
             A moderately long part title
    
  2. Capítulo 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

  3. Capítulo 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
  4. Capítulo 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

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

  6. Capítulo 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

Sinto muito se isso já foi perguntado antes, mas passei horas tentando hackear uma solução juntos, sem sucesso.

Responder1

No meu ponto de vista, usar um patch \@parté mais fácil do que usar tocloftcomandos aqui.

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

insira a descrição da imagem aqui

Uma configuração diferente com um\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}

insira a descrição da imagem aqui

informação relacionada