1

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}

enter image description here

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

marked as duplicate by egreg horizontal-alignment Sep 11 '16 at 12:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \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
3

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}

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.