Using the article class, I have a command \newcommand{\sectionbreak}{\clearpage}
(via the titlesec
package; see this answer) which clears the page before every new section so it begins on a new page. I now have a situation where a previous section contains only a longtable (generated by ltablex
). However, the next section does not start on a new page in this instance, but rather begins right where the multi-page table left off. It seems that the \sectionbreak
only applies if there is text content in the previous section and the longtable does not trigger the \sectionbreak
.
How can I force the section break to force sections to be on a new page even when previous sections do not contain content? Physically putting \newline
after the table (and leaving the \sectionbreak
command as it is) seems to work, but I'd like a solution that doesn't require knowing when these instances will occur and will just handle them automatically.
Minimum Working Example
\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}
\section
command and include\cleardoublepage
when using the\section
. Just add(in preamble) :\let\stdsection\section \renewcommand\section{\cleardoublepage\stdsection}