1

I'm trying to reimplement the David Carlisle solution given here for breaking long tables over certain rows (breaking in \hline commnands instead \cline ones). This solution is complemented with the use of \\* command (another example here), and works perfectly with longtableand longtabu environments.

Now, I would like do it work with the booktabs package, which uses a different mechanism to draw lines, especially in \hline\hline cases.

Following the procedure given by David, I rewrote the \@cmidrulea and \@cmidruleb commands, which, according booktabs documentation, actually draw the rules. This is a 'minimal' example:

\documentclass[oneside,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage[left=1in,right=1in]{geometry}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{multirow}   % Para las tablas (varias filas)
\usepackage[table]{xcolor}  % Colores. Tambien en tablas

\newcommand{\OG}{\cellcolor{orange}}

%%%% Tomado de https://tex.stackexchange.com/questions/52100/longtable-multirow-problem-with-cline-and-nopagebreak
\makeatletter

\def\@cmidrulea{%
  \multispan\@cmidla&\multispan\@cmidlb
  \unskip\hskip\cmrkern@l%
  {\CT@arc@\leaders\hrule \@height\@thisrulewidth\hfill}%
  \hskip\cmrkern@r\cr
  \noalign{\nobreak\vskip-\@thisrulewidth} %% Inserted \nobreak here
}%

\def\@cmidruleb{% This remains unchanged
  \multispan\@cmidlb
  \unskip\hskip \cmrkern@l%
  {\CT@arc@\leaders\hrule \@height\@thisrulewidth\hfill}%
  \hskip\cmrkern@r\cr
  % \noalign{\nobreak\vskip-\@thisrulewidth} %% With this, "almost" works...
}%

\makeatother

\begin{document}

\lipsum[1-3]

\begin{longtabu} to \textwidth {|X[3,lm]|X[1,cm]|X[1,cm]|X[1,cm]|X[1,cm]|X[1,cm]|X[1,cm]|}
  \toprule
  \rowfont\bfseries
  Lorem ipsum dolor sit amet, consectetuer adipiscing
  elit.
  & Curabitur
  & Nam
  & Pellentesque
  & Donec
  & Curabitur
  & Cras\\*
  \midrule
  \endhead

  \multicolumn{7}{r}{\small \it Continued on next page} \\* \endfoot

  \endlastfoot

  \multicolumn{7}{c}{\OG\textbf{First group heading}}\\*
  \midrule

  Lorem ipsum dolor sit amet & Terra & Integer & & Mauris & Mauris &  MC \\
  \midrule

  Pellentesque habitant morbi tristique senectus& Terra & Integer &
  & Orci & Mauris & DB \\
  \midrule

  Pellentesque habitant morbi tristique senectus& Terra & Integer &
  & Orci & Mauris & DB \\
  \midrule

  Pellentesque habitant morbi tristique senectus& Terra & Integer &
  & Orci & Mauris & DB \\
  \midrule

  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}

  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\

  \midrule

  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}

  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\

  \midrule

  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}

  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\

  \midrule
  \multicolumn{7}{c}{\OG\textbf{Second group heading}}\\*
  \midrule

  Lorem ipsum dolor sit amet & Terra & Integer & & Mauris & Mauris &  MC \\
  \midrule

  Pellentesque habitant morbi tristique senectus& Terra & Integer &
  & Orci & Mauris & DB \\
  \midrule

  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}

  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\

  \midrule

  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}

  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\

  \midrule
  \multicolumn{7}{c}{\OG\textbf{Third group heading}}\\*
  \midrule
  %\cmidrule{1-7} %% Ouch!

  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}

  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\


  \bottomrule 

  \caption{Fiat iustitia, ne pereat mundus.}    
  \label{tab:fiat-iustitia}
  \tabuphantomline
\end{longtabu}

\end{document}

As you can see if you process the document, there is a page breaking in the third heading group, even with a explicit no break \\* command.

As desperate solution, I've tried replace the \midrule command by a less elegant \cmidrule{1-7} but this only almost work if I uncomment the \nobreak line in \@cmidruleb (re)definition. If I do this, the page breaks correctly, but last line is drawn in the next page. I'm also interested in this last case because I'll probably need it with multirow cells in the right side of the table, with \cmidrule{1-n} commands.

Can anyone help me?

Thanks in advance.

2

Page breaking typically happens where a \vskip (rather than a rule) is added. The following puts \BT@nobreak before every vskip added by nooktables (with a default definition of \relax then two commands are provided that switch its definition between \relax and \nobreak so you can prevent page breaks at booktabs rules for sections of the table, specifically around your coloured group headings and in areas spanned by \multirow

\documentclass[oneside,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage[left=1in,right=1in]{geometry}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{multirow}   % Para las tablas (varias filas)
\usepackage[table]{xcolor}  % Colores. Tambien en tablas

\newcommand{\OG}{\cellcolor{orange}}

%%%% Tomado de http://tex.stackexchange.com/questions/52100/longtable-multirow-problem-with-cline-and-nopagebreak
\makeatletter
\let\BT@nobreak\relax
\def\@@@cmidrule[#1-#2]#3#4{\global\@cmidla#1\relax
    \global\advance\@cmidla\m@ne
    \ifnum\@cmidla>0\global\let\@gtempa\@cmidrulea\else
    \global\let\@gtempa\@cmidruleb\fi
    \global\@cmidlb#2\relax
    \global\advance\@cmidlb-\@cmidla
    \global\@thisrulewidth=#3
    \@setrulekerning{#4}
    \ifnum\@lastruleclass=\z@\BT@nobreak\vskip \aboverulesep\fi
    \ifnum0=`{\fi}\@gtempa
    \noalign{\ifnum0=`}\fi\futurenonspacelet\@tempa\@xcmidrule}

\def\@BTendrule{\ifx\@tempa\toprule\global\@lastruleclass=\@thisruleclass
  \else\ifx\@tempa\midrule\global\@lastruleclass=\@thisruleclass
  \else\ifx\@tempa\bottomrule\global\@lastruleclass=\@thisruleclass
  \else\ifx\@tempa\cmidrule\global\@lastruleclass=\@thisruleclass
  \else\ifx\@tempa\specialrule\global\@lastruleclass=\@thisruleclass
  \else\ifx\@tempa\addlinespace\global\@lastruleclass=\@thisruleclass
  \else\global\@lastruleclass=\z@\fi\fi\fi\fi\fi\fi
  \ifnum\@lastruleclass=\@ne\relax\else\BT@nobreak\vskip\@belowrulesep\fi
  \ifnum0=`{\fi}}

\def\@xcmidrule{%
   \ifx\@tempa\cmidrule
       \BT@nobreak\vskip-\@thisrulewidth
       \global\@lastruleclass=\@ne
   \else \ifx\@tempa\morecmidrules
       \BT@nobreak\vskip \cmidrulesep
       \global\@lastruleclass=\@ne\else
       \BT@nobreak\vskip \belowrulesep
       \global\@lastruleclass=\z@
   \fi\fi
   \ifnum0=`{\fi}}

\def\@BTrule[#1]{%
\BT@nobreak
  \global\@thisrulewidth=#1\relax
  \ifnum\@thisruleclass=\tw@\vskip\@aboverulesep\else
  \ifnum\@lastruleclass=\z@\vskip\@aboverulesep\else
  \ifnum\@lastruleclass=\@ne\vskip\doublerulesep\fi\fi\fi
  \ifx\longtable\undefined
     \let\@BTswitch\@BTnormal
  \else\ifx\hline\LT@hline
     \let\@BTswitch\@BLTrule
  \else
     \let\@BTswitch\@BTnormal
  \fi\fi
  \@BTswitch}

\def\@cmidrulea{%
  \multispan\@cmidla&\multispan\@cmidlb
  \unskip\hskip\cmrkern@l%
  {\CT@arc@\leaders\hrule \@height\@thisrulewidth\hfill}%
  \hskip\cmrkern@r\cr
  \noalign{\BT@nobreak\vskip-\@thisrulewidth} %% Inserted \BT@nobreak here
}%

\def\nobtrulebreaks{\noalign{\global\let\BT@nobreak\nobreak}}
\def\allowbtrulebreaks{\noalign{\global\let\BT@nobreak\relax}}
\makeatother

\begin{document}

\lipsum[1-3]

\begin{longtabu} to \textwidth {|X[3,lm]|X[1,cm]|X[1,cm]|X[1,cm]|X[1,cm]|X[1,cm]|X[1,cm]|}
  \toprule
  \rowfont\bfseries
  Lorem ipsum dolor sit amet, consectetuer adipiscing
  elit.
  & Curabitur
  & Nam
  & Pellentesque
  & Donec
  & Curabitur
  & Cras\\*
  \midrule
  \endhead

  \multicolumn{7}{r}{\small \it Continued on next page} \\* \endfoot

  \endlastfoot
\nobtrulebreaks
  \multicolumn{7}{c}{\OG\textbf{First group heading}}\\*
  \midrule


  Lorem ipsum dolor sit amet & Terra & Integer & & Mauris & Mauris &  MC \\
\allowbtrulebreaks
  \midrule

  Pellentesque habitant morbi tristique senectus& Terra & Integer &
  & Orci & Mauris & DB \\
  \midrule

  Pellentesque habitant morbi tristique senectus& Terra & Integer &
  & Orci & Mauris & DB \\
  \midrule

  Pellentesque habitant morbi tristique senectus& Terra & Integer &
  & Orci & Mauris & DB \\
  \midrule

  \nobtrulebreaks
  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}

  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\
\allowbtrulebreaks
  \midrule
  \nobtrulebreaks
  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}

  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\
\allowbtrulebreaks
  \midrule

\nobtrulebreaks
  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}

\allowbtrulebreaks
  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\

\nobtrulebreaks
  \midrule
  \multicolumn{7}{c}{\OG\textbf{Second group heading}}\\*
  \midrule

  Lorem ipsum dolor sit amet & Terra & Integer & & Mauris & Mauris &  MC \\
\allowbtrulebreaks
  \midrule

  Pellentesque habitant morbi tristique senectus& Terra & Integer &
  & Orci & Mauris & DB \\
  \midrule

  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}

  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\

  \midrule

\nobtrulebreaks
  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}
\allowbtrulebreaks
  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\

\nobtrulebreaks
  \midrule
  \multicolumn{7}{c}{\OG\textbf{Third group heading}}\\*
  \midrule
  %\cmidrule{1-7} %% Ouch!


  \multirow{2}{\linewidth}{Pellentesque habitant morbi tristique
    senectus} &
  Phasellus (vehicula augue eu neque) & Integer & \textbf{
    \parbox{\linewidth}{
      Curabitur\\
      Phasellus
    }
  } & Mauris & \textbf{Magna} &  DB \\*

  \cmidrule{2-7}
\allowbtrulebreaks
  & Donec (vehicula augue eu neque) & Puella &
  \textbf{Curabitur Phasellus Magna} & Mauris & \textbf{Magna} &  DB \\


  \bottomrule 

  \caption{Fiat iustitia, ne pereat mundus.}    
  \label{tab:fiat-iustitia}
  \tabuphantomline
\end{longtabu}

\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.