Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

\begin{tabularx}{\textwidth}{l|X}
  \multicolumn{2}{c}{Headline centered} \\
  \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 ?

share|improve this question

3 Answers 3

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.

share|improve this answer
    
+1 for mentioning the tabular* environment. :-) –  Mico Sep 25 '12 at 12:11
2  
@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}
  \multicolumn{2}{c}{Headline centered} \\
  \hline
  Column1 & Column2 which should be centered
\end{tabularx}

\end{document}
share|improve this answer

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]}
  \multicolumn{2}{c}{Headline centered} \\
  \hline
  Column1 & Column2 which should be centered
\end{tabu}

\end{document}

enter image description here

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]}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.