[Tex/LaTex] Wrap text in table column header AND fit table width to pagewidth

tabularx

How do I get a table where the header in column 2 is wrapped, while keeping the width of the table = textwidth? As you can see from the attached figure, I have a long title for the 2nd column, which makes the column very wide, resulting in wrapping of the text (actual entries, not the title) in the first column. I guess in a more general sense, I want to prioritize wrapping of column titles over wrapping of actual column entries. (I am not stuck on using tabularx; I am happy to try any other package if it works.)

\documentclass[12pt]{report} 
\usepackage{tabularx} 

\begin{document}

\begin{table}[h]
\begin{tabularx}{\textwidth}{XXXX}
\hline
\multicolumn{4}{c}{xxx xx xxxx}                                                                                           \\ \hline
\textbf{miRNA}  & \multicolumn{1}{l}{\textbf{logFC (ABCDEF vs ABCFED)}} & \textbf{PValue} & \textbf{False Discovery Rate}    \\ \hline
abcd efg hijka  & -1.854                                                & 9.52E-11        & 8.41E-10                         \\
abcd efg hijka  & -1.815                                                & 3.38E-09        & 2.42E-08                         \\ \hline
\end{tabularx}
\end{table}
a lot of text here to show text width a lot of text here to show text width a lot of text here to show text width a lot of text here to show text width a lot of text here to show text width a lot of text here to show text width a lot of text here to show text width

\end{document}

what I get now

Best Answer

Here is a solution using the makecell package that enables pre-formatting of column heads and multine heads. I also used the booktabs package to have better-looking horizontal rules and a less tight vertical spacing. Finally numprint allows for scientific notations in cells.

\documentclass[12pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{fourier}
\usepackage{array}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{makecell}

\newcolumntype{Z}{ >{\centering\arraybackslash}X }

\renewcommand\theadfont{\bfseries}
%\renewcommand\theadalign{cc}
\usepackage[autolanguage, np]{numprint}

\begin{document}

\begin{table}[!h]
\setlength\tabcolsep{4pt}
\begin{tabularx}{\textwidth}{*{4}{Z}}
\toprule
\multicolumn{4}{c}{xxx xx xxxx}   \\
\midrule
\thead{miRNA}  & \thead{logFC \\ (ABCDEF vs \\ ABCFED) } & \thead{PValue} & \thead{False \\Discovery\\ Rate} \\%
\midrule
abcd efg hijka  & $-1.854$                                                & \np{9.52E-11}        & \np{8.41E-10}                         \\
abcd efg hijka  & $-1.815$                                                & \np{3.38E-09}        & \np{2.42E-08}                         \\
 \bottomrule
\end{tabularx}
\end{table}
a lot of text here to show text width a lot of text here to show text width a lot of text here to show text width a lot of text here to show text width a lot of text here to show text width a lot of text here to show text width a lot of text here to show text width

\end{document} 

enter image description here

Related Question