# Center align vertically and horizontally in table with multicolumn and multirow

I want to create the table that is aligned vertically and horizontally and that is include multirow and multicolumn.

I used below script.

\begin{table}[!h]
\centering
\caption{My caption}
\begin{tabular}{|>{\centering\arraybackslash}m{1.5cm}|>{\centering\arraybackslash}m{1.5cm}|>{\centering\arraybackslash}m{1.5cm}|>{\centering\arraybackslash}m{1.5cm}|>{\centering\arraybackslash}m{1.5cm}|>{\centering\arraybackslash}m{1.5cm}|>{\centering\arraybackslash}m{1.5cm}|}
\hline
\multirow{2}{*}{(\%)} & \multicolumn{2}{c|}{A} & \multicolumn{2}{c|}{B} & \multicolumn{2}{c|}{C}\\[25pt] \cline{2-7}
& a         & b          & c         & d          & e         & f             \\[25pt] \hline
1                     & a1        & b1         & c1        & d1         & e1        & f1            \\[25pt] \hline
2                     & a2        & b2         & c2        & d2         & e2        & f2            \\[25pt] \hline
3                     & a3        & b3         & c3        & d3         & e3        & f3            \\[25pt] \hline
4                     & a4        & b4         & c4        & d4         & e4        & f4            \\[25pt] \hline
5                     & a5        & b5         & c5        & d5         & e5        & f5            \\[25pt] \hline
\end{tabular}\end{table}


It create below table.

The line with multirow and multicolumn does not aligned.

And the last row is also not aligned.

Thank you.

• Welcome to TeX.SX! Please help us (and also you) and add a full minimal working example (MWE), that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass and ending with \end{document}. May 24, 2018 at 14:30
• Just using the array and multirow packages will not (nor should) reproduce the image shown. Every entry should be at the top of the cell. May 24, 2018 at 14:39

I propose this far from perfect solution, based on the cellspace package, which lets you define minimal vertical spacing at the top and bottom of cells in columns with sêcifier prefixed with the letter S (or C if you use siunitx). Unfortunately this simple solution, for some reason, doesn't work for the first column, so I had to use a work-around based on makecell:

\documentclass{article}
\usepackage{multirow}
\usepackage{array, caption, makecell}
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{19pt}
\setlength{\cellspacebottomlimit}{16pt}

\begin{document}

\begin{table}[!h]
\centering
\caption{My caption}
\begin{tabular}{|>{\centering\arraybackslash}m{1.5cm}|>{\centering\arraybackslash}S{m{1.5cm}}|*{6}{>{\centering\arraybackslash}S{m{1.5cm}}|}}
\hline
\multirow{4.2}{*}{(\%)} & \multicolumn{2}{Sc|}{A} & \multicolumn{2}{Sc|}{B} & \multicolumn{2}{Sc|}{C}\\
\cline{2-7}
& a & b & c & d & e & f \\
\hline
\makecell{\\1} & a1 & b1 & c1 & d1 & e1 & f1 \\
\hline
\makecell{\\2} & a2 & b2 & c2 & d2 & e2 & f2 \\
\hline
\makecell{\\3} & a3 & b3 & c3 & d3 & e3 & f3 \\
\hline
\makecell{\\4} & a4 & b4 & c4 & d4 & e4 & f4 \\
\hline
\makecell{\\5} & a5 & b5 & c5 & d5 & e5 & f5 \\
\hline
\end{tabular}
\end{table}

\end{document}


Making a cell horizontally aligned can be achieved by using \begin{tabular}[ c | c ] or \multicolumn{2}{c}. By applying \usepackage{array} in the preamble and \begin{tabular}[ m{5em} | m{5em} ] produces vertically aligned text in the table.

If you want to make a cell both vertically and horizontally aligned than you should include >{\centering\arraybackslash} in front of cell format.

Here is an example:

 \begin{table}[h!]
\renewcommand{\arraystretch}{1}
\centering
\caption{My caption}
\begin{tabular}{|m{1.5cm}|>{\centering\arraybackslash}m{1.5cm}|>{\centering\arraybackslash}m{1.5cm}|m{1.5cm}|m{1.5cm}|m{1.5cm}|m{1.5cm}|}
\hline
\multirow{2}{1.5cm}{(\%)} & \multicolumn{2}{c|}{A} & \multicolumn{2}{c|}{B} & \multicolumn{2}{c|}{C}\\ \cline{2-7}
& a         & abra cadabra          & c         & d          & e         & f             \\ \hline
1                     & a1        & abra cadabra         & c1        & d1         & e1        & f1            \\ \hline
2                     & a2        & abra cadabra         & c2        & d2         & e2        & f2            \\ \hline
3                     & a3        & abra cadabra         & c3        & d3         & e3        & f3            \\ \hline
4                     & a4        & abra cadabra         & c4        & d4         & e4        & f4            \\ \hline
5                     & a5        & abra cadabra         & c5        & d5         & e5        & f5            \\ \hline
\end{tabular}
\renewcommand{\arraystretch}{1}
\end{table}


The following picture is the result of the example.
