116

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}
LearnToGrow
  • 2,579

8 Answers8

186

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

karlkoeller
  • 124,410
25

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

15

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}
  • 6
    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
  • While this works, the actual reason it works is that \parfillskip=0pt plus 1fil by default so the additional hskip at the end of the paragraph compensate for the initial \hfil, in my opinion it's not recommended to rely on this behavior. ⟨\parfillskip is a TeX primitive, see TeXbook or TeX by Topic for explanation.⟩ – user202729 Apr 11 '23 at 14:17
3

It’s easy peasy with tblr environment of the new LaTeX3 package tabularray: you can specify horizontal alignment, vertical alignmnet and column width at the same time in the powerful Q columns.

\documentclass[conference]{IEEEtran}

\usepackage{tabularray}

\begin{document}

\begin{table} \centering \begin{tblr}{|Q[c,2.5cm]|Q[c,2.5cm]|Q[c,2.5cm]|} \hline Reconstruction strategy & aa & bb(%) \ \hline Classic & 3342 voxels & 68 % \ \hline VC & 4296 voxels & 87 % \ \hline Vm=7 & 4745 voxels & 96 % \ \hline \end{tblr} \newline\newline \caption{title}\label{tab1} \end{table}

\end{document}

enter image description here

L.J.R.
  • 10,932
1

For me using only \hfil followed by the text works perfect

\begin{center}
\begin{tabular}{ |p{5cm}|p{3cm}|p{3cm}|p{3cm}|  }
\hline
\multicolumn{4}{|c|}{Comparison table} \\
\hline
Features & \hfil TreeMind & \hfil Calm & \hfil Headspace \\
\hline
Breathing exercises & \hfil \checkmark & \hfil \checkmark & \hfil \checkmark \\
Meditation courses & \hfil \checkmark & \hfil \checkmark & \hfil \checkmark \\
User's Feedback & \hfil \checkmark & \hfil \checkmark & \hfil \checkmark \\
Virtual trainer  & \hfil \checkmark & \hfil \checkmark & \hfil \checkmark \\
Sleep stories & \hfil \checkmark & \hfil \checkmark & \hfil \checkmark \\
\hline
\end{tabular}
\end{center}

enter image description here

Dakata
  • 111
1

A partial solution. It can help when you want to center text everywhere but in the last column (!)

Just add \centering in the cell you want centered text

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

enter image description here

PS: It would be nice, if somebody says how to solve the error message of \centering in the last column.

1

If you want fixed width horizontal columns with horizontally centered, single line entries, youmay be interested in array's wc column type:

\documentclass[conference]{IEEEtran}
\usepackage{array}
\begin{document}
\begin{table}
  \begin{tabular}{|>{\centering\arraybackslash}p{2.5cm}|wc{2.5cm}|wc{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}
\end{table}
\end{document}
leandriis
  • 62,593
0

I have added a phantom extra column as p{0cm} at the end of the table for using \centering trick in it.

\begin{table}
\centering
\begin{tabular}{p{5cm}p{3cm}p{3cm}p{3cm}p{0cm}}
\hline \centering 1 &\centering 2 &\centering 3&\centering 4& \\ 
\hline \centering 5  &\centering 6&\centering 7 &\centering 8 &\\
\hline
\end{tabular}
Roland
  • 6,655
Dan
  • 1
  • If you want to horizontally center all contents inside of a p type column, it is easier to use >{\centering\arraybackslash}p{5cm} instead of adding \centering to every single cell. (Needs the array package) If you want multiple of these columns in your table, you may want to consider defining your own column type using \newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}. You can then use C{5cm} instead of p{5cm}. With both of these suggestions, an empty extra column at the end of the table is not needed. – leandriis Jun 20 '21 at 12:55