# How to create a table with a headline centered and columns centered?

I use the following code to create a table with a centered headline.

\begin{tabularx}{\textwidth}{l|X}
\hline
Column1 & Column2 which should be centered
\end{tabularx}


Now I would like to center the second column, too. But the tabularx package needs at least one column to be X and this column will not be centered.

How can I achieve this ?

-

Use l>{\centering\arraybackslash}X as your tabular preamble.

In addition, something odd about your question struck me, you say tabularx needs an X column, which sort of implied that you don't need that?

You only need to use tabularx if you need X columns, that is multi-line entries that break to a width depending on other columns.

If your two columns are of the same type and both need single line entries do not use tabularx at all. Use

  \begin{tabular*}{\textwidth}{c@\extracolsep{\fill}}c}


tabular* is vastly more efficient than tabularx and does not require any package.

-
+1 for mentioning the tabular* environment. :-) – Mico Sep 25 '12 at 12:11
@Mico I always suggest people don't use my packages. Packages without users are much easier to maintain. – David Carlisle Sep 25 '12 at 12:51
Thank you very much. The first solution works well. The second one, with the tabular* environment, did not center the second column. Maybe I missed something. The reason I used tabularx with the X column was to fix the first column in the width and let the second column have the rest of the space. But as I am not into deep with tables too much, my attempt may not be the optimal one. :) – John Threepwood Sep 25 '12 at 17:25
@JohnThreepwood Then have a look at tabu – Sveinung Sep 25 '12 at 18:37
\documentclass{article}
\usepackage{tabularx,ragged2e}
\renewcommand\tabularxcolumn[1]{>{\Centering}p{#1}}
\begin{document}

\begin{tabularx}{\textwidth}{l|X}
\hline
Column1 & Column2 which should be centered
\end{tabularx}

\end{document}

-

You can also use the package tabu:

\documentclass{article}
\usepackage{tabu}
\begin{document}

\tabulinesep=3pt % For better spacing

\begin{tabu}to \textwidth {X[L]|X[C]}

If you want the second column to take remaining space, change the X[L] to an l (the letter, not the digit).
\begin{tabu}to \textwidth {X[L]|X[C]}