# Align columns correctly in multicolumn environment [duplicate]

I have the following table:

\documentclass[a4paper,11pt]{report}

\usepackage[english]{babel}
\usepackage{booktabs}

\begin{document}

\begin{tabular}{|cc|ccc|}
\hline
\multicolumn{2}{|c|}{Parameters} & \multicolumn{3}{|c|}{This is some long text about precision and recall}\\
\hline
n & p &S& P & R\\
\hline
3000 & 0.5 & 3000 & 0.01 & 1.00\\
3000 & 0.1 & 3000 & 0.01 & 1.00\\
3000 & 0.008 & 30 & 1.00 & 1.00\\
\hline
\end{tabular}

\end{document}


As you can see the variables S, P and R are not aligned correctly. I want them each to take the same space. How can I do this?

## marked as duplicate by Heiko Oberdiek, Malipivo, user31729, Christoph, JesseMay 4 '14 at 10:14

This is one possible where each column is defined by P style (thus array package is required) in which 2cm is assumed; then the width of the 3 columns is determined by dimexpr where 6cm comes from 3 x 2cm.

Update: The OP tries m{1cm}, so the 6cm should be changed to 3cm.

Code

\documentclass[border=5pt]{standalone}%[a4paper,11pt]{report}

\usepackage[english]{babel}
\usepackage{booktabs,array,multirow}

\newcolumntype{M}{>{\centering\arraybackslash}m{1cm}}
\newcolumntype{P}{>{\raggedright\arraybackslash}p{2cm}}

\begin{document}

\begin{tabular}{|cc|*{3}{M}|}
\hline
\multicolumn{2}{|c|}{\multirow{2}{*} {Parameters}} & \multicolumn{3}{p{\dimexpr 3cm+2\arrayrulewidth+4\tabcolsep\relax}|}{This is some long text about precision and recall}\\
\hline
n & p &S& P & R\\
\hline
3000 & 0.5 & 3000 & 0.01 & 1.00\\
3000 & 0.1 & 3000 & 0.01 & 1.00\\
3000 & 0.008 & 30 & 1.00 & 1.00\\
\hline
\end{tabular}

\end{document}


So if 3cm is assumed for S,P,R column, then 6cm should be changed to 9cm.

Code

\documentclass[border=5pt]{standalone}%[a4paper,11pt]{report}

\usepackage[english]{babel}
\usepackage{booktabs,array}

\newcolumntype{P}{>{\raggedright\arraybackslash}p{2cm}}

\begin{document}

\begin{tabular}{|cc|*{3}{P}|}
\hline
\multicolumn{2}{|c|}{Parameters} & \multicolumn{3}{p{\dimexpr 6cm+2\arrayrulewidth+4\tabcolsep\relax}|}{This is some long text about precision and recall}\\
\hline
n & p &S& P & R\\
\hline
3000 & 0.5 & 3000 & 0.01 & 1.00\\
3000 & 0.1 & 3000 & 0.01 & 1.00\\
3000 & 0.008 & 30 & 1.00 & 1.00\\
\hline
\end{tabular}

\end{document}

• Thank you for your answer! I managed almost the same thing by changing the definition \begin{tabular}{|cc|m{1cm}m{1cm}m{1cm}|m{1cm}m{1cm}m{1cm}|}. But it still bugs me that I cannot align the lettets P,R,S at center... – Paris P May 4 '14 at 7:52
• Hi, Why do you have 6 of m{1cm}? I just tried and the result is as expected. Did you define \newcolumntype{M}{>{\centering\arraybackslash}m{1cm}} and change the 6cm to 3cm? – Jesse May 4 '14 at 8:01
• I'm sorry my original table has 8 columns, I just simplified it to ask the question here. The correct one for this table posted here would be \begin{tabular}{|cc|m{1cm}m{1cm}m{1cm}|} – Paris P May 4 '14 at 8:04

This is how I would realize it:

\documentclass[a4paper,11pt]{report}

\usepackage[english]{babel}
\usepackage{array}
\usepackage{booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}
\begin{tabular}{C{1.5cm}C{1.5cm}C{2.5cm}C{2.5cm}C{2.5cm}}
\toprule
\multicolumn{2}{c}{Parameters} & \multicolumn{3}{c}{This is some long text about precision and recall}\\
\midrule
n & p & S& P & R\\
\midrule
3000 & 0.5 & 3000 & 0.01 & 1.00\\
3000 & 0.1 & 3000 & 0.01 & 1.00\\
3000 & 0.008 & 30 & 1.00 & 1.00\\
\bottomrule
\end{tabular}
\end{document}