
Como posso mesclar duas células em uma célula de uma tabela?
Por exemplo, gostaria de mesclar a célula “456” da seguinte maneira.
\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}
Tabela atual:
Responder1
Se você quiser fazer isso tabularx
com todas essas regras (não gosto delas, elas fazem a mesa parecer desordenada, desorganizada ou legível), sugiro usar os pacotes hhline
(dá melhor espaçamento que \cline
) e multirow
. Também usei array
e \extrarowheight
para obter melhor espaçamento nas regras horizontais (elas estão muito próximas no LaTeX padrão).
\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}
Para obter exatamente a tabela que você mostra na sua imagem (mais ou menos, não é exatamente o mesmo espaçamento), não use tabularx
apenas um normal 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}
O que eu usaria
Em vez de regras verticais (e regras após cada linha), eu usaria as booktabs
regras. Se eu precisar "mesclar" células, eu as colocaria na primeira coluna (se aplicável) e simplesmente adicionaria \addlinespace
após cada bloco. Dados significativos (como dados numéricos) não omitiria e mesclaria células, mas repetiria:
\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}
Responder2
Seu código não é realmente reproduzível, pois contém alguns erros (falta um pacote). Dê uma olhada na minha aproximação:
\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}
Responder3
Você pode fazer isso facilmente usando o novo pacote 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}