类似的问题已经被问过很多次了,也得到过很多次回答(例如这里,这里, 和这里),但所有答案似乎都有以下错误:如果目录 (TOC) 的最后一页只有一个项目,则不会出现所需的标题。
例如,给出的解决方案这里使用atbegshi
包。下面的最小工作示例 (MWE) 实现了这个解决方案,它确实会做它应该做的事情,因为 TOC 的最后一页上会出现多个项目:
\documentclass[letterpaper]{report}
\usepackage{tocloft}
\usepackage{atbegshi}% http://ctan.org/pkg/atbegshi
\makeatletter
\newcommand{\tocheader}{{%
\bfseries% Font selection
\hspace*{\cftchapindent}% Chapter indent
\makebox[\cftchapnumwidth][l]{Chapter no}% Chapter number heading
\hfill%
\contentsname% Title heading
\hfill%
\makebox[\@pnumwidth][r]{Page}% Page heading
\par\kern.5\baselineskip% After heading
}}
\AtBeginDocument{\renewcommand{\tableofcontents}{%
\begingroup%
\parindent\z@\parskip\cftparskip%
\tocheader%
\@starttoc{toc}%
\endgroup%
}}
\renewcommand{\cftaftertoctitle}{\par\kern.5\baselineskip\tocheader}
\newcommand{\AtBeginShipoutClear}{\gdef\AtBegShi@Hook{}}
\makeatother
\begin{document}
\AtBeginShipout{\tocheader}
\tableofcontents
\AtBeginShipoutClear
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\end{document}
这是分页符处的输出,在目录第二页的开头显示所需的标题“章节无目录页”:
但是现在只需注释掉\chapter{Something}
上面代码中的最后一行;结果将是这样的:
标题(即“章节无目录页”行)消失了!
使用以下解决方案时也会出现同样的问题afterpage
包(如这里);如果直接破解程序,也会发生这种情况\output
,例如这个乳胶类。
任何想法如何解决这一问题?
答案1
添加\leavevmode
,\AtBeginShipoutClear
强制我们回到水平模式,解决问题:
\documentclass[letterpaper]{report}
\usepackage{tocloft}
\usepackage{atbegshi}% http://ctan.org/pkg/atbegshi
\makeatletter
\newcommand{\tocheader}{{%
\bfseries% Font selection
\hspace*{\cftchapindent}% Chapter indent
\makebox[\cftchapnumwidth][l]{Chapter no}% Chapter number heading
\hfill%
\contentsname% Title heading
\hfill%
\makebox[\@pnumwidth][r]{Page}% Page heading
\par\kern.5\baselineskip% After heading
}}
\AtBeginDocument{\renewcommand{\tableofcontents}{%
\begingroup%
\parindent\z@\parskip\cftparskip%
\tocheader%
\@starttoc{toc}%
\endgroup%
}}
\renewcommand{\cftaftertoctitle}{\par\kern.5\baselineskip\tocheader}
\newcommand{\AtBeginShipoutClear}{\leavevmode\gdef\AtBegShi@Hook{}}
\makeatother
\begin{document}
\AtBeginShipout{\tocheader}
\tableofcontents
\AtBeginShipoutClear
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
\chapter{Something}
%\chapter{Something}
\end{document}