2

I've trying to format a fairly complex table. The layout is exactly as I want with bottom tables underneath the appropriate headings in the first table (P1 and P7). But the column widths aren't equal (though the first column can be wider to accommodate text) and after compilation the right hand side of the table goes right up to the edge of the page.

I've tried using tabularx and tabulary but these seem to break the overall format of the table. Perhaps I need to define the column widths with c| | but I couldn't get it to work either

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

I would like the table to have equal column widths, except for the first column and for the table to obey the global margin settings.

Any help greatly appreciated.

  • 2
    +1 for not using too many vertical lines. – user156344 Apr 3 at 9:53
  • Welcome to TeX.SE, please consider adding a compilable MWE. Thus we can help you better. – Raaja Apr 3 at 9:54
2

I define some new column types: C and X{}:

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

enter image description here


With some math mode added:

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

enter image description here

  • This is awesome! Thanks! Your answer is exactly what I asked for so I'll accept it, however, I'm actually going to use @egreg answer below just because it looks prettier! – Charles Baillie Apr 3 at 18:53
3

With some reworking. ;-)

The columns are forced to have the same width by setting their headers to.

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

enter image description here

  • This is stunning! Thanks (see comment above) – Charles Baillie Apr 3 at 18:54
  • @CharlesBaillie I revised the rules, fixing some of them. – egreg Apr 3 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.