2

Hi want to create a table in which two cells should be merged horizontally.The output table should looks like this:

enter image description here

I have tried it like that

\begin{table}[t]\small
\centering
\caption{Technical Characteristics of Hyperspectral Imaging System.}
\begin{tabular}{|c|c|c|c|c|}
 \hline
      Patch Size& & CNN& & HOG\\
\hline
       & Training & Testing & Training & Testing\\
 \hline
 500 x 500  &  99.43    & 94.72 & 83.28 & 74.34\\
 \hline

\end{tabular}
\label{camera}
\end{table}

But it didn't work

4
  • That table doesn't have any rows which are merged. Do you mean columns? Can you please make your code snippet into something compilable? Use \multicolumn{<number>}{<specifier>}{<content>} if you want to merge columns, rather than rows, as the image suggests.
    – cfr
    Commented May 28, 2018 at 2:19
  • E.g. \mulicolumn{2}{c}{ANN} or whatever.
    – cfr
    Commented May 28, 2018 at 2:20
  • Sorry its multi column.. where i can add \mulicolumn{2}{c}{ANN}? Commented May 28, 2018 at 3:09
  • You just put it where you would otherwise put ANN if you wanted just one column.
    – cfr
    Commented May 28, 2018 at 3:27

1 Answer 1

4

As @cfr has noted in a comment, the \multicolumn directive can be used to "merge" several cells across columns. E.g.,

\multicolumn{2}{c}{Some text}

centers the string "Some text" across 2 columns.

When using \multicolumn, do be aware that this directive completely replaces the default column properties. This means that if the default properties of the 2 columns in question include being bordered by a vertical line on the right, and if you wish the merged cells to be bordered by a vertical line on the right, it's necessary to write

\multicolumn{2}{c|}{Some text}

to add the vertical line to the right of the combined cells.

You may also want to familiarize yourself with ways to make the look of the table easier on your readers' eyes. If you want to stick with lots of vertical and horizontal rules, at least increase the vertical spacing a bit, say by setting \extrarowheight to a nonzero length. (2pt is usually enough.) Or, go ahead and delete all vertical rules and use fewer, but well-spaced horizontal rules. Your readers will almost certainly appreciate the resulting open and inviting "look" -- and they may well reward your effort by actually looking at what's inside the table...

The following screenshot and code illustrate these points.

enter image description here

\documentclass{article}
\usepackage{array}    % for \extrarowheight macro
\usepackage{booktabs} % for \toprule, \midrule, \cmidrule & \bottomrule macros
\begin{document}

\begin{table}[t]
\centering

\caption{Initial ``look''} 
\label{camera:1}
\begin{tabular}{|c|c|c|c|c|}
 \hline
      Patch Size& & CNN& & HOG\\
 \hline
       & Training & Testing & Training & Testing\\
 \hline
 500 x 500  &  99.43    & 94.72 & 83.28 & 74.34\\
 \hline
\end{tabular}

\bigskip
\caption{Two \texttt{\string\multicolumn} directives} \label{camera:2}
\begin{tabular}{|c|c|c|c|c|}
 \hline
 Patch Size & \multicolumn{2}{c|}{CNN} & \multicolumn{2}{c|}{HOG}\\
 \hline
            & Training & Testing & Training & Testing\\
 \hline
 500 x 500  &  99.43    & 94.72 & 83.28 & 74.34\\
 \hline
\end{tabular}


\bigskip
\setlength\extrarowheight{2pt} % for a less-cramped look
\caption{A less-cramped look; ``$\times$'' rather than ``x''} 
\label{camera:3}
\begin{tabular}{|c|c|c|c|c|}
 \hline
 Patch Size & \multicolumn{2}{c|}{CNN} & \multicolumn{2}{c|}{HOG}\\
 \hline
            & Training & Testing & Training & Testing\\
 \hline
 500 $\times$ 500  &  99.43    & 94.72 & 83.28 & 74.34\\
 \hline
\end{tabular}

\bigskip
\setlength\extrarowheight{0pt} % reset to default value
\caption{A much more open look} 
\label{camera:4}
\begin{tabular}{@{} ccccc @{}} % note: no vertical bars at all
 \toprule % not \hline
 Patch Size & \multicolumn{2}{c}{CNN} & \multicolumn{2}{c@{}}{HOG}\\
 \cmidrule(lr){2-3} \cmidrule(l){4-5}
            & Training & Testing & Training & Testing\\
 \midrule % not \hline
 500 $\times$ 500  &  99.43    & 94.72 & 83.28 & 74.34\\
 \bottomrule % not \hline
\end{tabular}

\end{table}
\end{document}
1
  • how can I merge the two rows of 'Patch size' Commented Aug 5, 2018 at 0:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .