复杂表格格式

复杂表格格式

我正在尝试格式化一个相当复杂的表格。布局完全符合我的要求,第一个表格(P1 和 P7)中的相应标题下方有底部表格。但列宽不相等(尽管第一列可以更宽以容纳文本),并且编译后表格的右侧会一直延伸到页面边缘。

我尝试使用 tabularx 和 tabulary,但它们似乎破坏了表格的整体格式。也许我需要用 c| | 定义列宽,但我也无法让它工作

\begin{table}[]\centering   
\label{table:OLS}
    \begin{tabular}{lcccccc}
        \cline{2-7}
        & \multicolumn{3}{c}{P1}                                                              & \multicolumn{3}{c}{P7}                                             \\ \hline
        \multicolumn{1}{c}{Model terms} & df                   & z-score              & p-value                               & df                   & z-score              & p-value              \\ \hline
        Shape $\sim$Size                & 1                    & 1.91                 & \multicolumn{1}{c|}{0.01*}            & 1                    & 0.85                 & 0.22                 \\
        Shape $\sim$Mode                & 2                    & 2.97                 & \multicolumn{1}{c|}{\textless{}0.01*} & 2                    & 2.79                 & \textless{}0.001*    \\
        Shape $\sim$Size * Mode         & 2                    & 1.13                 & \multicolumn{1}{c|}{0.86}             & 2                    & 1.64                 & 0.03*                \\ \hline
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{6}{c}{Pairwise (Group Slopes)}                                                                                                                         \\ \cline{2-7} 
        & E                    & M                    & G                                     & E                    & M                    & G                    \\ \cline{2-7} 
        \multicolumn{1}{r}{E}           & -                    & 0.62                 & \multicolumn{1}{c|}{0.35}             & -                    & 0.01*                & 0.59                 \\
        \multicolumn{1}{r}{M}           & 0.57                 & -                    & \multicolumn{1}{c|}{0.64}             & 1.82                 & -                    & 0.28                 \\
        \multicolumn{1}{r}{G}           & 0.29                 & 0.55                 & \multicolumn{1}{c|}{-}                & 0.36                 & 0.72                 & -                    \\ \cline{2-7} 
        & \multicolumn{3}{l|}{z-score = 1.10, Res.df = 12, p = 0.86}                          & \multicolumn{3}{l}{z-score = 1.53, Res.df = 13, p = 0.05*}         \\ \cline{2-7} 
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{6}{c}{Pairwise (Group Means)}                                                                                                                          \\ \cline{2-7} 
        & E                    & M                    & G                                     & E                    & M                    & G                    \\ \cline{2-7} 
        \multicolumn{1}{r}{E}           & -                    & \textless{}0.01*     & \multicolumn{1}{c|}{\textless{}0.01*} & -                    & \textless{}0.01*     & 0.01*                \\
        \multicolumn{1}{r}{M}           & 3.78                 & -                    & \multicolumn{1}{c|}{0.37}             & 3.90                 & -                    & 0.95                 \\
        \multicolumn{1}{r}{G}           & 3.31                 & 0.31                 & \multicolumn{1}{c|}{-}                & 2.67                 & 1.33                 & -                    \\ \cline{2-7} 
        & \multicolumn{3}{c|}{z-score = 3.08, Res.df = 14, p \textless 0.01*}                 & \multicolumn{3}{c}{z-score = 2.69, Res.df = 15, p \textless 0.01*} \\ \cline{2-7} 
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{6}{c}{ Pairwise (Allometry-free Group Means)}                                                                                                         \\ \cline{2-7} 
        & E                    & M                    & G                                     & E                    & M                    & G                    \\ \cline{2-7} 
        \multicolumn{1}{r}{E}           & -                    & \textless{}0.01*     & \multicolumn{1}{c|}{\textless{}0.01*} & -                    & \textless{}0.01*     & \textless{}0.01*     \\
        \multicolumn{1}{r}{M}           & 3.79                 & -                    & \multicolumn{1}{c|}{0.37}             & 3.79                 & -                    & 0.77                 \\
        \multicolumn{1}{r}{G}           & 3.32                 & 0.31                 & \multicolumn{1}{c|}{-}                & 3.43                 & 0.76                 & -                    \\ \cline{2-7} 
        & \multicolumn{3}{c|}{z-score = 3.10, df = 14, p \textless 0.01*}                     & \multicolumn{3}{c}{z-score = 2.82, Res.df = 16, p \textless 0.01*} \\ \cline{2-7} 
    \end{tabular}%
\end{table}

我希望表格除第一列之外的其他列宽相等,并且表格遵循全局边距设置。

非常感谢您的帮助。

答案1

我定义了一些新的列类型:CX{}

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{array}
\usepackage{calc}
\setlength\tabcolsep{5pt}
\newcolumntype{C}{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{1.25cm}}
\newcolumntype{X}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}
\begin{table}[t]\centering   
\label{table:OLS}
\begin{tabular}{lCCCCCC}
    \cline{2-7}
    & \multicolumn{3}{c}{P1} & \multicolumn{3}{c}{P7} \\ \hline
    \multicolumn{1}{c}{Model terms} & df & z-score & p-value & df & z-score & p-value \\ \hline
    Shape $\sim$Size & 1 & 1.91 & \multicolumn{1}{C|}{0.01*} & 1 & 0.85 & 0.22 \\
    Shape $\sim$Mode & 2 & 2.97 & \multicolumn{1}{C|}{\textless{}0.01*} & 2 & 2.79 & \textless{}0.001* \\
    Shape $\sim$Size * Mode & 2 & 1.13 & \multicolumn{1}{C|}{0.86} & 2 & 1.64 & 0.03* \\ \hline
    & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
    & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
    & \multicolumn{6}{c}{Pairwise (Group Slopes)}                                                                                                                         \\ \cline{2-7} 
        & E                    & M                    & G                                     & E                    & M                    & G                    \\ \cline{2-7} 
        \multicolumn{1}{r}{E}           & -                    & 0.62                 & \multicolumn{1}{C|}{0.35}             & -                    & 0.01*                & 0.59                 \\
        \multicolumn{1}{r}{M}           & 0.57                 & -                    & \multicolumn{1}{C|}{0.64}             & 1.82                 & -                    & 0.28                 \\
        \multicolumn{1}{r}{G}           & 0.29                 & 0.55                 & \multicolumn{1}{C|}{-}                & 0.36                 & 0.72                 & -                    \\ \cline{2-7} 
        & \multicolumn{3}{X{3.75cm+20pt}|}{z-score = 1.10,\newline Res.df = 12, p = 0.86}                          & \multicolumn{3}{X{3.75cm+20pt}}{z-score = 1.53,\newline Res.df = 13, p = 0.05*}         \\ \cline{2-7} 
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{6}{c}{Pairwise (Group Means)}                                                                                                                          \\ \cline{2-7} 
        & E                    & M                    & G                                     & E                    & M                    & G                    \\ \cline{2-7} 
        \multicolumn{1}{r}{E}           & -                    & \textless{}0.01*     & \multicolumn{1}{C|}{\textless{}0.01*} & -                    & \textless{}0.01*     & 0.01*                \\
        \multicolumn{1}{r}{M}           & 3.78                 & -                    & \multicolumn{1}{C|}{0.37}             & 3.90                 & -                    & 0.95                 \\
        \multicolumn{1}{r}{G}           & 3.31                 & 0.31                 & \multicolumn{1}{C|}{-}                & 2.67                 & 1.33                 & -                    \\ \cline{2-7} 
        & \multicolumn{3}{X{3.75cm+20pt}|}{z-score = 3.08,\newline Res.df = 14, p \textless 0.01*}                 & \multicolumn{3}{X{3.75cm+20pt}}{z-score = 2.69,\newline Res.df = 15, p \textless 0.01*} \\ \cline{2-7} 
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{6}{c}{ Pairwise (Allometry-free Group Means)}                                                                                                         \\ \cline{2-7} 
        & E                    & M                    & G                                     & E                    & M                    & G                    \\ \cline{2-7} 
        \multicolumn{1}{r}{E}           & -                    & \textless{}0.01*     & \multicolumn{1}{C|}{\textless{}0.01*} & -                    & \textless{}0.01*     & \textless{}0.01*     \\
        \multicolumn{1}{r}{M}           & 3.79                 & -                    & \multicolumn{1}{C|}{0.37}             & 3.79                 & -                    & 0.77                 \\
        \multicolumn{1}{r}{G}           & 3.32                 & 0.31                 & \multicolumn{1}{C|}{-}                & 3.43                 & 0.76                 & -                    \\ \cline{2-7} 
        & \multicolumn{3}{X{3.75cm+20pt}|}{z-score = 3.10, df = 14,\newline p \textless 0.01*}                     & \multicolumn{3}{X{3.75cm+20pt}}{z-score = 2.82,\newline Res.df = 16, p \textless 0.01*} \\ \cline{2-7} 
    \end{tabular}%
\end{table}
\end{document}

在此处输入图片描述


添加一些数学模式:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{array}
\usepackage{calc}
\setlength\tabcolsep{5pt}
\newcolumntype{C}{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{1.4cm}}
\newcolumntype{X}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}
\begin{table}[t]\centering   
\label{table:OLS}
\begin{tabular}{lCCCCCC}
    \cline{2-7}
    & \multicolumn{3}{c}{$P_1$} & \multicolumn{3}{c}{$P_7$} \\ \hline
    \multicolumn{1}{c}{Model terms} & $df$ & $z$-score & $p$-value & $df$ & $z$-score & $p$-value \\ \hline
    Shape $\sim$Size & 1 & 1.91 & \multicolumn{1}{C|}{0.01*} & 1 & 0.85 & 0.22 \\
    Shape $\sim$Mode & 2 & 2.97 & \multicolumn{1}{C|}{$<0.01$*} & 2 & 2.79 & $<0.001$* \\
    Shape $\sim$Size $\times$ Mode & 2 & 1.13 & \multicolumn{1}{C|}{0.86} & 2 & 1.64 & 0.03* \\ \hline
    & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
    & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
    & \multicolumn{6}{c}{Pairwise (Group Slopes)}                                                                                                                         \\ \cline{2-7} 
        & E                    & M                    & G                                     & E                    & M                    & G                    \\ \cline{2-7} 
        \multicolumn{1}{r}{E}           & -                    & 0.62                 & \multicolumn{1}{C|}{0.35}             & -                    & 0.01*                & 0.59                 \\
        \multicolumn{1}{r}{M}           & 0.57                 & -                    & \multicolumn{1}{C|}{0.64}             & 1.82                 & -                    & 0.28                 \\
        \multicolumn{1}{r}{G}           & 0.29                 & 0.55                 & \multicolumn{1}{C|}{-}                & 0.36                 & 0.72                 & -                    \\ \cline{2-7} 
        & \multicolumn{3}{X{4.2cm+20pt}|}{$z$-score $= 1.10$,\newline Res.$df = 12$, $p = 0.86$}                          & \multicolumn{3}{X{4.2cm+20pt}}{$z$-score = 1.53,\newline Res.$df = 13$, $p = 0.05$*}         \\ \cline{2-7} 
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{6}{c}{Pairwise (Group Means)}                                                                                                                          \\ \cline{2-7} 
        & E                    & M                    & G                                     & E                    & M                    & G                    \\ \cline{2-7} 
        \multicolumn{1}{r}{E}           & -                    & $<0.01$*     & \multicolumn{1}{C|}{$<0.01$*} & -                    & $<0.01$*     & 0.01*                \\
        \multicolumn{1}{r}{M}           & 3.78                 & -                    & \multicolumn{1}{C|}{0.37}             & 3.90                 & -                    & 0.95                 \\
        \multicolumn{1}{r}{G}           & 3.31                 & 0.31                 & \multicolumn{1}{C|}{-}                & 2.67                 & 1.33                 & -                    \\ \cline{2-7} 
        & \multicolumn{3}{X{4.2cm+20pt}|}{$z$-score = 3.08,\newline Res.$df = 14$, $p < 0.01$*}                 & \multicolumn{3}{X{4.2cm+20pt}}{$z$-score = 2.69,\newline Res.$df = 15$, $p< 0.01$*} \\ \cline{2-7} 
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{}                  & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
        & \multicolumn{6}{c}{Pairwise (Allometry-free Group Means)}                                                                                                         \\ \cline{2-7} 
        & E                    & M                    & G                                     & E                    & M                    & G                    \\ \cline{2-7} 
        \multicolumn{1}{r}{E}           & -                    & $<0.01$*     & \multicolumn{1}{C|}{$<0.01$*} & -                    & $<0.01$*     & $<0.01$*     \\
        \multicolumn{1}{r}{M}           & 3.79                 & -                    & \multicolumn{1}{C|}{0.37}             & 3.79                 & -                    & 0.77                 \\
        \multicolumn{1}{r}{G}           & 3.32                 & 0.31                 & \multicolumn{1}{C|}{-}                & 3.43                 & 0.76                 & -                    \\ \cline{2-7} 
        & \multicolumn{3}{X{4.2cm+20pt}|}{$z$-score $= 3.10$, $df = 14$,\newline $p < 0.01$*}                     & \multicolumn{3}{X{4.2cm+20pt}}{$z$-score $= 2.82$,\newline Res.$df = 16$, $p < 0.01$*} \\ \cline{2-7} 
    \end{tabular}%
\end{table}
\end{document}

在此处输入图片描述

答案2

一些返工。;-)

通过设置列标题来强制各列具有相同的宽度。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{siunitx,booktabs,array,calc,caption}


\begin{document}

\begin{table}[htp]
\centering

\newcommand{\head}[1]{{\makebox[\widthof{<0.001*}]{#1}}}
\newcommand{\zrp}[3]{%
  \multicolumn{3}{c}{\small
    \begin{tabular}{@{}c@{}}
      z-score = #1, Res.df = #2 \\ p #3
    \end{tabular}%
  }%
}

\caption{OLS}\label{table:OLS}

\sisetup{table-align-text-post=false,table-space-text-post=*}

\setlength{\tabcolsep}{0pt}

\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}} l
  S[table-format=1.2]
  S[table-format=1.2,table-comparator=true]
  S[table-format=1.2,table-comparator=true]
  S[table-format=1.2]
  S[table-format=1.2,table-comparator=true]
  S[table-format=1.3,table-comparator=true]
}
\toprule
& \multicolumn{3}{c}{P1} & \multicolumn{3}{c}{P7} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
\multicolumn{1}{@{}c}{Model terms} &
  \head{df} & \head{z-score} & \head{p-value} &
  \head{df} & \head{z-score} & \head{p-value} \\
\midrule
Shape $\sim$Size        & {1} & 1.91 &  0.01* & {1} & 0.85 &  0.22   \\
Shape $\sim$Mode        & {2} & 2.97 & <0.01* & {2} & 2.79 & <0.001* \\
Shape $\sim$Size * Mode & {2} & 1.13 &  0.86  & {2} & 1.64 &  0.03*  \\
\cmidrule[\heavyrulewidth]{2-7}
& \multicolumn{6}{c}{Pairwise (Group Slopes)} \\  
\cmidrule(lr){2-7}
& \head{E} & \head{M} & \head{G} & \head{E} & \head{M} & \head{G} \\  
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
\multicolumn{1}{r}{E} & {--} & 0.62 & 0.35 & {--} & 0.01* & 0.59 \\
\multicolumn{1}{r}{M} & 0.57 & {--} & 0.64 & 1.82 & {--}  & 0.28 \\
\multicolumn{1}{r}{G} & 0.29 & 0.55 & {--} & 0.36 & 0.72  & {--} \\  
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
& \zrp{1.10}{12}{= 0.86}  & \zrp{1.53}{13}{= 0.05*} \\
\cmidrule[\heavyrulewidth]{2-7}
& \multicolumn{6}{c}{Pairwise (Group Means)}  \\  
\cmidrule(lr){2-7}
& \head{E} & \head{M} & \head{G} & \head{E} & \head{M} & \head{G} \\  
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
\multicolumn{1}{r}{E} & {--} & <0.01* & <0.01* & {--} & <0.01* & 0.01* \\
\multicolumn{1}{r}{M} & 3.78 &  {--}  &  0.37  & 3.90 &  {--}  & 0.95  \\
\multicolumn{1}{r}{G} & 3.31 &  0.31  &  {--}  & 2.67 &  1.33  & {--}  \\  
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
& \zrp{3.08}{14}{<0.01*} & \zrp{2.69}{15}{<0.01*} \\  
\cmidrule[\heavyrulewidth]{2-7}
& \multicolumn{6}{c}{ Pairwise (Allometry-free Group Means)} \\  
\cmidrule(lr){2-7}
& \head{E} & \head{M} & \head{G} & \head{E} & \head{M} & \head{G} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
\multicolumn{1}{r}{E} & {--} & <0.01* & <0.01* & {--} & <0.01* & <0.01* \\
\multicolumn{1}{r}{M} & 3.79 &  {--}  &  0.37  & 3.79 &  {--}  &  0.77  \\
\multicolumn{1}{r}{G} & 3.32 &  0.31  &  {--}  & 3.43 &  0.76  &  {--}  \\  
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
& \zrp{3.10}{14}{<0.01*} & \zrp{2.82}{16}{<0.01*} \\  
\bottomrule
\end{tabular*}

\end{table}

\end{document}

在此处输入图片描述

相关内容