我想在我的文档类中提供两个目录,一个是中文的,另一个是英文的,ctexbook
这是目前编写中文书籍最常用的。这里我们收集了针对该问题的不同解决方案。
答案1
新的解决方案(2022-05-28)
新的解决方案@Tom 和我根据我的旧解决方案进行了改进,这可能是迄今为止最好的解决方案。
注意:新的解决方案提供了一种添加的方法更多其他语言的内容,即您可以通过定义不同的内容添加命令来添加法语、德语、希腊语等语言的内容,例如\entableofcontents
。
\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{M}\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}
\section{方法和材料}
\addentoc{section}{Methods and materials}
\begin{appendix}
\chapter{理论}
\addentoc{chapter}{Theory}
\end{appendix}
\end{document}
旧解决方案(2022-05-27)
我在引文中搜索类似的问题,并找到一个不受欢迎的解决方案(仍然要感谢@egreg)babel
由于该包没有选项,我无法使用它chinese
。而我通过改进那个不想要的解决方案得到了一个想要的解决方案。现在我把它发布在这里。
注意:此解决方案在以下情况下也很有用:添加另外两种语言的两部分内容(一个是主要语言,例如德语,另一个是另一种语言,例如英语或希腊语)并减少包裹数量。
\documentclass[a4paper]{ctexbook}
%%%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}
\section{方法和材料}
\addentoc{section}{Methods and materials}
\end{document}
XeLaTeX 对这两个内容的输出如下。
答案2
这是由@muzimuzhi Z 提供的另一种解决方案,来自链接。注意,该tocloft
包是在解决方案中使用的。
% ref: https://tex.stackexchange.com/a/170963/79060
\documentclass{ctexbook}
\usepackage[titles]{tocloft}
\usepackage{xpatch}
\makeatletter
\ExplSyntaxOn
\def\en@toc@ext{tec}
\def\en@contentsname{Contents}
%% two user commands
\newcommand\enTableOfContents{
\begingroup
% restore \contentsname
\let\contentsname\en@contentsname
% restore behavior of \numberline
\let\CTEX@toc@width@n\@gobble
% adjust numwidth (required by long section numbers)
\addtolength\cftchapnumwidth{.3em}
\addtolength\cftsecnumwidth{.5em}
\addtolength\cftsubsecnumwidth{.8em}
% output en-toc
\@enTableOfContents
\endgroup
}
\newcommand\enSectionTitle[1]{
\def\@currentTitle{#1}
\ignorespaces
}
%% provide \@enTableOfContents
\let\@enTableOfContents\tableofcontents
\@ifundefined {chapter}
{ % e.g., article.cls
\xpatchcmd\@enTableOfContents
{\@starttoc{toc}}
{\endgroup\@starttoc{\en@toc@ext}}
{}{\fail}
}
{ % e.g., book.cls
\xpatchcmd\@enTableOfContents
{\chapter}
{
\begingroup
% restore to ctex's "scheme=plain"
\ctexset{
chapter/format = \Huge\bfseries\raggedright
}
\chapter
}
{}{\fail}
\xpatchcmd\@enTableOfContents
{\@starttoc{toc}}
{\endgroup\@starttoc{\en@toc@ext}}
{}{\fail}
}
%% modify to write two toc entries
\cs_set_protected:Npn \CTEX@addtocline #1#2
{
% write cn-toc entry
\addcontentsline { toc } {#1}
{ \use:c { CTEX@#1@tocline } {#1} {#2} }
\begingroup
% restore to ctex's "scheme=plain"
\ctexset{
part/name = {Part\space,},
part/number = \Roman{part},
}
\@ifundefined {chapter}
{}
{
\ctexset{
chapter/name = {,},
chapter/number = \arabic{chapter}
}
}
% write en-toc entry
\addcontentsline \en@toc@ext {#1}
{ \use:c { CTEX@#1@tocline } {#1} { \@currentTitle } }
\endgroup
}
\ExplSyntaxOff
\makeatother
\begin{document}
\tableofcontents
\enTableOfContents
\enSectionTitle{Main body}
\part{正文}
\enSectionTitle{Introduction}
\chapter{介绍}
\setcounter{chapter}{10}
\enSectionTitle{Introduction 2}
\chapter{介绍}
\enSectionTitle{Method and materials}
\section{方法和材料}
\setcounter{section}{9}
\enSectionTitle{Method and materials 2}
\section{方法和材料 2}
\enSectionTitle{Data compilation}
\subsection{数据搜集}
\setcounter{subsection}{9}
\enSectionTitle{Data compilation 2}
\subsection{数据搜集 2}
\setcounter{part}{12}
\enSectionTitle{Appendix}
\part{附录}
\end{document}