答案1
- 请在问题中始终提供 MWE(最小工作示例),即一个可以重现您的问题的完整小文档。
- MWE 不能以代码图像的形式呈现,而必须是编辑器的副本,我们可以将其复制到我们的计算机中并按原样进行测试。
- 我建议使用包,它是和包
xltabular
的组合。longtable
tabularx
- 长表格中包含
multirow
s 可能会导致在页面之间拆分表格时出现问题。因此,在下面的 MWE 中使用\pagebreak
插入表格的方式,使得每页都有完整的multirow
单元格:
\documentclass{article}
\usepackage{geometry}
\usepackage{ragged2e}
\usepackage{multirow, xltabular}
\newcolumntype{L}{>{\RaggedRight}X}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}
\begin{document}
\begin{xltabular}{\linewidth}{| c | l | L |}
\hline
\endfirsthead
\hline
\endhead
%
\multicolumn{3}{|l|}{Test Script} \\
\hline
\multirow{6}{*}{1}
& Status & \\ \cline{2-3}
& Details & \\ \cline{2-3}
& Test Data & \\ \cline{2-3}
& Expected Result & \\ \cline{2-3}
& Actual Result & \\ \cline{2-3}
& Issues & \\
\hline
\multirow{6}{*}{2}
& Status & \\ \cline{2-3}
& Details & \\ \cline{2-3}
& Test Data & \\ \cline{2-3}
& Expected Result & \\ \cline{2-3}
& Actual Result & \\ \cline{2-3}
& Issues & \\
\hline
\multirow{6}{*}{3}
& Status & \\ \cline{2-3}
& Details & \\ \cline{2-3}
& Test Data & \\ \cline{2-3}
& Expected Result & \\ \cline{2-3}
& Actual Result & \\ \cline{2-3}
& Issues & \\
\hline
\multirow{6}{*}{4}
& Status & \\ \cline{2-3}
& Details & \\ \cline{2-3}
& Test Data & \\ \cline{2-3}
& Expected Result & \\ \cline{2-3}
& Actual Result & \\ \cline{2-3}
& Issues & \\
\hline
\multirow{6}{*}{5}
& Status & \\ \cline{2-3}
& Details & \\ \cline{2-3}
& Test Data & \\ \cline{2-3}
& Expected Result & \\ \cline{2-3}
& Actual Result & \\ \cline{2-3}
& Issues & \\
\hline
\multirow{6}{*}{6}
& Status & \\ \cline{2-3}
& Details & \\ \cline{2-3}
& Test Data & \\ \cline{2-3}
& Expected Result & \\ \cline{2-3}
& Actual Result & \\ \cline{2-3}
& Issues & \\
\hline
\multirow{6}{*}{7}
& Status & \\ \cline{2-3}
& Details & \\ \cline{2-3}
& Test Data & \\ \cline{2-3}
& Expected Result & \\ \cline{2-3}
& Actual Result & \\ \cline{2-3}
& Issues & \\
\hline
\pagebreak % <---
\multirow{6}{*}{8}
& Status & \\ \cline{2-3}
& Details & \\ \cline{2-3}
& Test Data & \\ \cline{2-3}
& Expected Result & \\ \cline{2-3}
& Actual Result & \\ \cline{2-3}
& Issues & \\
\hline
\multirow{6}{*}{9}
& Status & \\ \cline{2-3}
& Details & \\ \cline{2-3}
& Test Data & \\ \cline{2-3}
& Expected Result & \\ \cline{2-3}
& Actual Result & \\ \cline{2-3}
& Issues & \\
\hline
\end{xltabular}
\end{document}
第一页的顶部:
第二页:
答案2
您应该定义一个命令,而不是一遍又一遍地重复相同的标记,这很容易出错。
下面的代码基于Žarko的代码,但采用了来自使用 cline 和 nopagebreak 的 longtable 多行问题以避免不必要的分页符。
该宏\block
有七个参数,可以按照指定的顺序填充实际数据。
\documentclass{article}
\usepackage{multirow,xltabular}
\makeatletter % https://tex.stackexchange.com/a/52101/4427
\def\@cline#1-#2\@nil{%
\omit
\@multicnt#1%
\advance\@multispan\m@ne
\ifnum\@multicnt=\@ne\@firstofone{&\omit}\fi
\@multicnt#2%
\advance\@multicnt-#1%
\advance\@multispan\@ne
\leaders\hrule\@height\arrayrulewidth\hfill
\cr
\noalign{\nobreak\vskip-\arrayrulewidth}}
\makeatother
\newcommand{\block}[7]{%
% #1 = number, #2 = Status, #3 = Details, #4 = Test Data,
% #5 = Expected Result, #6 = Actual Result, #7 = Issues
\multirow{6}{*}{#1}
& Status & #2 \\* \cline{2-3}
& Details & #3 \\* \cline{2-3}
& Test Data & #4 \\* \cline{2-3}
& Expected Result & #5 \\* \cline{2-3}
& Actual Result & #6 \\* \cline{2-3}
& Issues & #7 \\ \hline
}
\begin{document}
\begin{xltabular}{\textwidth}{|c|l|X|}
\hline
\multicolumn{3}{|l|}{Test Script} \\
\hline
\endhead
\block{1}{}{}{}{}{}{}
\block{2}{}{}{}{}{}{}
\block{3}{}{}{}{}{}{}
\block{4}{}{}{}{}{}{}
\block{5}{}{}{}{}{}{}
\block{6}{}{}{}{}{}{}
\block{7}{}{}{}{}{}{}
\block{8}{}{}{}{}{}{}
\block{9}{}{}{}{}{}{}
\block{10}{}{}{}{}{}{}
\block{11}{}{}{}{}{}{}
\block{12}{}{}{}{}{}{}
\block{13}{}{}{}{}{}{}
\block{14}{}{}{}{}{}{}
\block{15}{}{}{}{}{}{}
\end{xltabular}
\end{document}
除了缩短输入之外,还有什么好处呢?只需更改定义和表前言,就可以获得不同的输出。
\documentclass{article}
\usepackage{xltabular,booktabs}
\makeatletter % https://tex.stackexchange.com/a/52101/4427
\def\@cline#1-#2\@nil{%
\omit
\@multicnt#1%
\advance\@multispan\m@ne
\ifnum\@multicnt=\@ne\@firstofone{&\omit}\fi
\@multicnt#2%
\advance\@multicnt-#1%
\advance\@multispan\@ne
\leaders\hrule\@height\arrayrulewidth\hfill
\cr
\noalign{\nobreak\vskip-\arrayrulewidth}}
\makeatother
\newcommand{\block}[7]{%
% #1 = number, #2 = Status, #3 = Details, #4 = Test Data,
% #5 = Expected Result, #6 = Actual Result, #7 = Issues
\multicolumn{2}{l}{\itshape Test #1} \\*
Status & #2 \\*
Details & #3 \\*
Test Data & #4 \\*
Expected Result & #5 \\*
Actual Result & #6 \\*
Issues & #7 \\ \midrule
}
\begin{document}
\begin{xltabular}{\textwidth}{lX}
\toprule
\multicolumn{2}{l}{Test Script} \\
\midrule
\endhead
\endfoot
\bottomrule
\endlastfoot
\block{1}{}{}{}{}{}{}
\block{2}{}{}{}{}{}{}
\block{3}{}{}{}{}{}{}
\block{4}{}{}{}{}{}{}
\block{5}{}{}{}{}{}{}
\block{6}{}{}{}{}{}{}
\block{7}{}{}{}{}{}{}
\block{8}{}{}{}{}{}{}
\block{9}{}{}{}{}{}{}
\block{10}{}{}{}{}{}{}
\block{11}{}{}{}{}{}{}
\block{12}{}{}{}{}{}{}
\end{xltabular}
\end{document}