
我想在 Latex 中建立這樣的表。
這是我的創建表的腳本。
\begin{table}
\centering
\caption{Table}
\begin{tabular}{|c|c|l|} \hline
\multirow{3}{*}{DEF}&\multicolumn{3}{|c|}{ ABC} \\ \hline
A&B&C\\ \hline
1& 4 & 7 \\ \hline
2& 5 & 8 \\ \hline
3& 6 & 9 \\ \hline
\end{tabular}
\end{table}
當我運行這個腳本時,它顯示這樣的錯誤。
! Extra alignment tab has been changed to \cr.<template> \endtemplate ...row{3}{}{DEF}&\multicolumn{3}{|c|}{ ABC}
如何創建像這樣的圖片的表格?
答案1
您可以破解以下模板。
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{array,multirow}
\let\mc=\multicolumn
\let\mr=\multirow
\let\cl=\cline
\begin{document}
\begin{tabular}{|*6{c|}l}
\cl{3-6}
\mc{2}{c|}{\mr{2}{*}{empty}} & \mc{4}{c|}{Primes} \\\cl{3-6}
\mc{2}{c|}{} & 2 & 3 & 5 & 7 \\\cl{1-6}
\mr{2}{*}{Powers} & 504 & 3 & 2 & 0 & 1 \\\cl{2-6}
& 540 & 2 & 3 & 1 & 0 \\\cl{1-6}
\mr{2}{*}{Powers} & HCF & 2 & 2 & 0 & 0 & min \\\cl{2-6}
& LCM & 3 & 3 & 1 & 1 & max \\\cl{1-6}
\end{tabular}
\end{document}
警告
\mr
可以嵌套,\mc
但反之則不行!
參考
我上面的程式碼的靈感來自於一個例子維基百科如下。
\begin{tabular}{cc|c|c|c|c|l}
\cline{3-6}
& & \multicolumn{4}{ c| }{Primes} \\ \cline{3-6}
& & 2 & 3 & 5 & 7 \\ \cline{1-6}
\multicolumn{1}{ |c }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{504} & 3 & 2 & 0 & 1 & \\ \cline{2-6}
\multicolumn{1}{ |c }{} &
\multicolumn{1}{ |c| }{540} & 2 & 3 & 1 & 0 & \\ \cline{1-6}
\multicolumn{1}{ |c }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{gcd} & 2 & 2 & 0 & 0 & min \\ \cline{2-6}
\multicolumn{1}{ |c }{} &
\multicolumn{1}{ |c| }{lcm} & 3 & 3 & 1 & 1 & max \\ \cline{1-6}
\end{tabular}
然而,正如您所看到和比較的那樣,我的程式碼要簡單得多,並且重構得很好,但仍然可讀,對吧?
答案2
您嘗試做的事情可以這樣完成:
\documentclass{standalone}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|c|c} \hline
& & \multicolumn{3}{|c|}{ ABC} \\ \hline
& & A&B&C\\ \hline
\multirow{3}{*}{DEF} & D &1& 4 & 7 \\ \cline{2-5}
&E& 2& 5 & 8 \\ \cline{2-5}
&F& 3& 6 & 9 \\ \cline{2-5}
\end{tabular}
\end{document}
但! LaTeX 可以製作比這種 Excel 樣式佈局更好的表格。考慮做這樣的事情,如果你有超過 3 列,這仍然很好看。
\documentclass{standalone}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{lcccccc}
\toprule
& \multicolumn{3}{c}{ ABC} & \multicolumn{3}{c}{ ABC} \\\cmidrule(lr){2-4} \cmidrule(lr){5-7}
& A&B&C & A2&B2&C2\\ \midrule
DEF:\\
\;D &1& 4 & 7 &1& 4 & 7 \\
\;E& 2& 5 & 8 &1& 4 & 7 \\
\;F& 3& 6 & 9 &1& 4 & 7 \\
XYZ:\\
\;X &1& 4 & 7 &1& 4 & 7 \\
\;Y& 2& 5 & 8 &1& 4 & 7 \\
\;Z& 3& 6 & 9 &1& 4 & 7 \\ \bottomrule
\end{tabular}
\end{document}
答案3
\multirow
命令,需要對其自己的列進行操作...因此,您需要在每列的左側添加一個額外的列,因此在&
表的每一行的每個左側都需要一個額外的列,並且需要一個額外的列類型(可以說c
) 在表格參數的開頭。
另外,範例中的多列應為 3 行高,並且應從行 (1,4,7) 開始。但是,\hline
在這一行或下一行中,也會在「多行」儲存格中新增一行,這是不想要的。因此,我們必須更改一些\hline
s,\cline{2-4}
即從第 2 列到第 4 列的一行,而不是從第一列到最後一行。
最後,您可以添加空\multicolumn{1}{c|}{}
作為前兩行的第一個元素,以便將它們的行為從 更改為|c|
並c|
以這種方式從左側刪除不需要的垂直線:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}
\centering
\caption{Table}
\begin{tabular}{|c|c|c|l|} \cline{2-4}
\multicolumn{1}{c|}{}&\multicolumn{3}{|c|}{ ABC} \\ \cline{2-4}
\multicolumn{1}{c|}{}&A&B&C\\ \hline
\multirow{3}{*}{DEF}&1& 4 & 7 \\ \cline{2-4}
&2& 5 & 8 \\ \cline{2-4}
&3& 6 & 9 \\ \hline
\end{tabular}
\end{table}
\end{document}
PS:試圖向您展示您的錯誤是什麼,而不是找到一種方法來讓您的桌子變得更漂亮。