0

How can I merge two cells into one cell in a table?

For example, I would like to merge the cell for "456" as follows.

expected table

\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}

Current table:

current table

  • Did you have a look at commands like \multicolumn and \multirow? – albert Mar 17 '23 at 11:22
  • Your current minimal code example isn't compilable. You're missing \usepackage{tabularx} and one package that defines \captionof (but please note that you don't need \captionof inside a table environment, \caption suffices there). – Skillmon Mar 17 '23 at 11:39

3 Answers3

0

Your code is not really reproducible, since it has some errors (one package is missing). Have a look at my approximation:

\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}

enter image description here

scd
  • 774
0

If you want to do this in tabularx and with all those rules (I don't like them, they make a table look cluttered, not tidy or legible), I'd suggest using the packages hhline (gives better spacing than \cline) and multirow. I also used array and \extrarowheight to get better spacing to the horizontal rules (they are too close in default 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}

enter image description here

To get exactly the table you show in your image (more or less, that's not exactly the same spacing) don't use tabularx but a 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}

enter image description here


What I'd use

Instead of vertical rules (and rules after each line) I'd use the booktabs rules. If I need to "merge" cells I'd put them in the first column (if applicable) and simply add \addlinespace after each block. Meaningful data (like numeric data) I'd not omit and merge cells, but repeat:

\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}

enter image description here

Skillmon
  • 60,462
  • Hi @Skillmon, how to make the table with tabularx becomes more concise by deleting the white space area in column "C"? Thank you. – Nicholas TI Mar 27 '23 at 07:21
  • @NicholasTI The column with spec X is as wide as necessary to fill the width you specified in the first argument to tabularx (so in this case \columnwidth), if you want it to be narrower change \columnwidth to another value. If you want it just as narrow as necessary, change it to use l, c, or r (left, centre, or right aligned). – Skillmon Mar 27 '23 at 07:28
0

You can do it very easily using new package 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}

enter image description here

Krishna
  • 125