1

In a long table it is usually possible to insert a \pagebreak in front of a row. If, however, that row begins with a command using \noalign (like \multicolumn or \rowcolor) \pagebreak causes a "misplaced \noalign" error.

I have tried inserting a \\ after the \pagebreak but that inserts an undesired empty row.

I have tried getting rid of the empty row by inserting a negative space with \pagebreak\\[-\baselineskip] but \baselineskip appears to be 0pt.

With the help of \showthe\baselineskip I have found that \pagebreak\\[-\arraystretch\dimexpr12pt\relax] does what I want - as long as nobody changes the font size.

How do I correctly insert a \pagebreak in front of a \multicolumn without hard coding the 12pt?

\documentclass{article}
\usepackage{ltablex}
\usepackage[table]{xcolor}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}

\colorlet{sectionbg}{gray!20}
\renewcommand{\arraystretch}{1.5}
\newcommand{\eol}{\\\hline}
\renewcommand{\section}[1]{\rowcolor{sectionbg}\multicolumn2{|l|}{#1}\eol}

\keepXColumns

%TODO: how do I avoid hardcoding the 12pt?
\newcommand{\ltpagebreak}{\pagebreak\\[-\arraystretch\dimexpr12pt\relax]}

\begin{document}
    \begin{tabularx}{.5\linewidth}{|lL|}
    \hline\endhead
    \section{Section~1}
        1 & Line A \eol
        2 & Line B \eol
        3 & Line C \eol
    \ltpagebreak
    \section{Section~2}
        4 & Line D \eol
        5 & Line E \eol
        6 & Line F \eol
    \section{Section~3}
        7 & Line G \eol
        8 & Line H \eol
        9 & Line I \eol
    \end{tabularx}
\end{document}
2
  • 2
    you can use \normalbaselineskip Apr 30, 2021 at 15:07
  • yes, thank you. If you post that as an answer I will accept it.
    – jakun
    Apr 30, 2021 at 15:20

1 Answer 1

2

tabularx compiles the tabular twice, the first time to do some measuring, and this doesn't like the pagebreak. You can avoid that with the following test (it is a bit hacky, but tabularx is missing a better test condition.

Regarding the baselineskip: in a tabular you can access it with \normalbaselineskip. And consider to use xltabular instead of ltablex directly. xltabular is maintained.

\documentclass{article}
\usepackage{ltablex}
\usepackage[table]{xcolor}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}

\colorlet{sectionbg}{gray!20}
\renewcommand{\arraystretch}{1.5}
\newcommand{\eol}{\\\hline}
\renewcommand{\section}[1]{\rowcolor{sectionbg}\multicolumn2{|l|}{#1}\eol}

\keepXColumns

%TODO: how do I avoid hardcoding the 12pt?
\let\jakunTXtest\write
\newcommand{\ltpagebreak}{\ifx\jakunTXtest\write \pagebreak\fi}


\begin{document}
    \begin{tabularx}{.5\linewidth}{|lL|}
    \hline\endhead
    \section{Section~1}
        1 & Line A \eol
        2 & Line B \eol
        3 & Line C \eol
    \ltpagebreak
    \section{Section~2}
        4 & Line D \eol
        5 & Line E \eol
        6 & Line F \eol
    \section{Section~3}
        7 & Line G \eol
        8 & Line H \eol
        9 & Line I \eol
    \end{tabularx}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.