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

AlignmentLatexTabular

Alignment Problem Overview


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.

Alignment Solutions


Solution 1 - Alignment

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

Solution 2 - Alignment

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

Solution 3 - Alignment

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionreprogrammerView Question on Stackoverflow
Solution 1 - AlignmentMicaView Answer on Stackoverflow
Solution 2 - AlignmentMatt BallView Answer on Stackoverflow
Solution 3 - AlignmentMattAllegroView Answer on Stackoverflow