117

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}
3
  • 4
    \begin{tabular}{ccc}
    – Crissov
    Commented Feb 3, 2014 at 19:54
  • 16
    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.
    – thiagowfx
    Commented Dec 24, 2016 at 18:28
  • 4
    Does this answer your question? How to create fixed width table columns with text raggedright/centered/raggedleft? [remark: the linked question is more general, but the answer there is better that it gets hyphenation and \newline correct]
    – user202729
    Commented Apr 11, 2023 at 14:41

8 Answers 8

188

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

1
  • Thanks a lot! I was about to write \centering in each cell before I saw your answer. ´;) Commented Apr 27, 2021 at 12:13
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

3
  • 1
    Thanks! What to do for vertical alignment when the width isn't defined?
    – Ruchir
    Commented Jan 10, 2018 at 3:11
  • @Ruchir I have the same problem... did you solve it? Commented Jun 28, 2022 at 12:26
  • @hellomynameisA if you want to play with vertical alignment, you've got solutions here: tex.stackexchange.com/a/113026/101631
    – sandoval31
    Commented Jun 29, 2022 at 14:47
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}
2
  • 6
    Welcome to TeX.SX! Are you sure that it is centered then? Could you maybe add a code example illustrating this?
    – TeXnician
    Commented Sep 20, 2018 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
    Commented Apr 11, 2023 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

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

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}
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}
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
    Commented Jun 20, 2021 at 12:55

Not the answer you're looking for? Browse other questions tagged .