![同时创建多列和多行](https://linux22.com/image/475395/%E5%90%8C%E6%97%B6%E5%88%9B%E5%BB%BA%E5%A4%9A%E5%88%97%E5%92%8C%E5%A4%9A%E8%A1%8C.png)
我想同时创建多列和多行。我找到了不同的方法(\Block 或 \multirow 中的嵌套 \multicolumn),但它们对我来说不起作用。(特别是因为示例在合并单元格之前没有几行。)
这就是 mwe 以及下面图片中我想要的东西。
\documentclass{scrbook}
\usepackage{lscape}
\usepackage{makecell}
\usepackage{float}
\usepackage{booktabs}
\usepackage{tabularx, ragged2e}
\usepackage[longtable]{multirow}
\usepackage[flushleft]{threeparttable}
\begin{document}
\begin{landscape}
\begin{table}[H]
\begin{threeparttable}
\begin{tabularx}{\paperwidth}{XXXXXXXXXX}
\toprule[1.5pt]
&& \multicolumn{3}{c}{Hello}&&\multicolumn{3}{c}{World}&\\
\cmidrule{3-5} \cmidrule{7-9}
A & B & C & D & E & & F & G & H & I \\
\midrule
\multirow[c]{2}{*}{\shortstack[l]{items 1\\and 2}}
& item 1 & 0.01 & 0.02 & 0.015 & & 693329 & 6786784 & 862556 & Bla \\
& item 2 & 0.002 & 0.02 & 0.011 & & 693329 & 7356548 & 736283 & Bla \\
\multirow[c]{5}{*}{Items 3-6}
& item 3 &&&&&&&&\\
& item 4 &&&&&&&&\\
& item 5 &&&&&&&&\\
& item 6 &&&&&&&&\\
\bottomrule
\end{tabularx}
\end{threeparttable}
\end{table}
\end{landscape}
\end{document}
答案1
在您的代码中,我已加载nicematrix
,将其替换{tabularx}
为{NiceTabularX}
并放置命令\Block{*-*}{Merge Cells}
。这样就可以了...
\documentclass{scrbook}
\usepackage{lscape}
\usepackage{makecell}
\usepackage{float}
\usepackage{booktabs}
\usepackage{tabularx, ragged2e}
\usepackage[longtable]{multirow}
\usepackage[flushleft]{threeparttable}
\usepackage{nicematrix}
\begin{document}
\begin{landscape}
\begin{table}[H]
\begin{NiceTabularX}{\linewidth}{XXXXXXXXXX}
\toprule[1.5pt]
&& \multicolumn{3}{c}{Hello}&&\multicolumn{3}{c}{World}&\\
\cmidrule{3-5} \cmidrule{7-9}
A & B & C & D & E & & F & G & H & I \\
\midrule
\multirow[c]{2}{*}{\shortstack[l]{items 1\\and 2}}
& item 1 & 0.01 & 0.02 & 0.015 & & 693329 & 6786784 & 862556 & Bla \\
& item 2 & 0.002 & 0.02 & 0.011 & & 693329 & 7356548 & 736283 & Bla \\
\multirow[c]{4}{*}{Items 3-6}
& item 3 &\Block{*-*}{Merge Cells}\\
& item 4 \\
& item 5 \\
& item 6 \\
\bottomrule
\end{NiceTabularX}
\end{table}
\end{landscape}
\end{document}
但是,在“纯粹”的意义上nicematrix
(如果我可以说……),这里是标准编码:
\documentclass{scrbook}
\usepackage{lscape}
\usepackage{float}
\usepackage{booktabs}
\usepackage{nicematrix}
\begin{document}
\begin{landscape}
\begin{table}[H]
\begin{NiceTabular}{*{10}{X}}
\toprule[1.5pt]
&& \Block{1-3}{Hello} &&&& \Block{1-3}{World} \\
\cmidrule{3-5} \cmidrule{7-9}
A & B & C & D & E & & F & G & H & I \\
\midrule
\Block[t,l]{2-1}{items 1\\and 2}
& item 1 & 0.01 & 0.02 & 0.015 & & 693329 & 6786784 & 862556 & Bla \\
& item 2 & 0.002 & 0.02 & 0.011 & & 693329 & 7356548 & 736283 & Bla \\
\Block{4-1}{Items 3-6}
& item 3 &\Block{*-*}{Merge Cells}\\
& item 4 \\
& item 5 \\
& item 6 \\
\bottomrule
\end{NiceTabular}
\end{table}
\end{landscape}
\end{document}
输出是一样的。
答案2
您可以随时\multirow
替换\raisebox
\documentclass{scrbook}
\usepackage{lscape}
\usepackage{makecell}
\usepackage{float}
\usepackage{booktabs}
\usepackage{tabularx, ragged2e}
\usepackage[longtable]{multirow}
\usepackage[flushleft]{threeparttable}
\begin{document}
\begin{landscape}
\begin{table}[H]
%\begin{threeparttable}% not used
\begin{tabularx}{\linewidth}{XXXXXXXXXX}
\toprule[1.5pt]
&& \multicolumn{3}{c}{Hello}&&\multicolumn{3}{c}{World}&\\
\cmidrule{3-5} \cmidrule{7-9}
A & B & C & D & E & & F & G & H & I \\
\midrule
\multirow[c]{2}{*}{\shortstack[l]{items 1\\and 2}}
& item 1 & 0.01 & 0.02 & 0.015 & & 693329 & 6786784 & 862556 & Bla \\
& item 2 & 0.002 & 0.02 & 0.011 & & 693329 & 7356548 & 736283 & Bla \\
\multirow[c]{4}{*}{Items 3-6}
& item 3 &\multicolumn{8}{c}{\raisebox{-1.5\normalbaselineskip}[0pt][0pt]{Merge Cells}}\\
& item 4 \\
& item 5 \\
& item 6 \\
\bottomrule
\end{tabularx}
%\end{threeparttable}
\end{table}
\end{landscape}
\end{document}
答案3
无需手动调整tabularray
:
\documentclass{scrbook}
\usepackage{lscape}
\usepackage{float}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{landscape}
\begin{table}[H]
\begin{tblr}{
width=\linewidth,
colspec={*{10}{X}},
row{3}={belowsep=0pt},
row{4-Y}={rowsep=0pt},
row{Z}={abovesep=0pt},
}
\toprule[1.5pt]
&& \SetCell[c=3]{c} Hello&&&&\SetCell[c=3]{c} World&&\\
\cmidrule{3-5} \cmidrule{7-9}
A & B & C & D & E & & F & G & H & I \\
\midrule
\SetCell[r=2]{l} {items 1\\and 2} & item 1 & 0.01 & 0.02 & 0.015 & & 693329 & 6786784 & 862556 & Bla \\
& item 2 & 0.002 & 0.02 & 0.011 & & 693329 & 7356548 & 736283 & Bla \\
\SetCell[r=4]{l} Items 3-6 & item 3 &\SetCell[c=8,r=4]{c} Merge Cells&&&&&&&\\
& item 4 \\
& item 5 \\
& item 6 \\
\bottomrule
\end{tblr}
\end{table}
\end{landscape}
\end{document}