# Table with multirows, multicolumns, and a combination of both

I need to create similar table using LaTeX:

I have tried to use \tabular and \multicolumn, but every time I get something wrong.

Could someone help me?

\begin{table}[!ht]
\small
\begin{tabular}{ | l | l | l | l | l | l | l | l | l | l | l | l | l | }
\hline
\multirow{3}{*}{Header 1 \\&\multirow{2}{*}{Header 2} &  &  & Header 3 \\&  &  &  &     &  &  &  &  \\ \hline
&  &  &  & L1 &  & L2 &  & L3 &  &  &  &  \\ \hline
& \multicolumn{2}{c}{H1} & \multicolumn{2}{c}{H2} & \multicolumn{2}{c}{H3} & M1 &     M2 & S1 & S2 & K1 & K2 &  &  &  \\ \hline
T1 & 2 & 0 & 0 & 5 & 2 & 2 & . & 0 & . &  &  &  \\ \hline
T2 & 2 & 1 & 1 & 2 & 2 & 1 & 1 & 1 & 1 &  &  &  \\ \hline
T3 & 2 & 3 & 1 & 4 & 0 & 2 & 1 & 1 & 1 &  &  &  \\ \hline
T4 & 18 & 0 & 0 & 2 & 2 & 4 & . & 0 & . &  &  &  \\ \hline
T5 & 5 & 0 & 0 & 3 & 3 & 1 & . & 0 & . &  &  &  \\ \hline
T6 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 & \  & \  & \  \\ \hline
T7 & 1 & 0 & 1 & 1 & 1 & 1 & . & 1 & 1 & \  & \  & \  \\ \hline
T8 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 & \  & \  & \  \\ \hline
\end{tabular}
\end{table}

-
What have you tried and what goes wrong? Please post some minimal working example and we can help you out. By the way using multicolumns and multirows this problem should have already some answers in other threads here. –  Trefex Apr 21 '14 at 14:57
Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  cfr Apr 21 '14 at 15:05

Here are two approaches. The first uses the standard tabular aesthetic with vertical rules. The second follows the guidelines provided by booktabs which dispenses with vertical rules. See the documentation for further information.

\documentclass{article}
\usepackage{multirow,array,booktabs}
\begin{document}
\begin{table}
\begin{tabular}{|>{\bfseries}c|*{9}{c|}}
\hline
&     \multicolumn{3}{c|}{}   &   \multicolumn{2}{c|}{\bfseries H1}   &   \multicolumn{2}{c|}{\bfseries H2}   &   \multicolumn{2}{c|}{\bfseries H3}\\\cline{2-10}
& \bfseries L1    &   \bfseries L2    &   \bfseries L3    &   \bfseries M1    &   \bfseries M2    &   \bfseries S1    &   \bfseries S2    &   \bfseries K1    &   \bfseries K2\\\hline
T1 & 2 & 0 & 0 & 5 & 2 & 2 & . & 0 & .  \\ \hline
T2 & 2 & 1 & 1 & 2 & 2 & 1 & 1 & 1 & 1  \\ \hline
T3 & 2 & 3 & 1 & 4 & 0 & 2 & 1 & 1 & 1  \\ \hline
T4 & 18 & 0 & 0 & 2 & 2 & 4 & . & 0 & . \\ \hline
T5 & 5 & 0 & 0 & 3 & 3 & 1 & . & 0 & .  \\ \hline
T6 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0  \\ \hline
T7 & 1 & 0 & 1 & 1 & 1 & 1 & . & 1 & 1  \\ \hline
T8 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0  \\ \hline
\end{tabular}
\end{table}
\begin{table}
\begin{tabular}{>{\bfseries}c*{9}{c}}
\toprule
&     \multicolumn{3}{c}{}    &   \multicolumn{2}{c}{\bfseries H1}    &   \multicolumn{2}{c}{\bfseries H2}    &   \multicolumn{2}{c}{\bfseries H3}\\\cmidrule{2-10}
& \bfseries L1    &   \bfseries L2    &   \bfseries L3    &   \bfseries M1    &   \bfseries M2    &   \bfseries S1    &   \bfseries S2    &   \bfseries K1    &   \bfseries K2\\\midrule
T1 & 2 & 0 & 0 & 5 & 2 & 2 & . & 0 & .  \\ \midrule
T2 & 2 & 1 & 1 & 2 & 2 & 1 & 1 & 1 & 1  \\ \midrule
T3 & 2 & 3 & 1 & 4 & 0 & 2 & 1 & 1 & 1  \\ \midrule
T4 & 18 & 0 & 0 & 2 & 2 & 4 & . & 0 & . \\ \midrule
T5 & 5 & 0 & 0 & 3 & 3 & 1 & . & 0 & .  \\ \midrule
T6 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0  \\ \midrule
T7 & 1 & 0 & 1 & 1 & 1 & 1 & . & 1 & 1  \\ \midrule
T8 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0  \\\bottomrule
\end{tabular}
\end{table}
\end{document}


Here are another couple of possibilities using booktabs in light of Manuel's comment. The first trims the rules within the tabular, leaving only the main \midrule division at the full table width:

\documentclass{article}
\usepackage{multirow,array,booktabs}
\begin{document}
\begin{table}
\begin{tabular}{>{\bfseries}c*{9}{c}}
\toprule
&     \multicolumn{3}{c}{}    &   \multicolumn{2}{c}{\bfseries H1}    &   \multicolumn{2}{c}{\bfseries H2}    &   \multicolumn{2}{c}{\bfseries H3}\\\cmidrule(lr){2-4}\cmidrule(lr){5-6}\cmidrule(lr){7-8}\cmidrule(lr){9-10}
& \bfseries L1    &   \bfseries L2    &   \bfseries L3    &   \bfseries M1    &   \bfseries M2    &   \bfseries S1    &   \bfseries S2    &   \bfseries K1    &   \bfseries K2\\\midrule
T1 & 2 & 0 & 0 & 5 & 2 & 2 & . & 0 & .  \\ \cmidrule(lr){1-10}
T2 & 2 & 1 & 1 & 2 & 2 & 1 & 1 & 1 & 1  \\ \cmidrule(lr){1-10}
T3 & 2 & 3 & 1 & 4 & 0 & 2 & 1 & 1 & 1  \\ \cmidrule(lr){1-10}
T4 & 18 & 0 & 0 & 2 & 2 & 4 & . & 0 & . \\ \cmidrule(lr){1-10}
T5 & 5 & 0 & 0 & 3 & 3 & 1 & . & 0 & .  \\ \cmidrule(lr){1-10}
T6 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0  \\ \cmidrule(lr){1-10}
T7 & 1 & 0 & 1 & 1 & 1 & 1 & . & 1 & 1  \\ \cmidrule(lr){1-10}
T8 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0  \\\bottomrule
\end{tabular}
\end{table}
\end{document}


This removes the midrules from the main part of the table altogether:

\documentclass{article}
\usepackage{multirow,array,booktabs}
\begin{document}
\begin{table}
\begin{tabular}{>{\bfseries}c*{9}{c}}
\toprule
&     \multicolumn{3}{c}{}    &   \multicolumn{2}{c}{\bfseries H1}    &   \multicolumn{2}{c}{\bfseries H2}    &   \multicolumn{2}{c}{\bfseries H3}\\\cmidrule(lr){2-4}\cmidrule(lr){5-6}\cmidrule(lr){7-8}\cmidrule(lr){9-10}
& \bfseries L1    &   \bfseries L2    &   \bfseries L3    &   \bfseries M1    &   \bfseries M2    &   \bfseries S1    &   \bfseries S2    &   \bfseries K1    &   \bfseries K2\\\midrule
T1 & 2 & 0 & 0 & 5 & 2 & 2 & . & 0 & .  \\
T2 & 2 & 1 & 1 & 2 & 2 & 1 & 1 & 1 & 1  \\
T3 & 2 & 3 & 1 & 4 & 0 & 2 & 1 & 1 & 1  \\
T4 & 18 & 0 & 0 & 2 & 2 & 4 & . & 0 & . \\
T5 & 5 & 0 & 0 & 3 & 3 & 1 & . & 0 & .  \\
T6 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0  \\
T7 & 1 & 0 & 1 & 1 & 1 & 1 & . & 1 & 1  \\
T8 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0  \\\bottomrule
\end{tabular}
\end{table}
\end{document}


-
Thank you very much indeed –  user50283 Apr 21 '14 at 15:27
@user50283 No problem. Once you posted your code, it wasn't difficult to tidy up the headings. But note for future reference that a Minimal Working Example should start with \documentclass and end with \end{document}. That way, people can see which packages you are using, for example. (Your code won't compile without multirow being loaded, for instance.) –  cfr Apr 21 '14 at 15:30
Just a comment. I think (if I haven't misunderstood the documentation) that the \midrules aren't intended for that use, in fact just to separate “parts” of a table: in this case, just to separate the “header” from the table (i.e., just the first \midrule should stay, all the others shouldn't be necessary). –  Manuel Apr 21 '14 at 15:55
@Manuel You may be right so I've added an example of that kind. I think it makes the table more difficult to read, though, so I've also added an example using trimmed midrules which I think maybe works best. But I'm not at all sure about this... –  cfr Apr 21 '14 at 18:07
@cfr I also think it's less readable. I was just pointing out the “correct use” (in my humble opinion, after reading the documentation) of \midrule. May be there should be another kind of “hrule” for those who doesn't want to follow the “no horizontal lines”. Anyway, I don't completely agree with that norm. –  Manuel Apr 21 '14 at 18:17

Here is another option using tap. It may break some things, since it's not pure “LaTeX” but with care you have a beautifully typeset table.

\documentclass{scrartcl}
\input{tap}
\begin{document}
\begintable
\begintableformat
& \center
\endtableformat
\=
\B!:                   | @3                                      | @6 \textbf{Header 3}                                                              \E!
\B!-                   | @3 \textbf{Header 2}                    | @6 \-                                                                             \E!
\B!: \textbf{Header 1} | @3                                      | @2 \textbf{H1}            | @2 \textbf{H2}            | @2 \textbf{H3}            \E!
\B!-                   | @9 \-                                                                                                                       \E!
\B!:                   | \textbf{L1} | \textbf{L2} | \textbf{L3} | \textbf{M1} | \textbf{M2} | \textbf{S1} | \textbf{S2} | \textbf{K1} | \textbf{K2} \E!
\-
\B!: \textbf{T1}       | 2           | 0           | 0           | 5           | 2           | 2           | .           | 0           | .           \E!
\-
\B!: \textbf{T2}       | 2           | 1           | 1           | 2           | 2           | 1           | 1           | 1           | 1           \E!
\-
\B!: \textbf{T3}       | 2           | 3           | 1           | 4           | 0           | 2           | 1           | 1           | 1           \E!
\-
\B!: \textbf{T4}       | 18          | 0           | 0           | 2           | 2           | 4           | .           | 0           | .           \E!
\-
\B!: \textbf{T5}       | 5           | 0           | 0           | 3           | 3           | 1           | .           | 0           | .           \E!
\-
\B!: \textbf{T6}       | 4           | 0           | .           | 1           | 1           | 1           | .           | 1           | 0           \E!
\-
\B!: \textbf{T7}       | 1           | 0           | 1           | 1           | 1           | 1           | .           | 1           | 1           \E!
\-
\B!: \textbf{T8}       | 4           | 0           | .           | 1           | 1           | 1           | .           | 1           | 0           \E!
\=
\endtable
\end{document}

-

Here is a way of doing. It uses the makecell package, to define common formatting of row and column heads and better vertical spacing than in basic LaTeX:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{array, multirow,  makecell}

\begin{document}

\begin{table}[!ht]
\small\sffamily
\begin{tabular}{ |*{12}{c|} }
\cline{5-10}
\cline{2-10}
\thead{T1} & 2 & 0 & 0 & 5 & 2/2 & 2 & -- & 0 & -- \\ \hline
\thead{T2} & 2 & 1 & 1 & 2 & 2/2 & 1 & 1/1 & 1 & 1/1 \\ \hline
\thead{T3} & 2 & 3 & 1 & 4 & 0 & 2 & 1/3 & 1 & 1/1 \\ \hline
\thead{T4} & 18 & 0 & 0 & 2 & 2/8 & 4 & -- & 0 & -- \\ \hline
\thead{T5} & 5 & 0 & 0 & 3 & 3/5 & 1 & -- & 0 & -- \\ \hline
\thead{T6} & 4 & 0 & -- & 1 & 1/4 & 1 & -- & 1 & 0 \\ \hline
\thead{T7} & 1 & 0 & 1 & 1 & 1/1 & 1 &--. & 1 & 1/1 \\ \hline
\thead{T8} & 4 & 0 & -- & 1 & 1 & 1/4 & -- & 1 & 0 \\ \hline
\end{tabular}
\end{table}

\end{document}


-
You have a spurious space in T 2 ;). –  cfr Apr 21 '14 at 18:09
@cfr: Thanks. Updated. –  Bernard Apr 21 '14 at 18:18