# How to center text in tabularx? [duplicate]

Possible Duplicate:
Centering in tabularx and X columns

I was using \begin{tabular} to insert a table in my article. The titles for columns were long which caused the table to extend out across the paper boundary. I searched online and found that {tabularx} will wrap the text. I used it which solved the problem. But I want my content to be centered. How do I achieve this in the {tabularx} environment? I am new to LaTeX and this is my first article. I have copied my script below:

\begin{table}[ht]
\caption{Clear Zone Deficiency and Crash Severity}
\centering
\begin{tabularx} {\linewidth} {X X X X X X X}
\hline\hline
CLEAR ZONE DEFICIENCY & TOTAL CRASHES & FATAL & INJURY & PDO & \% CRASHES & \% FATAL \\ [0.5ex]
\hline
0ft-4ft & 4 & 0 & 2 & 2 & 7.84 & 0 \\
4ft-8ft & 14 & \textbf{1} & 4 & 9 & 27.45 & 9.09 \\
8ft-12ft & 15 & \textbf{1} & 5 & 9 & 29.41 & 9.09 \\
12ft + & 18 & \textbf{9} & 4 & 5 & 35.29 & \textbf{81.82}\\
\hline
TOTAL & 51 & 11 & 15 & 25 & 100 & 100 \\ [1ex]
\hline
\end{tabularx}
\label{table:CZseverity}
\end{table}

-
–  tohecz Jan 9 at 17:44
If you do want to use tx rather than ty then replace each X by >{\centering\arraybackslash}X –  David Carlisle Jan 9 at 17:56

## marked as duplicate by ArTourter, Kurt, Werner, Guido, Stefan Kottwitz♦Jan 10 at 7:18

If you use the tabulary instead, and then use C instead of X, it will automatically adjust the table widths and center the contents:

\documentclass{article}

\begin{document}

\begin{table}[ht]
\caption{Clear Zone Deficiency and Crash Severity}
\centering
\begin{tabulary} {\linewidth} {C C C C C C C}
\hline\hline
CLEAR ZONE DEFICIENCY & TOTAL CRASHES & FATAL & INJURY & PDO & \% CRASHES & \% FATAL \\ [0.5ex]
\hline
0ft-4ft & 4 & 0 & 2 & 2 & 7.84 & 0 \\
4ft-8ft & 14 & \textbf{1} & 4 & 9 & 27.45 & 9.09 \\
8ft-12ft & 15 & \textbf{1} & 5 & 9 & 29.41 & 9.09 \\
12ft + & 18 & \textbf{9} & 4 & 5 & 35.29 & \textbf{81.82}\\
\hline
TOTAL & 51 & 11 & 15 & 25 & 100 & 100 \\ [1ex]
\hline
\end{tabulary}
\label{table:CZseverity}
\end{table}

\end{document}


If you find some of your titles have unnecessary broken words and you don't need word wrapping in those columns, just use c instead of C.

-

I suggest several changes:

• I would load the package {booktabs} and use it's \toprule, \midrule and \bottomrule.

• Instead of 0ft-4ft you should 0\,ft--4\,ft (thin space between value and unit, n-dash instead of hyphen).

• You can pass \centering\arraybackslash to the column X by saying >{\centering\arraybackslash}X, the shortcut *{n}{spec} repeats column specification spec exactly n times.

Here's the code:

\begin{table}[ht]
\caption{Clear Zone Deficiency and Crash Severity}
\centering
\begin{tabularx}{\linewidth}{*{7}{>{\centering\arraybackslash}X}}
\toprule
CLEAR ZONE DEFICIENCY & TOTAL CRASHES & FATAL & INJURY & PDO & \% CRASHES & \% FATAL \\ [0.5ex]
\midrule[\heavyrulewidth]
0\,ft--4\,ft & 4 & 0 & 2 & 2 & 7.84 & 0 \\
4\,ft--8\,ft & 14 & \textbf{1} & 4 & 9 & 27.45 & 9.09 \\
8\,ft--12\,ft & 15 & \textbf{1} & 5 & 9 & 29.41 & 9.09 \\
$>{}$12\,ft & 18 & \textbf{9} & 4 & 5 & 35.29 & \textbf{81.82}\\
\midrule
TOTAL & 51 & 11 & 15 & 25 & 100 & 100 \\ [1ex]
\bottomrule
\end{tabularx}
\label{table:CZseverity}
\end{table}

-