12

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

3
  • 2
    Remove the \multicolumn{1} for the header in the second column.
    – egreg
    May 4, 2014 at 16:36
  • 1
    Thanks! I didn't really understand why that was there, tablesgenerator.com produced that table. Teaches me not to use code without understanding.
    – srao
    May 4, 2014 at 16:40
  • 2
    also don't use [h] on its own (latex normamly issues a warning and changes it to [ht] but better to use [htp] to give latex a chance not to take the float to the end of the document May 4, 2014 at 16:52

3 Answers 3

8

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

2
  • Thanks, this looks beautiful. Quick clarification: can I just use '\begin{tabularx}{\textwidth}{*{4}{Z}}' instead of \begin{tabularx}'{\textwidth}{*{4}{ >{\centering\arraybackslash}Z}}' as they both seem to do the same thing? Maybe you meant to write '\begin{tabularx}{\textwidth}{*{4}{ >{\centering\arraybackslash}X}}' there? Also, numprint is awesome!
    – srao
    May 4, 2014 at 19:44
  • 1
    Yes you can. I mistyped the new column type definition. I'll correct it at once.
    – Bernard
    May 4, 2014 at 19:57
5

I would use siunitx and tabular* instead of tabularx, because you're so able to get perfect output for the numbers. The first example is how you can do with tabularx, but note that you would have to manually correct the input for numbers. With the siunitx approach you can just input numbers as they are.

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

\newcommand{\tabheader}[1]{%
  \bfseries
  \begin{tabular}{@{}c@{}}
  \strut#1\strut
  \end{tabular}%
}

\begin{document}

\begin{table}[htp]
\centering
\begin{tabularx}{\textwidth}{*{4}{>{\raggedright\arraybackslash}X}}
\hline
\multicolumn{4}{c}{xxx xx xxxx} \\
\hline
\textbf{miRNA}  & 
  \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}

\begin{table}[htp]
\sisetup{
  exponent-product={},
  output-exponent-marker=\ensuremath{\mathrm{E}},
}
\centering
\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}}
  l
  S[table-format=-1.3]
  S[table-format=1.2E-2]
  S[table-format=1.2E-2]
  @{}
}
\toprule
\multicolumn{4}{c}{xxx xx xxxx} \\
\midrule
\tabheader{miRNA}  & 
  \tabheader{logFC (ABCDEF\\ vs ABCFED)} & 
  \tabheader{PValue} & 
  \tabheader{False\\ Discovery\\ Rate} \\
\midrule
abcd efg hijka  & -1.854 & 9.52E-11 & 8.41E-10 \\
abcd efg hijka  & -1.815 & 3.38E-09 & 2.42E-08 \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}

enter image description here

4

Use X columns only if needed:

\documentclass[12pt]{report} 
\usepackage{tabularx,ragged2e} 
\renewcommand\tabularxcolumn[1]{>{\RaggedRight}p{#1}}

\begin{document}

\begin{table}[h]
\begin{tabularx}{\textwidth}{@{} lXlX @{} }\hline
\multicolumn{4}{c}{xxx xx xxxx} \\ \hline
\textbf{miRNA}  & \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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .