# How to get column alignment in tabularx? [duplicate]

This question already has an answer here:

When I put X in column specifier, how can I define column formatting? How can I get center alignment? Currently I am putting \centering, but that is not a proper solution.

This is my code:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\usepackage{booktabs,tabularx}
\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry

\begin{document}

\begin{table}[t]
\caption{Case studies}
\centering
\begin{tabularx}{\textwidth}{p{4cm}*{5}{X}}
\toprule
\centering Nature of unreliability & \centering Sender SST & \centering Receiver SST & \centering Implementation SST &  Retransmission Bound \\

\midrule

Noisy (Single fixed error message) & DSST & DSST & DSST & Unbounded \\
\bottomrule
\end{tabularx}
\label{case-studies}
\end{table}

\end{document}

-

## marked as duplicate by Joseph Wright♦Jul 21 '14 at 6:24

\centering is the proper solution. But if you use it in the last column you should use \tabularnewline to end the row of the tabular. –  Ulrike Fischer Feb 7 '13 at 12:27
If you want it to apply to the whole column you can use >{\centering \arraybackslash}X as it suggests in the tabularx manual. –  David Carlisle Feb 7 '13 at 12:41
Please, clarify if you need to center align only one row or all of them. –  egreg Feb 7 '13 at 13:41
@DavidCarlisle Please check my answer -- for whatever reason, >{\centering \arraybackslash}X does not suffice. –  lockstep Feb 7 '13 at 14:20

My answer to Fixed width column with text centered/raggedright/raggedleft in tabular? can also be applied in slightly modified form to X columns. (Note that omitting \let\newline\\ will result in text that is not properly centered.)

\documentclass{article}

\usepackage{tabularx}

\usepackage{array}
\newcolumntype{Z}{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}X}

\begin{document}

\begin{tabularx}{\linewidth}{| c | c | Z |}
Abc & Bcd & Cde \\
Abc & Bcd & A long cell with text that is centered \newline
and allows manual line breaks \\
\end{tabularx}

\end{document}


-
yes \centering only redefines \ not \newline so if you need manual breaks you can do as here, or don't use \arraybackslash and use \ for centering break and \tabularnewline for end of table row, or perhaps simplest you can use a blank line to force a new centred parapgraph –  David Carlisle Feb 7 '13 at 16:45