在没有 tocloft 包的新 \entableofcontents 中,在数字前添加“章节”或“附录”

在没有 tocloft 包的新 \entableofcontents 中,在数字前添加“章节”或“附录”

首先,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}

在此处输入图片描述

相关内容