在表格列标题中换行文本并使表格宽度适合页面宽度

在表格列标题中换行文本并使表格宽度适合页面宽度

如何获得一个表格,其中第 2 列的标题被换行,同时保持表格的宽度 = textwidth?从附图中可以看到,我为第二列设置了一个长标题,这使得该列非常宽,导致第一列中的文本(实际条目,而不是标题)换行。我想从更一般的意义上讲,我希望优先换行列标题,而不是换行实际列条目。(我没有坚持使用 tabularx;如果可行,我很乐意尝试任何其他软件包。)

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

我现在得到了什么

答案1

这是一个使用makecell包的解决方案,可以预先格式化列标题和多行标题。我还使用该booktabs包来获得更好看的水平规则和不太紧密的垂直间距。最后numprint允许在单元格中使用科学符号。

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

在此处输入图片描述

答案2

我会使用siunitx和 而tabular*不是tabularx,因为这样你就能得到完美的数字输出。第一个例子是你如何使用tabularx,但请注意,你必须手动更正数字的输入。使用这种siunitx方法,你可以直接输入数字。

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

在此处输入图片描述

答案3

仅在需要时使用X列:

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

在此处输入图片描述

相关内容