
如何將表格中的兩個儲存格合併為一個儲存格?
例如,我想合併“456”的單元格,如下所示。
\documentclass[journal]{IEEEtran}
\begin{document}
\begin{table}[h]
\captionof{table}{Test Table}
\label{tab:table_x}
\noindent\begin{tabularx}{\columnwidth} { |
c |
c |
>{\raggedright\arraybackslash}X |
}
\hline
\textbf{A} & \textbf{B} & \textbf{C} \\
\hline
abc & def & ghi \\
\hline
123 & 456 & mno \\
\hline
jkl & 456 & 789 \\
\hline
xxx & yyy & zzz \\
\hline
\end{tabularx}
\end{table}
\end{document}
目前表:
答案1
如果您想在tabularx
所有這些規則中執行此操作(我不喜歡它們,它們使表格看起來混亂,不整潔或易讀),我建議使用套件hhline
(提供比\cline
) 和更好的間距multirow
。我還使用了array
和\extrarowheight
來獲得更好的水平線間距(它們在預設的 LaTeX 中太接近了)。
\documentclass[journal]{IEEEtran}
\usepackage{tabularx}
\usepackage{hhline}
\usepackage{multirow}
\usepackage{array}
\setlength\extrarowheight{1pt}
\begin{document}
\begin{table}[h]
\caption{Test Table\label{tab:table_x}}
\noindent\begin{tabularx}{\columnwidth} { |
c |
c |
>{\raggedright\arraybackslash}X |
}
\hline
\textbf{A} & \textbf{B} & \textbf{C} \\
\hline
abc & def & ghi \\
\hline
123 & & mno \\
\hhline{|-|~|-|}
jkl & \multirow{-2}{*}[-.5\arrayrulewidth]{456} & 789 \\
\hline
xxx & yyy & zzz \\
\hline
\end{tabularx}
\end{table}
\end{document}
要準確地取得影像中顯示的表格(或多或少,間距不完全相同),請不要使用tabularx
普通的tabular
:
\documentclass[journal]{IEEEtran}
\usepackage{hhline}
\usepackage{multirow}
\usepackage{array}
\setlength\extrarowheight{1pt}
\begin{document}
\begin{table}[h]
\centering
\caption{Test Table\label{tab:table_x}}
\begin{tabular}{ | c | c | c | }
\hline
\textbf{A} & \textbf{B} & \textbf{C} \\
\hline
abc & def & ghi \\
\hline
123 & & mno \\
\hhline{|-|~|-|}
jkl & \multirow{-2}{*}[-.5\arrayrulewidth]{456} & 789 \\
\hline
xxx & yyy & zzz \\
\hline
\end{tabular}
\end{table}
\end{document}
我會用什麼
我會使用規則,而不是垂直規則(以及每行後面的規則)booktabs
。如果我需要“合併”單元格,我會將它們放在第一列(如果適用)中,然後簡單地\addlinespace
在每個區塊之後添加。有意義的資料(如數字資料)我不會省略和合併儲存格,但重複:
\documentclass[journal]{IEEEtran}
\usepackage{tabularx}
\usepackage{booktabs}
\begin{document}
\begin{table}[h]
\centering
\caption{Test Table\label{tab:table_x}}
\begin{tabularx}{\columnwidth} { c c >{\raggedright\arraybackslash}X }
\toprule
\textbf{A} & \textbf{B} & \textbf{C} \\
\midrule
abc & def & ghi \\
& 456 & mno \\
\addlinespace
jkl & 456 & 789 \\
\addlinespace
xxx & yyy & zzz \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
答案2
您的程式碼並不是真正可重現的,因為它有一些錯誤(缺少一個套件)。看看我的近似值:
\documentclass[journal]{IEEEtran}
\usepackage{multirow} % <-- added package
\usepackage{tabularx}
\begin{document}
\begin{table}[h]
\caption{Test Table}
\label{tab:table_x}
\noindent\begin{tabularx}{\columnwidth} { |
c |
c |
>{\raggedright\arraybackslash}X |
}
\hline
\textbf{A} & \textbf{B} & \textbf{C} \\
\hline
abc & def & ghi \\
\hline
123 & \multirow{2}{*}{456} & mno \\ % <-- merged cell
\cline{1-1}\cline{3-3} % <-- added rule
jkl & & 789 \\ % <-- merged cell
\hline
xxx & yyy & zzz \\
\hline
\end{tabularx}
\end{table}
\end{document}
答案3
您可以使用新的 tabularray 套件非常輕鬆地完成此操作。
\documentclass[journal]{IEEEtran}
\usepackage{tabularray}
\begin{document}
\begin{table}[h]
\caption{Test Table}
\label{tab:table_x}
\centering
\begin{tblr}{
colspec = {QQQ},
hlines,
vlines,
columns={c},
rows={m},
cell{3}{2} = {r=2,c=1}{c},
row{1}={font=\bfseries},
}
A & B & C \\
abc & def & ghi \\
123 & 456 & mno \\
jkl & 456 & 789 \\
xxx & yyy & zzz \\
\end{tblr}
\end{table}
\end{document}