55

How can I center the column values in this code?

\documentclass[conference]{IEEEtran}
\begin{document}
\begin{table}
  \centering
  \begin{tabular}{|p{2.5cm}|p{2.5cm}|p{2.5cm}|}
    \hline
    Reconstruction strategy & aa          & bb( \%) \\ \hline
    Classic                 & 3342 voxels & 68 \%   \\ \hline
    VC                      & 4296 voxels & 87 \%   \\ \hline
    V m=7                   & 4745 voxels & 96 \%   \\ \hline
  \end{tabular}
  \newline\newline 
  \caption{title}\label{tab1}
\end{table}
\end{document}
  • 2
    \begin{tabular}{ccc} – Crissov Feb 3 '14 at 19:54
  • 4
    If you use c, then you can't define / set a horizontal size limit for the column (such as 2.5cm in OP's example). If you use c, lines won't be automatically wrapped inside the table cells. – Thiago Perrotta Dec 24 '16 at 18:28
87

You have to load the package array, define a new column type with horizontal centering

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

and use this one (P) instead of p

\documentclass[conference]{IEEEtran}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}

\begin{table}
  \centering
  \begin{tabular}{|P{2.5cm}|P{2.5cm}|P{2.5cm}|}
    \hline
    Reconstruction strategy & aa          & bb( \%) \\ \hline
    Classic                 & 3342 voxels & 68 \%   \\ \hline
    VC                      & 4296 voxels & 87 \%   \\ \hline
    V m=7                   & 4745 voxels & 96 \%   \\ \hline
  \end{tabular}
  \newline\newline
  \caption{title}\label{tab1}
\end{table}
\end{document} 

Output:

enter image description here

If you also want vertical centering, use m instead of p:

\documentclass[conference]{IEEEtran}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table}
  \centering
  \begin{tabular}{|M{2.5cm}|M{2.5cm}|M{2.5cm}|}
    \hline
    Reconstruction strategy & aa          & bb( \%) \\ \hline
    Classic                 & 3342 voxels & 68 \%   \\ \hline
    VC                      & 4296 voxels & 87 \%   \\ \hline
    V m=7                   & 4745 voxels & 96 \%   \\ \hline
  \end{tabular}
  \newline\newline
  \caption{title}\label{tab1}
\end{table}
\end{document} 

Output:

enter image description here

9

If the width of the columns is not predefined, the most simple solution is to use c instead of p:

\documentclass[conference]{IEEEtran} 
\begin{document}
\begin{table}
 \centering
 \begin{tabular}{|c|c|c|}
 \hline
 Reconstruction strategy & aa          & bb( \%) \\ \hline
 Classic                 & 3342 voxels & 68 \%   \\ \hline
 VC                      & 4296 voxels & 87 \%   \\ \hline
 V m=7                   & 4745 voxels & 96 \%   \\ \hline
 \end{tabular}
\newline\newline 
\caption{title}\label{tab1}
\end{table}
\end{document}

Output:

enter image description here

  • Thanks! What to do for vertical alignment when the width isn't defined? – Ruchir Jan 10 '18 at 3:11
3

A very simple solution seems to work!

use tabular and p{...} and add \hfil before cell text. it seems that the text become centered! as an example:

\begin{table}
    \begin{center}
        \begin{tabular}{c|p{2.5cm}|p{2.5cm}}
        \multirow{2}{*} {something in multirow} & \multicolumn{2}{c}{another in multicolumn} \\
        & \hfil$Q in kg/s$ & \hfil$Q in m^3/s$ \\
        \hline
        first & \hfil7.3 & \hfil inf \\
        second & \hfil3.2 & \hfil0.5 \\
        third & \hfil inf & \hfil0 \\
        \end{tabular}
    \end{center}
\end{table}
  • 5
    Welcome to TeX.SX! Are you sure that it is centered then? Could you maybe add a code example illustrating this? – TeXnician Sep 20 '18 at 16:55
  • It works for me perfectly. Thanks! Best and simplest answer! – Infinite Dec 1 '18 at 17:06

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.