2

Beginning latex user here,

I'm trying to customise a TOC using titletoc. What would like to align the parts and chapters so that the chapter/part names (e.g. Chapter 1, Part I) are right-aligned, whereas the titles of the chapters and parts should be left-aligned. Basically creating a gap between the names and titles that is exactly the same for chapters and parts.

I can think of possible solutions but cannot wrap my head around whether they work, and if so how to implement them:

  1. Put the Part X and Chapter X in a text-box of predefined with and align these. Problem with this is how to separate the chapter/part title from the chapter/part name?
  2. Increase the space between part name and chapter name. Problem with this is that this will not left-align chapter/part names.

I would really like to keep using titletoc because of its integration with titlesec, and I hope this is possible.

Thank you in advance for any ideas!

\documentclass{book}
\usepackage[newparttoc]{titlesec}
\usepackage{titletoc}

\titleformat{\part}
  [display]
  {\centering\bfseries\Huge}
  {\huge\partname~\thepart}
  {2ex}
  {}

\contentsmargin[2.5cm]{-30pt}

\titlecontents{part}
  [0em]
  {\normalfont\vspace{1.5em}}
  {{\partname~\thecontentslabel~\quad}}
  {}
  {}
  [\titlerule\addvspace{.25em}]

\titlecontents{chapter}
  [0em]
  {\normalfont\filright}
  {\contentsmargin{0pt}\contentspush{\bfseries\chaptername~\thecontentslabel~\quad}}
  {}
  {\hfill\thecontentspage}
  [\addvspace{0.25em}]

\begin{document}

\tableofcontents

\part{First Part}
\chapter{Introduction}
\chapter{This is a chapter with a very long title that will go the the second row}

\part{Very important second part}
\chapter{Another introduction}


\end{document}

1 Answer 1

2
\documentclass{book}
\usepackage[newparttoc]{titlesec}
\usepackage{titletoc}
\newlength\toclabelwidth
\setlength\toclabelwidth{2.2cm}
\titleformat{\part}
  [display]
  {\centering\bfseries\Huge}
  {\huge\partname~\thepart}
  {2ex}
  {}

\contentsmargin[2.5cm]{-30pt}

\titlecontents{part}
  [\toclabelwidth]
  {\normalfont\vspace{1.5em}}
  {\contentslabel[\hfill\partname~\thecontentslabel\quad]{\toclabelwidth}}
  {}
  {}
  [\titlerule\addvspace{.25em}]

\titlecontents{chapter}
  [\toclabelwidth]
  {\normalfont\filright}
  {\contentslabel[\hfill\bfseries\chaptername~\thecontentslabel\quad]{\toclabelwidth}}
  {}
  {\hfill\thecontentspage}
  [\addvspace{0.25em}]

\begin{document}

\tableofcontents

\part{First Part}
\chapter{Introduction}
\chapter{This is a chapter with a very long title that will go the the second row}

\part{Very important second part}
\chapter{Another introduction}


\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .