user2670818 user2670818 - 3 months ago 41
LaTeX Question

How to merge cells with multirow in LaTeX?

I have the following code:

\begin{table}[ht]

\begin{center}
\begin{tabular}{ | l | l | l | l | l | l | l | }
\hline
\multirow{2}{*}{Oprava} & \multirow{2}{*}{zkratka} & \multirow{2}{*}{alg.} & chybovost \% & & & \\ \hline
& & & MAE & RSE & RMSE & RRSE \\ \hline
velikosti & vel. & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
bilateralne & bilat. & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
oprava & zkratka & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
oprava & zkratka & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
\end{tabular}
\end{center}

\caption{Multi-row table}
\label{tab:multicol}
\end{table}


And what I get is this:
LaTeX output

I need to merge the cells, so that the text is not crossed out. The same for the rows with the repeated algorithms names - NBM, kNN etc ..... they should me merged and centered. Pls help.

Thanks

Answer

The solution to your problem is very simple. You should use \cline{4-7} instead of \hline. Namely \hline makes a horizontal line over the complete width of the table, while \cline{4-7} will only make a horizontal line from column 4 until 7, and thus not crossing the first ones.

To get the text in front of the repeated words centered and merged, you can use multirow{}{}, again in combination with \cline{}. More info about these commands at this link.

An example of the code is given below:

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|l|l|l|l|l|}
\hline
\multirow{2}{*}{Oprave} & \multirow{2}{*}{Zkratka} & \multirow{2}{*}{Alg.} & \multicolumn{4}{l|}{Chybovost \%} \\ \cline{4-7} 
  &  &  & MAE & RSE & RMSE & RRSE \\ \hline
\multirow{4}{*}{velikosti} & \multirow{4}{*}{vel.} & NBM &  &  &  &  \\ \cline{3-7} 
 &  & c4.5 &  &  &  &  \\ \cline{3-7} 
 &  & kNN &  &  &  &  \\ \cline{3-7} 
 &  & SMO &  &  &  &  \\ \hline
\multirow{4}{*}{bilateralne} & \multirow{4}{*}{bilat.} & NBM &  &  &  &  \\ \cline{3-7} 
 &  & c4.5 &  &  &  &  \\ \cline{3-7} 
 &  & kNN &  &  &  &  \\ \cline{3-7} 
 &  & SMO &  &  &  &  \\ \hline
\multirow{4}{*}{oprava} & \multirow{4}{*}{zkratka} & NBM &  &  &  &  \\ \cline{3-7} 
 &  & c4.5 &  &  &  &  \\ \cline{3-7} 
 &  & kNN &  &  &  &  \\ \cline{3-7} 
 &  & SMO &  &  &  &  \\ \hline
\multirow{4}{*}{oprava} & \multirow{4}{*}{zkratka} & NBM &  &  &  &  \\ \cline{3-7} 
 &  & c4.5 &  &  &  &  \\ \cline{3-7} 
 &  & kNN &  &  &  &  \\ \cline{3-7} 
 &  & SMO &  &  &  &  \\ \hline
\end{tabular}
\end{table}

Resulting into:

Result

Note: if you have struggles with table generation, or you want a more quick way to easily make tables as you would do in Excel, you can use an online table generator for Latex. I often use http://www.tablesgenerator.com/