0

I've searched for days and unfortunately could not find the solution to my problem.

Consider the following minimal example, it produces a ToC as follows:

Undesirable ToC

This is not the one I need. I need the following ToC:

Contents

   Chapter 1: Ch1   .............................. 3 
           1.1: Sec 1    .............................. 3 
           1.2: Sec 2    .............................. 4 
   Chapter 2: Ch2   .............................. 7 
           2.1: Sec 1    .............................. 7 
           2.2: Sec 2    .............................. 8
Appendix A: App1 .............................. 11
           A.1: Sec 1    .............................. 11
           A.2: Sec 2    .............................. 12
Appendix B: App2 .............................. 15
           B.1: Sec 1    .............................. 15
           B.2: Sec 2    .............................. 16

I have tried the following packages, but could not make them work: tocloft, titletoc and titlesec. I use TeXLive 2017 (XeLaTeX).

Minimal Code:

\documentclass[a4paper,10pt,twoside]{book}
\usepackage{lipsum} % for dummy text only

\begin{document}
    \tableofcontents

    \chapter{Ch1}
        \section{Sec1}
            \lipsum
        \section{Sec2}
            \lipsum
    \chapter{Ch2}
        \section{Sec1}
            \lipsum
        \section{Sec2}
            \lipsum
    \appendix
    \chapter{App1}
        \section{Sec1}
            \lipsum
        \section{Sec2}
            \lipsum
    \chapter{App2}
        \section{Sec1}
            \lipsum
        \section{Sec2}
            \lipsum 


\end{document}
0

1 Answer 1

2

This is based on this answer but adds \l@appendix for appendices. Specifically, it modifies the \@chapter macro from book to use \chapterhandler instead of chapter in \addcontentsline. One must reset it to appendix manually (unless you also want to modify the \appendix definition).

\documentclass[a4paper,10pt,twoside]{book}
\usepackage{lipsum} % for dummy text only
%\usepackage{appendix}

\makeatletter
    \renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \settowidth{\@tempdimb}{\normalsize\bfseries \chaptername~}%
      \advance\leftskip by \@tempdimb
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@tempdima
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      \chaptername~#1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
      \penalty\@highpenalty
    \endgroup
  \fi}

\newcommand*\l@appendix[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \settowidth{\@tempdimb}{\normalsize\bfseries \appendixname~}%
      \advance\leftskip by \@tempdimb
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@tempdima
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      \appendixname~#1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
      \penalty\@highpenalty
    \endgroup
  \fi}

\newcommand{\chapterhandler}{chapter}

\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{\chapterhandler}%
                                   {\protect\numberline{\thechapter}#1}%
                       \else
                         \addcontentsline{toc}{\chapterhandler}{#1}%
                       \fi
                    \else
                      \addcontentsline{toc}{\chapterhandler}{#1}%
                    \fi
                    \chaptermark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi}
\makeatother

\begin{document}
    \tableofcontents

    \chapter{A very long chapter title just to test the hanging indentation}
        \section{Sec1}
            \lipsum
        \section{Sec2}
            \lipsum
    \chapter{Ch2}
        \section{Sec1}
            \lipsum
        \section{Sec2}
            \lipsum
    \appendix
    \renewcommand{\chapterhandler}{appendix}
    \chapter{A very long appendix title just to test the hanging indentation}
        \section{Sec1}
            \lipsum
        \section{Sec2}
            \lipsum
    \chapter{App2}
        \section{Sec1}
            \lipsum
        \section{Sec2}
            \lipsum 

\end{document}

You must log in to answer this question.

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