# How to define multiple columns with specific width and centered? [duplicate]

I want to define multiple columns (using *{}{}), with specific width (using m{}), and centered (using >{\centering}). However, I am not able to combine the three of them. The MWE below can only center the first column:

\documentclass{article}
\usepackage{amsmath,array}
\begin{document}

\begin{table}
\centering
\begin{tabular}{ccc|>{\centering}*{6}{m{0.8cm}}}
\hline
i & t & k & $D_{im1}$ & $D_{im2}$ & $D_{im3}$ & $D_{ir1}$ & $D_{ir2}$ & $D_{ir3}$ \\
\hline
1 & 1 & $m$ & 0.5 & 0 & 0 & 0 & 0 & 0 \\
1 & 2 & $m$ & 0 & 0.5 & 0 & 0 & 0 & 0 \\
1 & 3 & $r$ & 0 & 0 & 0 & 0 & 0 & 0.5 \\
2 & 1 & $r$ & 0 & 0 & 0 & 0.5 & 0 & 0 \\
2 & 2 & $r$ & 0 & 0 & 0 & 0 & 0.5 & 0 \\
2 & 3 & $m$ & 0 & 0 & 0.5 & 0 & 0 & 0 \\
\hline
\end{tabular}
\end{table}

\end{document}


If I use *{6}{>{\centering}m{0.8cm}} (as I originally attempted), it does not compile.

## marked as duplicate by egreg horizontal-alignment StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Sep 11 '16 at 12:36

• \begin{tabular}{ccc|*{6}{>{\centering\arraybackslash}m{0.8cm}}}, but I strongly suggest to define a column type for this! – user31729 Sep 11 '16 at 12:30
• Your definition of with \centering will work for the first m column only, after that it is gone due to grouping. The centering must appear inside column repetition statement, i.e. *{6}{>{centering...}} – user31729 Sep 11 '16 at 12:38
• I did look up for similar questions. There was no way I was going to find that one, with such a horrible title. – luchonacho Sep 11 '16 at 12:48
• Never mind, finding sometimes similar questions is difficult – user31729 Sep 11 '16 at 12:50

I suggest to use the \newcolumntype feature and define a new columntype, say M, for this.

The direct usage of >{...}... is possible as well but sometimes error-prone, especially if many of those specialized column types appear.

\begin{tabular}{*{6}M{0.8cm}}


however is much easier (after definition of M, of course)

The decimal values should be set with S columntypes from siunitx, however, that's another question and is not addressed here.

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}

\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}

\begin{table}
\centering
\begin{tabular}{ccc|*{6}{M{0.8cm}}}
%    \begin{tabular}{ccc|*{6}{>{\centering\arraybackslash}m{0.8cm}}} % Alternative...
\hline
i & t & k & $D_{im1}$ & $D_{im2}$ & $D_{im3}$ & $D_{ir1}$ & $D_{ir2}$ & $D_{ir3}$ \\
\hline
1 & 1 & $m$ & 0.5 & 0 & 0 & 0 & 0 & 0 \\
1 & 2 & $m$ & 0 & 0.5 & 0 & 0 & 0 & 0 \\
1 & 3 & $r$ & 0 & 0 & 0 & 0 & 0 & 0.5 \\
2 & 1 & $r$ & 0 & 0 & 0 & 0.5 & 0 & 0 \\
2 & 2 & $r$ & 0 & 0 & 0 & 0 & 0.5 & 0 \\
2 & 3 & $m$ & 0 & 0 & 0.5 & 0 & 0 & 0 \\
\hline
\end{tabular}
\end{table}

\end{document}