需要帮助格式化表格

需要帮助格式化表格

我根本不是 LaTaX 用户,但喜欢创建一些表格,因为它们非常出色。

我整理了一些代码(如下),但对结果并不完全满意,也无法得到我想要的,希望有人能帮助我,我想改变的是以下内容:

  1. 以“Kruskall-Wallis 群”为中心
  2. 将 x2 列稍微向左移一点
  3. 将“group”和“p”置于事后统计的中心

感谢任何帮助。

\documentclass[
  convert={
    density=300 -alpha deactivate,
    size=1080x1080,
    outext=.png
  },
]{standalone}
\usepackage{array}
\usepackage{booktabs}

\usepackage{xcolor}

\usepackage{tabularray}

\usepackage{boldline}
\usepackage{nicematrix}
\begin{document} 

{}

\begin{tabular}{cc@{}cclllllll@{}}\toprule
&  \multicolumn{0}{c}{Kruskal-Wallis  Group}  &  &  &   \multicolumn{3}{c} {Conover-Iman post-hoc}                        \\
\cmidrule (lll){2-4}  \cmidrule(rl){5-6}
 Month & Treat & x2 &  piiii  & group & p \\  
 
 \cmidrule(lr){1-1} \cmidrule (lll){2-4}  \cmidrule(lr){5-6}


  & synthesized & 10.6 & 0.001 & synthesized vs control & 0.007 & \\
   June 2022 & commercial& 7.34           & 0.05 & commercial vs control &  0.0001 \\

   & control& 1.00&  0.1 &  commercial vs synthesized &  0.415 \\

 
\bottomrule


\end{tabular}

\end{document}

答案1

这个尝试是好的,但也存在几个错误:

  • \multicolumn{0}没有什么意义;
  • \cmidrule(lll)三次指定同一件事;
  • 您定义了十一列,但只使用了六列。

这些是编码错误。接下来是印刷错误:

  • “月份”是与 Kruskal-Wallis 组和 Conover-Iman 事后同一级别的标题;
  • 2022 年 6 月应位于组首;其下方的缺失值被理解为相同的;
  • 即使列是左对齐的,子标题也应该全部位于相应列的中心。

为了分隔数据组,您可以使用一些垂直空间或\midrule,但我建议后者仅用于分隔表格中概念上不同的部分。

\documentclass{article}
\usepackage[hmargin=1cm]{geometry}
\usepackage{array}
\usepackage{booktabs}

\usepackage{xcolor}

\begin{document}

\begin{tabular}{@{}llcclc@{}}
\toprule
Month &
  \multicolumn{3}{c}{Kruskal-Wallis  Group} &
  \multicolumn{2}{c}{Conover-Iman post-hoc} \\
\cmidrule (lr){2-4}  \cmidrule(l){5-6}
& \multicolumn{1}{c}{Treat} & x2 &  piiii  & \multicolumn{1}{c}{group} & p \\  
\midrule
June 2022
& synthesized & 10.6 & 0.001 & synthesized vs control    &  0.007 \\
& commercial  & 7.34 & 0.05  & commercial vs control     &  0.0001 \\
& control     & 1.00 & 0.1   & commercial vs synthesized &  0.415 \\
\addlinespace
July 2022
& synthesized & 10.6 & 0.001 & synthesized vs control    &  0.007 \\
& commercial  & 7.34 & 0.05  & commercial vs control     &  0.0001 \\
& control     & 1.00 & 0.1   & commercial vs synthesized &  0.415 \\
\addlinespace
August 2022
& synthesized & 10.6 & 0.001 & synthesized vs control    &  0.007 \\
& commercial  & 7.34 & 0.05  & commercial vs control     &  0.0001 \\
& control     & 1.00 & 0.1   & commercial vs synthesized &  0.415 \\
\bottomrule
\end{tabular}

\end{document}

在这里我使用它article来避免复杂性和后期处理。

在此处输入图片描述

一个可能的改进是将数字在小数点分隔符处对齐。

\documentclass{article}
\usepackage[hmargin=1cm]{geometry}
\usepackage{array}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{xcolor}

\begin{document}

\begin{tabular}{
  @{}
  l
  l
  S[table-format=2.2]
  S[table-format=1.3]
  l
  S[table-format=1.4]
  @{}
}
\toprule
Month &
  \multicolumn{3}{c}{Kruskal-Wallis  Group} &
  \multicolumn{2}{c}{Conover-Iman post-hoc} \\
\cmidrule (lr){2-4}  \cmidrule(l){5-6}
& \multicolumn{1}{c}{Treat} & {x2} &  {piiii}  & \multicolumn{1}{c}{group} & {p} \\  
\midrule
June 2022
& synthesized & 10.6 & 0.001 & synthesized vs control    &  0.007 \\
& commercial  & 7.34 & 0.05  & commercial vs control     &  0.0001 \\
& control     & 1.00 & 0.1   & commercial vs synthesized &  0.415 \\
\addlinespace
July 2022
& synthesized & 10.6 & 0.001 & synthesized vs control    &  0.007 \\
& commercial  & 7.34 & 0.05  & commercial vs control     &  0.0001 \\
& control     & 1.00 & 0.1   & commercial vs synthesized &  0.415 \\
\addlinespace
August 2022
& synthesized & 10.6 & 0.001 & synthesized vs control    &  0.007 \\
& commercial  & 7.34 & 0.05  & commercial vs control     &  0.0001 \\
& control     & 1.00 & 0.1   & commercial vs synthesized &  0.415 \\
\bottomrule
\end{tabular}

\end{document}

在此处输入图片描述

答案2

我的主要建议是将第 3、4 和 6 列中的数字与各自的小数点对齐。这可以通过加载希尼奇S包并对所讨论的三列采用该包的列类型。

在此处输入图片描述

\documentclass[
  convert={
    density=300 -alpha deactivate,
    size=1080x1080,
    outext=.png
  },
  border=2pt
]{standalone}
\usepackage{booktabs,siunitx}
\begin{document}

\begin{tabular}{@{} l 
                    l 
                    S[table-format=2.2]
                    S[table-format=1.3]
                    l
                    S[table-format=1.4] @{}}
\toprule
Month
& \multicolumn{3}{c}{Kruskal-Wallis Group}  
& \multicolumn{2}{c@{}}{Conover-Iman post-hoc} \\
\cmidrule(lr){2-4}  \cmidrule(l){5-6}
& Treat & {x2} & {piiii}  & group & {p} \\  
\midrule
  & synthesized & 10.6 & 0.001 & synthesized vs control    & 0.007  \\
June 2022 
  & commercial  & 7.34 & 0.05  & commercial vs control     & 0.0001 \\
  & control     & 1.00 & 0.1   & commercial vs synthesized & 0.415  \\
\bottomrule
\end{tabular}

\end{document}

相关内容