5

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.

  • Welcome to TeX.SX! Do you really want to have "List of Figures" two times on your page? I think that looks very strange. Maybe you explain a bit, how the appendix should look like. Like this, there is a bit too much "clearpaging" and "doubling". What do you mean with "part" here? – LaRiFaRi Aug 11 '14 at 13:17
  • @mike Your code modifies the behaviour of \part but \part is not used at all. Could you please provide an example where both the \part command is used, and the issue you need to solve is present? Then it'll be much easier to solve the problem. And please don't be rude. We're not paid to help you here, and even if we were, it's not a reason for rudeness. – yo' Aug 12 '14 at 12:05
  • my doc has two parts, so I use \part{part 1} and \part{part 2} inside it. What it does to them is it makes their headings(part 1 and part 2) NOT show on a separate page, which is default. But the code also makes my appendices not appear right in the toc and I suspect the chapter parts in that code are causing this. – mike Aug 12 '14 at 12:08
4

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

  • Thank you so much. I just tried your solution and it works exactly as I wanted it. But I need to ask just to be safe: this code solves the problem between appendix and titlesec and that is it, right? I mean could it potentially introduce other problems, like the code I was using did? I looked at the document a few times and all seems fine, but I am asking because I don't know latex that well. Anyway, I really appreciate the effort and thanks again! – mike Aug 12 '14 at 16:11
  • @mike Any time you use packages which both do similar kinds of things there can be problems. The appendix package assumes the standard titling commands, and titlesec obviously changes them, so it's not too surprising that they might have some interaction. But if your document comes out the way you want, then you should be fine and I wouldn't worry about other problems. – Alan Munn Aug 12 '14 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.