这是对以下问题的回答的后续问题分页后重新显示章节标题
与后续问题一样,我想在分页符后重复章节标题,但仅限于分页符与章节结尾不对应的情况。
我尝试了 Werner 的答案中提出的代码,但是当延伸到下一页的长部分应该从页面末尾开始但 LaTeX 将其完全移动到下一页时,它的性能并不好,例如因为在部分开始后立即插入了一个长表格,如下面的代码所示:
\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\let\@section@title@\relax% Sectional heading storage
\patchcmd{\@sect}% <cmd>
{\@xsect}% <search>
{\gdef\@section@title@{% Store sectional heading
{\noindent#6\@svsec#8\normalfont\ \smash{(continued)}}\par\bigskip}\@xsect}% <replace>
{}{}% <success><failure>
\EveryShipout{%
\ifdim\pagetotal>\pagegoal% There is content overflow on this page
\aftergroup\@section@title@% Reprint/-insert sectional heading
\fi%
}
\makeatother
\begin{document}
\section{A section}\lipsum[1-4]
\section{Second section}
\begin{tabular}{l}
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
\end{tabular}
\par\lipsum[7-14]
\end{document}
在这种情况下,第二页在正常标题“2 第二节”之前错误地插入了“2 第二节(续)”:
答案1
以下解决方案基本上修补了tabular
环境以测量表格的高度,并在实际黑客攻击中执行更多测试以避免错误行为。我在代码中留下了有关单个步骤的更多细节,就像@Werner 的原始解决方案一样。
代码
\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{environ}% http://www.ctan.org/pkg/environ
\makeatletter
\let\@section@title@\relax% Sectional heading storage
\patchcmd{\@sect}% <cmd>
{\@xsect}% <search>
{\gdef\@section@title@{% Store sectional heading
{\noindent#6\@svsec#8\normalfont\ \smash{(continued)}}\par\bigskip}\@xsect}% <replace>
{}{}% <success><failure>
\newif\if@sectionpage %Conditional to see if we are on the page where a section has been started
\newif\if@tab@pagebreak %Conditional to check if a table will cause a pagebreak
\newdimen\currtabheight
\newdimen\pt@saved
\let\ltx@tabular\tabular
\let\ltx@endtabular\endtabular
\providecommand{\env@tabular@save@env}{}
\providecommand{\env@tabular@process}{}
%Patching the tabular envionment
\RenewEnviron{tabular}[1]{%
\setbox0=\hbox{% box the tabular to measure the height later
\ltx@tabular{#1}
\BODY
\ltx@endtabular
}
\currtabheight=\ht0 \advance\currtabheight by \dp0%
\pt@saved=\the\pagetotal% store the current pagetotal value
\advance\pt@saved by \currtabheight% add the height of the current tabular
\ifdim\pt@saved>\pagegoal% the tabular will cause a pagebreak
\global\@tab@pagebreaktrue
\fi
\ltx@tabular{#1}% printing the tabular
\BODY
\ltx@endtabular
}
%Doing the old shipout trick with the addtional declared conditionals
\EveryShipout{%
\ifdim\pagetotal>\pagegoal
\if@sectionpage
\if@tab@pagebreak\else\aftergroup\@section@title@\fi
\else
\aftergroup\@section@title@
\fi
\fi
\global\@sectionpagefalse
\global\@tab@pagebreakfalse}
%redefinition to make the sectionpage-switch work
\let\ltx@section=\section
\renewcommand{\section}{\@sectionpagetrue\ltx@section}
\makeatother
\begin{document}
\section{A section}\lipsum[1-4]
\section{Second section}
\begin{tabular}{l}
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA \\
AAAAAAAAAAAAAAAAAAAA
\end{tabular}
\lipsum[7-14]
\end{document}
输出
评论
如果您使用除表格之外的其他不可破坏材料,则可能会再次出现错误。这些宏必须像环境一样进行修补tabular
。如果是这种情况,也许只需发表评论即可。