# centering content of first row in a tabularx include hyphenation

I want the first row in a tabularx to be the headlines. So the content of the first row should be centered, bold and with hyphenation.

\documentclass{scrartcl}
\usepackage{xltxtra}
\usepackage{polyglossia}
\setdefaultlanguage{english}

\usepackage{tabularx}

\begin{document}
\begin{tabularx}{4cm}{|X|X|}
\hline
\centering\textbf{A}&
\hline
a&b\\
\hline
\end{tabularx}
\end{document}


When I use centering for the second column head I get a missplaced align error message.

I know a workaround with \multicolumn{1}{c} or \thead. But both of them deactivte hyphenation.

• Use \tabularnewline instead of \\, because \centering modifies the meaning of \\; this has no consequence in the first column, but of course can't work in the final one. – egreg Aug 28 '16 at 20:43

I suggest you load the ragged2e package as well and define a new column type (after loading tabularx) via the instruction

\newcolumntype{C}{>{\Centering\arraybackslash}X}


Then, use the instruction

\begin{tabularx}{4cm}{|X|C|}


to define the table's structure.

A full MWE:

\documentclass{scrartcl}
\usepackage{xltxtra}
\usepackage{polyglossia}
\setdefaultlanguage{english} % since the MWE is in English...

\usepackage{tabularx,ragged2e}
\newcolumntype{C}{>{\Centering\arraybackslash}X}

\begin{document}
\begin{tabularx}{4cm}{|X|C|}
\hline
\centering\textbf{A}&
\hline
a&b\\
\hline
\end{tabularx}
\end{document}


Addendum to address the OP's follow-up comment and clarification: If only a cell but not the remaining entries in a column should be centered, use a \multicolumn "wrapper" to override the column type for the cell in question. In the code below, the C column type is defined as above, viz., \newcolumntype{C}{>{\Centering\arraybackslash}X}.

...
\begin{tabularx}{4cm}{|X|X|}
\hline
\centering\textbf{A}
\hline
a&b\\
\hline
\end{tabularx}
...

• This centers the whole column. But I only want to center the cells of the first row because this should be headlines. – buhtz Aug 29 '16 at 7:28
• Does hyphenation work in your multicolumn? – buhtz Aug 29 '16 at 7:59
• @buhtz - Yes it does. :-) The \Centering directive that's part of the definition of the C column type allows hyphenation. – Mico Aug 29 '16 at 8:21
• Ah! It is a uppdercase C there. It is from ragged2e. – buhtz Aug 29 '16 at 8:27