![在没有 tocloft 包的新 \entableofcontents 中,在数字前添加“章节”或“附录”](https://linux22.com/image/450645/%E5%9C%A8%E6%B2%A1%E6%9C%89%20tocloft%20%E5%8C%85%E7%9A%84%E6%96%B0%20%5Centableofcontents%20%E4%B8%AD%EF%BC%8C%E5%9C%A8%E6%95%B0%E5%AD%97%E5%89%8D%E6%B7%BB%E5%8A%A0%E2%80%9C%E7%AB%A0%E8%8A%82%E2%80%9D%E6%88%96%E2%80%9C%E9%99%84%E5%BD%95%E2%80%9D.png)
首先,MWE 及其输出\entableofcontents
如下。
\documentclass[a4paper]{ctexbook}
\ctexset{chapter/number=\arabic{chapter}}%change number format in Chinese \tableofcontents
\usepackage{etoolbox}%for \pathcmd
\usepackage{calc}%\widthof
\makeatletter
%add "Chapter" or "Appendix" before numbers in \entableofontents
%make the chapter and appendix titles without the words Chapter or Appendix or numbers be indent the same in \entableofcontents
\patchcmd{\l@chapter}{\setlength\@tempdima{1.5em}}{\setlength\@tempdima{\widthof{\textbf{附录\space{M}\space}}}}{\@latex@info{true}}{\@latex@info{false}}
\makeatother
%%%define \entableofcontents%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\newcommand{\encontentsname}{Contents}
\newcommand\entableofcontents{%
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{%
\encontentsname
\@mkboth{\MakeUppercase\encontentsname}
{\MakeUppercase\encontentsname}%
}%
\@starttoc{entoc}%
\if@restonecol\twocolumn\fi
}
\newcommand{\addentoc}[2]{%
\addcontentsline{entoc}{#1}{\protect\numberline{\csname the#1\endcsname}#2}%
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\frontmatter
\tableofcontents
\entableofcontents
\mainmatter
\chapter*{凡例}
\addcontentsline{toc}{chapter}{凡例}
\addcontentsline{entoc}{chapter}{Styles}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\section{方法和材料}
\addentoc{section}{Methods and materials}
\begin{appendix}
\chapter{理论}
\addentoc{chapter}{Theory}
\end{appendix}
\end{document}
如你所见,为了生成中文内容的英文内容,我定义了命令,\entableofcontents
其想法来自于@egreg。\entableofcontents
有没有办法在没有 tocloft 包的情况下在新版本中在数字前添加“章节”或“附录” ?
答案1
这是一个解决方案:
\documentclass[a4paper]{ctexbook}
\ctexset{chapter/number=\arabic{chapter}}%change number format in Chinese \tableofcontents
\usepackage{etoolbox}%\apptocmd
\usepackage{calc}%\widthof
%%%define \entableofcontents%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\newcommand{\encontentsname}{Contents}
\newcommand\entableofcontents{%
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{%
\encontentsname
\@mkboth{\MakeUppercase\encontentsname}
{\MakeUppercase\encontentsname}%
}%
\@starttoc{entoc}%
\if@restonecol\twocolumn\fi
}
\newcommand{\enchapapp}{Chapter}
\apptocmd{\appendix}{\renewcommand{\enchapapp}{Appendix}}{}{}
\newcommand{\addentoc}[2]{%
\ifstrequal{#1}{chapter}{%
\addcontentsline{entoc}{#1}{\setlength\@tempdima{\widthof{\textbf{Appendix\space{00}\space}}}\protect\numberline{\enchapapp\space\thechapter}#2}}%
{\addcontentsline{entoc}{#1}{\protect\numberline{\csname the#1\endcsname}#2}}%
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\frontmatter
\tableofcontents
\entableofcontents
\mainmatter
\chapter*{凡例}
\addcontentsline{toc}{chapter}{凡例}
\addcontentsline{entoc}{chapter}{Styles}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\chapter{引言}
\addentoc{chapter}{Introduction}
\section{方法和材料}
\addentoc{section}{Methods and materials}
\begin{appendix}
\chapter{理论}
\addentoc{chapter}{Theory}
\end{appendix}
\end{document}