3

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}
2
  • You can redefine the \section command and include \cleardoublepage when using the \section. Just add(in preamble) : \let\stdsection\section \renewcommand\section{\cleardoublepage\stdsection}
    – Bobyandbob
    Feb 14, 2017 at 22:36
  • 1
    @Bobyandbob This solution works just as well, and I might say even better than the other one (the other solution has some odd behavior in weird edge cases). Feel free to post this as a full answer.
    – zephyr
    Feb 15, 2017 at 13:39

2 Answers 2

5

longtable and so tabularx with ltablex doesn't reset \if@nobreak to false if immediately following a section title.

So, if you have

\section{Title}

\begin{tabularx}{\textwidth}{cX}
<long table>
\end{tabularx}

\section{Title}

when LaTeX scans the second \section command it sees \if@nobreak set to true (every sectional command sets it, in order to avoid page breaks between two consecutive titles).

You can patch \endlongtable to issue \@nobreakfalse (which acts globally).

\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
  • Great! This perfectly solves my problem.
    – zephyr
    Feb 14, 2017 at 22:24
  • I was going to say that:-) Feb 14, 2017 at 22:24
1

You can redefine the \section command and include \cleardoublepage when using the \section. Just add(in preamble) :

\let\stdsection\section
\renewcommand\section{\cleardoublepage\stdsection}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .