我们绝对可以\phantomsection \addcontentsline{toc}{chapter}{<name>}
在开始环境后立即使用或键入相关宏,其中包括目录中的摘要、图表列表和表格列表。有没有办法修补这些命令,以便我们不再需要键入\phantomsection \addcontentsline{toc}{chapter}{<name>}
?
这个问题的目的是教我如何用一个例子来修补命令,并不是我不想手动使用\phantomsection
和\addcontentsline
\documentclass[notitlepage]{book}
\usepackage[no-math]{fontspec}
\usepackage[inline]{enumitem}
\usepackage{blindtext}
\usepackage[showframe]{geometry}
\usepackage{tocloft}
\usepackage{etoolbox}
\patchcmd{\frontmatter}{\pagenumbering{roman}}{\pagenumbering{Roman}}{}{}
% ===== Define abstract environment =====
\makeatletter
\if@titlepage
\newenvironment{abstract}{%
\titlepage
\null\vfil
\@beginparpenalty\@lowpenalty
\begin{center}%
\bfseries \abstractname
\@endparpenalty\@M
\end{center}}%
{\par\vfil\null\endtitlepage}
\else
\newenvironment{abstract}{%
\if@twocolumn
\section*{\abstractname}%
\else
\small
\begin{center}%
{\bfseries \abstractname\vspace{-.5em}\vspace{\z@}}%
\end{center}%
\quotation
\fi}
{\if@twocolumn\else\endquotation\fi}
\fi
\makeatother
\begin{document}
\frontmatter
\pagestyle{plain}
\tableofcontents
\pagestyle{headings}
\cleardoublepage
\begin{abstract}
\thispagestyle{plain}
\blindtext
\end{abstract}
\phantomsection \addcontentsline{toc}{chapter}{\abstractname}
\cleardoublepage
\phantomsection \addcontentsline{toc}{chapter}{\listfigurename}
\listoffigures
\cleardoublepage
\phantomsection \addcontentsline{toc}{chapter}{\listtablename}
\listoftables
\mainmatter
\Blinddocument
\Blinddocument
\Blinddocument
\end{document}
答案1
有没有什么abstract
为该类进行内部修补book
——根本没有这样的环境book
。
如果使用report
或,则可以选择应用 Peter Wilson/Will Robertson 的软件包。article
abstract
addtotoc
如果没有abstract
环境,则必须以自由的方式定义它,或者必须从前面提到的类中复制代码\abstractname
,然后说\usepackage[addtotoc]{abstract}
。
解决方案检查类是否是report
或article
其他类并定义abstract
环境,然后加载abstract
包以使其有机会修改环境。
图表和表格的目录条目可以通过tocbibind
以下方式实现:约翰内斯·B。
\documentclass[notitlepage]{book}
\usepackage[no-math]{fontspec}
\usepackage[inline]{enumitem}
\usepackage{blindtext}
\usepackage[showframe]{geometry}
\usepackage{tocbibind}
\usepackage{tocloft}
\usepackage{etoolbox}
\patchcmd{\frontmatter}{\pagenumbering{roman}}{\pagenumbering{Roman}}{}{}
% ===== Define abstract environment =====
\makeatletter
\@ifclassloaded{report}{%
% Do nothing, everything seems to be setup already
}{\@ifclassloaded{article}{%
% Do nothing, everything seems to be setup already
}{%
% Any other class that does not provide an abstract environment
% We have to define `\abstractname` and the abstract environment (most likely)
% After that, load the abstract package
\@ifundefined{abstractname}{\newcommand{\abstractname}{Abstract}}{}
\@ifundefined{abstract}{%
\if@titlepage
\newenvironment{abstract}{%
\titlepage
\null\vfil
\@beginparpenalty\@lowpenalty
\begin{center}%
\bfseries \abstractname
\@endparpenalty\@M
\end{center}}%
{\par\vfil\null\endtitlepage}
\else
\newenvironment{abstract}{%
\if@twocolumn
\section*{\abstractname}%
% \addcontentsline{toc}{section}{\protect\abstractname} Or add it here....
\else
\small
\begin{center}%
{\bfseries \abstractname\vspace{-.5em}\vspace{\z@}}%
\end{center}%
\quotation
\fi}
{\if@twocolumn\else\endquotation\fi}
\fi
% Eventually a check about the existence of \endabstract, i.e. \@ifundefined{endabstract}{}{}
}{%
% abstract environment presumably exists
}
}% End of \@ifclassloaded{article}
}% End of \@ifclassloaded{report}
\makeatother
\usepackage[addtotoc]{abstract}
\begin{document}
\frontmatter
\pagestyle{plain}
\tableofcontents
\pagestyle{headings}
\cleardoublepage
\begin{abstract}
\thispagestyle{plain}
\blindtext
\end{abstract}
\cleardoublepage
\listoffigures
\cleardoublepage
\listoftables
\mainmatter
\Blinddocument
\Blinddocument
\Blinddocument
\end{document}