[Tex/LaTex] How to make appendix appear in part level of table of contents

appendicestable of contents

In a book documentclass I would like to have an appendix which appears in the table of contents at the part level. The TOC should look like this:

Preface
Part title
  Chapter title
  Chapter title
Part title
  Chapter title
  Chapter title
Appendix: Some title
Bibliography
Index

I know how to include the bibliography and the index with \addcontentsline{toc}{part}{...} and I even know about \phantomsection trick to get it to point to the right place in the PDF.

But how do I convince the appendix to go to the part level of TOC? It goes under chapter of the last part.

I do not care if I make an appendix "by hand", i.e., without using \appendix.
By the way, I am using \titlesec to customize chapter and part formatting.

Best Answer

Since you have only one appendix, a "one shot hack" should be sufficient:

\documentclass{book}

\usepackage{etoolbox}
\makeatletter
\newcommand{\appchapter}[1]{%
  \begingroup
  \patchcmd{\@chapter}
   {\addcontentsline{toc}{chapter}}
   {\addcontentsline{toc}{part}}
   {}{}
  \patchcmd{\@chapter}
   {\addcontentsline{toc}{chapter}}
   {\addcontentsline{toc}{part}}
   {}{}
  \chapter{#1}
  \endgroup
}
\makeatother

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

\chapter{Preface}

\part{Part title}
\chapter{Chapter title}
\chapter{Chapter title}

\part{Part title}
\chapter{Chapter title}
\chapter{Chapter title}

\backmatter
\appendix
\appchapter{Appendix: Some title}

\chapter{Bibliography}

\end{document}

enter image description here

Related Question