使用 cmidrule 和 nopagebreak 的 longtable 多行问题

使用 cmidrule 和 nopagebreak 的 longtable 多行问题

我正在尝试重新实现 David Carlisle 给出的解决方案这里用于拆分长表的特定行(使用\hline命令拆分而不是\cline一行)。此解决方案与使用\\*命令(另一个示例这里longtable),并且与和环境完美配合longtabu

booktabs现在,我想与使用不同机制来绘制线条的包一起工作,特别是在\hline\hline某些情况下。

按照 David 给出的步骤,我重写了\@cmidrulea\@cmidruleb命令,根据 booktabs 文档,它们实际上绘制了规则。这是一个“最小”示例:

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

正如您所看到的,如果您处理该文档,则会在第三个标题组中出现分页符,即使使用明确的无分页\\* 命令也是如此。

作为绝望的解决方案,我尝试用\midrule一​​个不太优雅的命令替换该命令\cmidrule{1-7},但这只有在我取消注释(重新)定义\nobreak中的行时才有效\@cmidruleb。如果我这样做,页面会正确分页,但最后一行会在下一页绘制。我对最后一种情况也很感兴趣,因为我可能需要在表格右侧的多行单元格中使用\cmidrule{1-n}命令。

谁能帮我?

提前致谢。

答案1

分页通常发生在\vskip添加(而不是规则)的地方。以下内容将\BT@nobreaknooktables 添加的每个 vskip 放在前面(默认定义为) \relax,然后提供两个命令在和之间切换其定义\relax\nobreak以便您可以防止在 booktabs 规则中为表格的各个部分(特别是在彩色组标题周围以及由\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}

相关内容