How to center cell contents of a LaTeX table whose columns have fixed widths?
Asked Answered
W

3

78

Consider the following piece of LaTeX code:

\begin{tabular}{p{1in}p{1in}} 
A & B\\ 
C & D\\
\end{tabular}

How can I make the contents of each cell aligned in the center of the cell rather than the left? Note that I want to make sure that the widths of my columns are fixed, so I cannot use the "c" position attribute instead of "p{.1in}" to center my cell contents.

Winnifredwinning answered 31/8, 2009 at 14:41 Comment(1)
M
108

\usepackage{array} in the preamble

then this:

\begin{tabular}{| >{\centering\arraybackslash}m{1in} | >{\centering\arraybackslash}m{1in} |}

note that the "m" for fixed with column is provided by the array package, and will give you vertical centering (if you don't want this just go back to "p"

Mcginn answered 31/8, 2009 at 16:11 Comment(3)
use raggedleft instead of centeringSaipan
Why such statement not make text in second column aligned to top \begin{tabular}{| >{\centering\arraybackslash}m{1in} | >{\centering\arraybackslash}p{1in} |}? The same is for b. All cells are vertically aligned like first statement (here m{1in}) sets.Aitken
any reason why this would not be working for me in Overleaf? wondering if there's been any updates since this was answered back in '09 that would make it out of date.Pantomime
V
8

You can use \centering with your parbox to do this.

More info here and here.

(Sorry for the Google cached link; the original one I had doesn't work anymore.)

Vague answered 31/8, 2009 at 14:47 Comment(0)
C
1

I think that this answer is the best source. You can declare the newcolumntype in the header and then use it when necessary:

\documentclass{article}
\usepackage{array}

\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}
\begin{tabular}{|C{1in}|C{1in}|}
A & B\\
C & D\\
\end{tabular}
\end{document}

I only added the vertical lines | to make the column width (1in) and the centering more evident in the output:

screenshot of output

Coxswain answered 26/5, 2022 at 12:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.