使用文章类,我有一个命令\newcommand{\sectionbreak}{\clearpage}
(通过titlesec
包;参见这个答案) 会在每个新部分之前清除页面,以便从新页面开始。我现在遇到一种情况,即上一节仅包含一个长表(由 生成ltablex
)。但是,在这种情况下,下一节不会从新页面开始,而是从多页表停止的地方开始。似乎只有\sectionbreak
在上一节中有文本内容并且长表不会触发 时才适用\sectionbreak
。
如何强制分节符强制将各节放在新页面上,即使前几节不包含内容?物理上放在\newline
表格后面(并保持\sectionbreak
命令原样)似乎可行,但我想要一种不需要知道这些情况何时发生并自动处理它们的解决方案。
最小工作示例
\documentclass{article}
\usepackage{titlesec} % Used to specify new section properties
\usepackage[table,dvipsnames]{xcolor} % Provides coloring for tables and text
\usepackage{ltablex} % Customized table formatting (uses tabularx and longtable)
\usepackage{environ} % Used to define custom table environment
% Used to split the header from the table content
\makeatletter
\long\def\split@doublebar#1\\#2\@nil{%
\def\doctableheader{#1}%
\def\doctablebody{#2}%
}
% Defines an environment to create a table in the document according to
% a common formatting. This uses the environ package.
\NewEnviron{doctable}[1]{%
\centering
\expandafter\split@doublebar\BODY\@nil % Split the body into the header and the content
\rowcolors{3}{black!5}{black!15}
\begin{tabularx}{\textwidth}{L{0.5}|L{0.5}<{\clearrow}}
\caption{#1} \\ % Include the caption at the top of the table
\hline
\rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
\doctableheader \\
\endfirsthead % Only show the caption for the first page
\hiderowcolors \caption{Continued} \\ % Include a caption on subsequent pages that just says continued
\showrowcolors % row coloring was turned off for the caption, now lets turn it back on
\hline
\rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
\doctableheader
\endhead % End of header for all pages after first
\doctablebody
\hline
\end{tabularx}
\label{SomeLabel}
}
% Fixes conflict between variable names in tabularx and xcolor
\newcounter{tblerows}
\expandafter\let\csname c@tblerows\endcsname\rownum
% Define various table properties and commands
\definecolor{greyblue}{rgb}{0.6353,0.6863,0.7686} % Define a color used in the tables
\newcommand\setrow[1]{\gdef\rowmac{#1}#1\ignorespaces} % Used for making a row bold
\newcommand\clearrow{\global\let\rowmac\relax} \clearrow % Used for clearing a row formatting
\newcolumntype{L}[1]{>{\hsize=#1\hsize\rowmac\raggedright\arraybackslash}X} % Left-aligned column, input is relative width of page
\keepXColumns % Option from ltablex which keeps tabularx column sizes
% Command from titlesec to force new sectons on new pages
\newcommand{\sectionbreak}{\clearpage} % Force sections to start on new pages
\begin{document}
\section{First Section}
\begin{doctable}{Some caption}
Header 1 & Header 2 \\
Content & Content \\
Content & Content \\
Content & Content \\
\end{doctable}
\section{Second Section}
This section is still on the same page as the First Section
\section{Third Section}
This section is on a new page.
\end{document}
答案1
longtable
因此,如果紧跟在章节标题之后,tabularx
withltablex
就不会重置为 false。\if@nobreak
因此,如果你有
\section{Title}
\begin{tabularx}{\textwidth}{cX}
<long table>
\end{tabularx}
\section{Title}
当 LaTeX 扫描到第二个\section
命令时,它看到的\if@nobreak
设置为 true (每个分段命令都会设置它,以避免两个连续标题之间出现分页符)。
您可以修补\endlongtable
问题\@nobreakfalse
(其在全球范围内有效)。
\documentclass{article}
\usepackage{titlesec,ltablex}
\usepackage{etoolbox}
\newcommand{\sectionbreak}{\clearpage}
\makeatletter
\appto\endlongtable{\@nobreakfalse\everypar{}}
\makeatother
\begin{document}
\section{Title 1}
text
\section{Title 2}
\begin{tabularx}{\textwidth}{cX}
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
abc & def \\
\end{tabularx}
\section{Title 3}
text
\end{document}
答案2
您可以在使用时重新定义\section
命令并包含。只需添加(在序言中):\cleardoublepage
\section
\let\stdsection\section
\renewcommand\section{\cleardoublepage\stdsection}