我正在尝试在目录中添加简短的章节描述,这些描述已在其他地方介绍过对于书籍 documentclass或者加上额外的包裹。我不想安装任何新软件包,我好奇的是为什么这对文章文档类来说会失败。
定义类似\newcommand{\summary}[1]{\addtocontents{toc}{#1}
几乎可以工作,但是在Something's wrong--perhaps a missing \item
新的之前使用时会神秘地失败\section
(请参阅下面的 MWE)。
目前,我正在通过使用回忆录文档类来解决这个问题(使用一些命令和[sub]\cftsetindents
的更新来使 ToC 缩进更像文章),但我想知道为什么这对文章类不起作用。\cft
sectionaftersnumb
\documentclass{article}
%% Neither of these work:
\newcommand{\summary}[1]{\addtocontents{toc}{#1}}
% \newcommand{\summary}[1]{\addcontentsline{toc}{sectioninfo}{#1}}
%% This one works, but doesn't look right (I don't want page numbers or dotted
%% spacefilling for \summary entries.), and besides, using
%% 'subsubsection' rather than 'sectioninfo' feels Wrong.
% \newcommand{\summary}[1]{\addcontentsline{toc}{subsubsection}{#1}}
\begin{document}
\tableofcontents
\section{First Sec}
\summary{This is fine.}
\subsection{A subsec}
\subsubsection{A subsubsec}
\subsection{Another subsec}
\summary{This causes problems}
\section{Second Sec}
\subsection{further subsecs}
\summary{This is fine.}
\subsection{further subsecs}
\summary{This is fine.}
\end{document}
答案1
问题是\contentsline{section}{...}{...}
,\addpenalty
如果您不结束该段落,则会发出非法问题。
如果你注意到,你的.toc
文件将会
\contentsline {section}{\numberline {1}First Sec}{1}
This is fine.
\contentsline {subsection}{\numberline {1.1}A subsec}{1}
\contentsline {subsubsection}{\numberline {1.1.1}A subsubsec}{1}
\contentsline {subsection}{\numberline {1.2}Another subsec}{1}
This causes problems
\contentsline {section}{\numberline {2}Second Sec}{1}
\contentsline {subsection}{\numberline {2.1}further subsecs}{1}
This is fine.
\contentsline {subsection}{\numberline {2.2}further subsecs}{1}
This is fine.
“这会导致问题”这句话后面紧接着\contentsline{section}
,因此\addpenalty
会出现在段落中间。如果文本后面跟着\contentsline{subsection}
条目,则不会发生这种情况,因为这些条目不会发出\addpenalty
。
简单的解决方案:
\documentclass{article}
\newcommand{\summary}[1]{\addtocontents{toc}{#1\par}}
\begin{document}
\tableofcontents
\section{First Sec}
\summary{This is fine.}
\subsection{A subsec}
\subsubsection{A subsubsec}
\subsection{Another subsec}
\summary{This causes problems}
\section{Second Sec}
\subsection{further subsecs}
\summary{This is fine.}
\subsection{further subsecs}
\summary{This is fine.}
\end{document}