我正在使用以下代码创建一个长表-
\begin{longtable}[c]{lllllll}
\caption{Random Data}
\label{tab:my-table}\\
Instance No. & Concept No. & 1 & 2 & 3 & 4 & 5 \\
\endfirsthead
%
\multicolumn{7}{c}%
{{\bfseries Table \thetable\ continued from previous page}} \\
Instance No. & Concept No. & 1 & 2 & 3 & 4 & 5 \\
\endhead
%
\multirow{5}{*}{1-1} & Name & A & B & C & D & E \\
& a & 2 & 2 & 2 & 2 & 2 \\
& b & 6 & 7 & 8 & 9 & 6 \\
& x & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} \\
& y & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} \\
\multirow{5}{*}{1-2} & Name & A & B & C & D & \\
& a & 2 & 2 & 2 & 2 & \\
& b & 6 & 7 & 8 & 9 & \\
& x & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} & \\
& y & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} & \\
\multirow{5}{*}{1-3} & Name & A & B & C & D & \\
& a & 2 & 2 & 2 & 2 & \\
& b & 7 & 7 & 8 & 9 & \\
& x & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} & \\
& y & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} & {[}1,1{]} &
\end{longtable}
但是,有时,表格会在合并行中间断开并延伸到下一页。如何防止在合并行处断开?我希望表格在合并行开始之前断开。
答案1
有点晚了……
使用longtable
表格代码应如下:
\documentclass{article}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%
\usepackage{lipsum} % for dummy text
%---------------------------------------------------------------%
\usepackage{longtable, multirow}
\usepackage{booktabs}
\begin{document}
\lipsum[1-3]
\begin{longtable}[c]{lllllll}
\caption{Random Data}
\label{tab:my-table}\\
\toprule
Instance No. & Concept No. & 1 & 2 & 3 & 4 & 5 \\
\midrule
\endfirsthead
%
\caption[]{Random Data} \\
\toprule
Instance No. & Concept No. & 1 & 2 & 3 & 4 & 5 \\
\midrule
\endhead
\midrule[\heavyrulewidth]
\multicolumn{7}{r}{\footnotesize\itshape Continued on next page}
\endfoot
\bottomrule
\endlastfoot
%
\multirow{5}{*}{1-1}
& Name & A & B & C & D & E \\*
& a & 2 & 2 & 2 & 2 & 2 \\*
& b & 6 & 7 & 8 & 9 & 6 \\*
& x & [1,1] & {[}1,1] & [1,1] & [1,1] & [1,1] \\*
& y & [1,1] & [1,1] & [1,1] & [1,1] & [1,1] \\
\multirow{5}{*}{1-2}
& Name & A & B & C & D & \\*
& a & 2 & 2 & 2 & 2 & \\*
& b & 6 & 7 & 8 & 9 & \\*
& x & [1,1{]} & [1,1{]} & [1,1{]} & [1,1] & \\*
& y & [1,1] & [1,1] & [1,1] & [1,1] & \\
\multirow{5}{*}{1-3}
& Name & A & B & C & D & \\*
& a & 2 & 2 & 2 & 2 & \\*
& b & 7 & 7 & 8 & 9 & \\*
& x & [1,1] & [1,1] & [1,1] & [1,1] & \\*
& y & [1,1] & [1,1] & [1,1] & [1,1] & \\
\end{longtable}
\end{document}
(红线显示页面布局)
如您所见,我稍微改变了一下表格设计,看起来有点好看 :-)。但是,我会考虑使用tabularray
包。通过它的表环境,longtblr
表代码相当短:
\documentclass{article}
\usepackage{lipsum} % for dummy text
%---------------------------------------------------------------%
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\ExplSyntaxOn
\NewChildSelector{eachfive}
{
\int_step_inline:nnnn {1}{5}{\l_tblr_childs_total_tl}
{ \clist_put_right:Nn \l_tblr_childs_clist {##1} }
}
\ExplSyntaxOff
\begin{document}
\lipsum[1-3]
\begin{longtblr}[
caption = {Random Data},
label = {tab:my-table}
]{colspec={ Q[c,m,wd=4em] Q[c,m,wd=3em] *{6}{c} },
cell{2,7,12}{1} = {r=5}{m},
row{2-Z} = {rowsep=0pt},
row{2} = {abovesep=2pt}, row{Z} = {belowsep=2pt},
row{eachfive} = {belowsep=1ex},
rowhead = 1
}
\toprule
Instance No. & Concept No. & 1 & 2 & 3 & 4 & 5 \\
\midrule
1-1 & Name & A & B & C & D & E \\*
\midrule
& a & 2 & 2 & 2 & 2 & 2 \\*
& b & 6 & 7 & 8 & 9 & 6 \\*
& x & [1,1] & {[}1,1] & [1,1] & [1,1] & [1,1] \\*
& y & [1,1] & [1,1] & [1,1] & [1,1] & [1,1] \\
%
1-2 & Name & A & B & C & D & \\*
\midrule
& a & 2 & 2 & 2 & 2 & \\*
& b & 6 & 7 & 8 & 9 & \\*
& x & [1,1{]} & [1,1{]} & [1,1{]} & [1,1] & \\*
& y & [1,1] & [1,1] & [1,1] & [1,1] & \\
%
1-3 & Name & A & B & C & D & \\*
\midrule
& a & 2 & 2 & 2 & 2 & \\*
& b & 7 & 7 & 8 & 9 & \\*
& x & [1,1] & [1,1] & [1,1] & [1,1] & \\*
& y & [1,1] & [1,1] & [1,1] & [1,1] & \\
\bottomrule
\end{longtblr}
\end{document}
在上面的 MWE 中我稍微改变了表格样式:
ble 代码相当短: