格式化目录,使每个章节前面都有章节,每个附录前面都有附录

格式化目录,使每个章节前面都有章节,每个附录前面都有附录

我搜索了好几天,但不幸的是找不到解决我的问题的方法。

考虑以下最小示例,它生成如下 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

我尝试过以下软件包,但无法使它们工作:toclofttitletoctitlesec。我使用TeXLive 2017 (XeLaTeX)

最小代码:

\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}

答案1

这是基于这个答案但添加了\l@appendix附录。具体来说,它将\@chapter书中的宏修改为使用\chapterhandler而不是chapter\addcontentsline。必须appendix手动将其重置为(除非您还想修改\appendix定义)。

\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}

相关内容