2

I have a problem with the the aligning of a tabular. I used the code

\begin{center}
\begin{tabular}{|m{1.5cm}|m{1.5cm}||m{1.5cm}|m{1.5cm}|}
\hline
1& 2 & 7 & 8 \\
0 & 89 & 100 & 55\\
4 & 6  & 4999& 055\\
\hline
\end{tabular}
\end{center}

and I want the elements centered. What command I should use?

  • do you really want centering here? It is more normal to make numeric columns be aligned so that the digits are in vertical columns. – David Carlisle Dec 15 '15 at 17:00
4

There is no need for the m-type columns, since they're intended for vertical alignment of cell contents. You seem to be interested in a centred p-type column:

enter image description here

\documentclass{article}
\usepackage{array,booktabs}
\begin{document}
\begin{center}
  \begin{tabular}{ *{4}{>{\centering\arraybackslash}p{1.5cm}} }
    \toprule
    1 &  2 &   7  &  8  \\
    0 & 89 &  100 &  55 \\
    4 &  6 & 4999 & 055 \\
    \bottomrule
  \end{tabular}
\end{center}
\end{document}

The above suggests a number of things:

  1. Using booktabs for tabular presentation;

  2. Use a short-hand notation for duplicate column specifications: *{<num>}{<col spec>}; and

  3. Using array to insert content with every cell in a column with the notation >{<before>} (or <{<after>}).


If you're into aligning numbers at the least significant digit, then consider using siunitx:

enter image description here

\documentclass{article}
\usepackage{booktabs,siunitx}
\begin{document}
\begin{center}
  \sisetup{table-column-width=15mm}
  \begin{tabular}{
      S[table-format=1]%    x
      S[table-format=2]%   xx
      S[table-format=4]% xxxx
      S[table-format=3]%  xxx
    }
    \toprule
    1 &  2 &    7 &   8 \\
    0 & 89 &  100 &  55 \\
    4 &  6 & 4999 & 055 \\
    \bottomrule
  \end{tabular}
\end{center}
\end{document}

Note that 0-prefixes in numbers are removed (which it should).

  • 1
    I would tend to write \newcolumntype{C}{>{\centering\arraybackslash}p{1.5cm}<{}} and then \begin{tabular}{CCCC} but otherwise I fully agree. – Denis Dec 15 '15 at 16:44
  • I do not understand how can Iadd columns? – Sakis Dec 15 '15 at 17:12
  • 1
    @VanityFair: You can add columns around my *{<num>}{<col spec>} specification. Or, you can increase <num> to set the number of columns you need. – Werner Dec 15 '15 at 18:01
1
\documentclass{article}
\usepackage{array,booktabs,ragged2e}
\newcolumntype{C}[1]{>{\Centering}p{#1}}
\begin{document}
    \begin{center}
        \begin{tabular}{ *4{C{1.5cm}} }% or { C{1.5cm} C{1.5cm} ... }
            \toprule
            1 &  2 &   7  &  8  \\
            0 & 89 &  100 &  55 \\
            4 &  6 & 4999 & 055 \\
            \bottomrule
        \end{tabular}
    \end{center}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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