在没有 tocloft 包的情况下,在目录中的数字前添加“章节”或“附录”

在没有 tocloft 包的情况下,在目录中的数字前添加“章节”或“附录”

首先,MWE 及其输出如下。

\documentclass{book}

\begin{document}
\tableofcontents
\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\begin{appendix}
\chapter{Basic Concepts}
\section{Theory}
\end{appendix}
\end{document}

在此处输入图片描述

我想改进输出的两点:

  1. 在目录中的数字前添加“章节”或“附录”;
  2. 使章节和附录标题(不带章节或附录字样)或编号缩进相同。

我发现的两个问题的解决方案几乎都诉诸于tocloft不受欢迎的方案,例如

\usepackage{tocloft}%
\usepackage{calc}%widthof
%add Chapter before numbers in Contents
\renewcommand\cftchappresnum{\chaptername\space}
%change Chapter to Appendix before numbers in Contents
\makeatletter
\g@addto@macro\appendix{%
    \addtocontents{toc}{%
        \protect\renewcommand{\protect\cftchappresnum}{\appendixname\space}%
    }%
}
\makeatother
%make chapter and appendix titles without the words Chapter or Appendix or numbers be indent larger
\setlength{\cftchapnumwidth}{\widthof{\textbf{Appendix\space00\space}}}

那么有没有什么解决方案,不调用或者使用尽可能少的包来尽可能少地tocloft改变默认命令?\tableofconts

答案1

您可以修补该命令\@chapter和该命令\l@chapter

\documentclass{book}
\usepackage{etoolbox}
\newlength{\chapnwd}
\settowidth{\chapnwd}{\textbf{Appendix\space00\space}}
\makeatletter
\patchcmd{\@chapter}{\protect\numberline{\thechapter}#1}{\protect\numberline{\@chapapp\space\thechapter\space}#1}{\@latex@info{true}}{\@latex@info{false}}
\patchcmd{\l@chapter}{\setlength\@tempdima{1.5em}}{\setlength\@tempdima{\chapnwd}}{\@latex@info{true}}{\@latex@info{false}}
\makeatother
\begin{document}
\tableofcontents
\chapter{Basic Concepts}
\section{Theory}


\chapter{Basic Concepts}
\section{Theory}

\chapter*{test}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}


\begin{appendix}
\chapter{Basic Concepts}
\section{Theory}
\end{appendix}
\end{document}

在此处输入图片描述

答案2

您还没有明确说明“线路扩展”的含义。您还没有说明您的问题所在,tocloft所以我只能猜测这应该对您有用。

% tocloftprob.tex  SE 645714

\documentclass{book}

\usepackage%[titles] % this option does not reset the title spacing
    {tocloft}

  \addtolength{\cftchapnumwidth}{7em}  % space for Chapter/Appendix number
  \renewcommand{\cftchappresnum}{Chapter }
\begin{document}
\tableofcontents
\chapter{Basic Concepts}
\section{Theory}

\chapter{Basic Concepts}
\section{Theory}

\begin{appendix}
\addtocontents{toc}{\protect\renewcommand{\protect\cftchappresnum}{Appendix }}

\chapter{Basic Concepts}
\section{Theory}
\end{appendix}
\end{document}

相关内容