76

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
reprogrammer
  • 14,298
  • 16
  • 57
  • 93
  • Similar question: [how to fix the width of the columns in the latex table? - TeX - LaTeX Stack Exchange](https://tex.stackexchange.com/questions/51009/how-to-fix-the-width-of-the-columns-in-the-latex-table) – user202729 Oct 16 '21 at 06:30

3 Answers3

106

\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"

Will Robertson
  • 62,540
  • 32
  • 99
  • 117
Mica
  • 18,501
  • 6
  • 46
  • 43
  • 3
    use raggedleft instead of centering – Franck Dernoncourt Jul 29 '12 at 23:13
  • 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. – boczniak767 Dec 04 '17 at 19:51
  • 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. – David Dec 23 '21 at 19:01
7

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.)

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
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

MattAllegro
  • 6,455
  • 5
  • 45
  • 52