1

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}&
    \textbf{B head centered with hyphenation please}\\
    \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.

  • 1
    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
3

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:

enter image description here

\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}&
    \textbf{B head centered with hyphenation please}\\
    \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} 
\multicolumn{1}{C|}{\textbf{B head centered with hyphenation please}}\\
\hline
a&b\\
\hline
\end{tabularx}
...
| improve this answer | |
  • 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
  • 1
    @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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