[Tex/LaTex] Problem with titlesec and appendix

appendicestitlesec

I am using the titlesec package and the code under the usepackage to make parts appear on the same page as chapters, not on an individual page. Sorry I don't remember where I got the code from, but it does what I want. The problem is it also does something to the way appendices appear in toc. I want "Appendix A" in toc, but using the code for titlesec makes appendices appear without "Appendix" in toc. I tested and I know for sure this is the cause. Please suggest some modification so that appendices appear "Appendix A" in toc and parts stay on the same page as chapters. Here is some outline code for my document.

\documentclass[titlepage, oneside, 12pt]{report}

\usepackage[titletoc]{appendix}

\usepackage[nottoc, notlof, notlot]{tocbibind}

\renewcommand*\listfigurename{}
\renewcommand*\listtablename{}

\usepackage{titlesec}
\titleclass{\part}{top}
\titleformat{\part}[display]
  {\normalfont\huge\bfseries}{\centering\partname\ \thepart}{20pt}{\Huge\centering}
\titlespacing*{\part}{0pt}{50pt}{40pt}
\titleclass{\chapter}{straight}
\titleformat{\chapter}[display]
  {\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge}
\titlespacing*{\chapter} {0pt}{50pt}{40pt}


\begin{document}

\begingroup
\setcounter{tocdepth}{2}
\tableofcontents{}
\endgroup

\part{part 1}
\chapter{chapter one}

\part{part 2}
\chapter{chapter two}


\clearpage
\begin{appendices}
\chapter{List of figures}
\listoffigures
\clearpage
\chapter{List of tables}
\listoftables
\end{appendices}

\end{document}

Here is what happens to the appendices in toc with the code between \usepackage{titlesec} and \begin{document} left as it is:

Here is what happens to the appendices in toc with the code between \usepackage{titlesec} and \begin{document} left as it is:

And here is what the appendices in toc look like when I remove that code. This is the way I would like them to be displayed

And here is what the appendices in toc look like when I remove that code

I would like someone to modify this code so that it still keeps parts from having a separate page and makes appendices show up properly in toc. Thanks.

Best Answer

The problem is the combination of the appendix package (which redefines the \addcontentsline) and titlesec, so you need to modify the code a bit to make it aware of \titlesec. Here's a solution to your problem:

The redefinition is here. Add this to your preamble after your titlesec definitions.

\makeatletter
\renewcommand{\@redotocentry@pp}[1]{%
  \let\oldacl@pp=\addcontentsline
  \def\addcontentsline##1##2##3{%
    \def\@pptempa{##1}\def\@pptempb{toc}%
    \ifx\@pptempa\@pptempb
      \def\@pptempa{##2}\def\@pptempb{#1}%
      \ifx\@pptempa\@pptempb
        \oldacl@pp{##1}{##2}{\appendixname\space ##3}%
      \else
        \oldacl@pp{##1}{##2}{\chaptertitlename\space ##3}% added \chaptertitlename
      \fi
    \else
      \oldacl@pp{##1}{##2}{##3}%
    \fi}
}
\makeatother

Here's the complete document:

\documentclass[titlepage, oneside, 12pt]{report}

\usepackage[titletoc]{appendix}

\usepackage[nottoc, notlof, notlot]{tocbibind}

\renewcommand*\listfigurename{}
\renewcommand*\listtablename{}

\usepackage{titlesec}
\titleclass{\part}{top}
\titleformat{\part}[display]
  {\normalfont\huge\bfseries}{\centering\partname\ \thepart}{20pt}{\Huge\centering}
\titlespacing*{\part}{0pt}{50pt}{40pt}
\titleclass{\chapter}{straight}
\titleformat{\chapter}[display]
  {\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge}
\titlespacing*{\chapter} {0pt}{50pt}{40pt}

\makeatletter
\renewcommand{\@redotocentry@pp}[1]{%
  \let\oldacl@pp=\addcontentsline
  \def\addcontentsline##1##2##3{%
    \def\@pptempa{##1}\def\@pptempb{toc}%
    \ifx\@pptempa\@pptempb
      \def\@pptempa{##2}\def\@pptempb{#1}%
      \ifx\@pptempa\@pptempb
        \oldacl@pp{##1}{##2}{\appendixname\space ##3}%
      \else
        \oldacl@pp{##1}{##2}{\chaptertitlename\space ##3}% added \chaptertitlename
      \fi
    \else
      \oldacl@pp{##1}{##2}{##3}%
    \fi}
}
\makeatother

\begin{document}

\begingroup
\setcounter{tocdepth}{2}
\tableofcontents{}
\endgroup

\part{part 1}
\chapter{chapter one}

\part{part 2}
\chapter{chapter two}


\clearpage

\begin{appendices}
\chapter{List of figures}

\listoffigures
\clearpage
\chapter{List of tables}
\listoftables
\end{appendices}
\end{document}

output of TOC

Related Question