多列表格 - tabularray

多列表格 - tabularray

我想设计一个像下面这样的表格,但我对 Latex 不够熟练,而且会花太长时间。有人可以帮忙写一个这样的“模型”表格吗?

在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage{tabularx,ragged2e,booktabs}


\begin{document}

\begin{table}[h]
    \begin{tabularx}{\textwidth}{X *6{r}}
        \toprule \addlinespace[6pt]
        Statistics &
        \multicolumn{2}{c}{April} &
        \multicolumn{2}{c}{June} \\
        \cmidrule(lr){2-3}
        \cmidrule(lr){4-5}
         & Cumulative Operating Time/h & Average Operating Time/h  & Cumulative Operating Time/h & Average Operating Time/h \\
        \midrule
        Trawler            & 142.87 & 137.72 & 142.87 & 142.87   \\
        Purse-seine             & 102.94 & 190.15 & 142.87 & 142.87 \\
        \addlinespace[3pt]
        \toprule \addlinespace[6pt]
        Statistics &
        \multicolumn{2}{c}{September} &
        \multicolumn{2}{c}{October}\\
        \cmidrule(lr){2-3}
        \cmidrule(lr){4-5}
             & Cumulative Operating Time/h & Average Operating Time/h  & Cumulative Operating Time/h & Average Operating Time/h \\
        \midrule
        Trawler   & -65.76 & -60.18 & 142.87 & 142.87  \\
        Purse seine             &  42.37 &  36.11 & 142.87 & 142.87 \\
        \bottomrule
    \end{tabularx}
    \caption{Statistics of speed, heading and depth for OTB \& PS fishing vessel types}
    \label{table_statistics}
\end{table}

\end{document}

得出: 在此处输入图片描述

答案1

我认为不需要tabularray,甚至tabularx,环境。一旦对臃肿的标题单元格材料应用一些合理的缩写,表格的所有 9 列都适合文本块的宽度。这表明采用 9 列tabular*环境。

在此处输入图片描述

\documentclass{article}
\usepackage[flushleft]{threeparttable}
\usepackage{booktabs,siunitx}
\newcommand\mc[1]{\multicolumn{2}{c}{#1}} % handy shortcut macro

\begin{document}

\begin{table}[ht!]
\setlength\tabcolsep{0pt}

\begin{threeparttable}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{8}{S[table-format=3.2]} }
    \toprule 
    Statistics 
    & \mc{April} & \mc{June} & \mc{September} & \mc{October}\\
    \cmidrule{2-3} \cmidrule{4-5} \cmidrule{6-7} \cmidrule{8-9}
    & {COT\tnote{a}} & {AOT\tnote{b}} & {COT} & {AOT} & {COT} & {AOT} & {COT} & {AOT} \\
    \midrule
    Trawler      & 142.87 & 137.72 & 142.87 & 142.87 & -65.76 & -60.18 & 142.87 & 142.87 \\
    Purse-seine  & 102.94 & 190.15 & 142.87 & 142.87 &  42.37 &  36.11 & 142.87 & 142.87 \\
    \bottomrule
\end{tabular*}
    
\smallskip\footnotesize
\begin{tablenotes}
  \item[a]COT: Cumulative Operating Time, in hours
  \item[b]AOT: Average Operating Time, in hours
\end{tablenotes}
    
\caption{Statistics of speed, heading and depth for OTB \& PS fishing vessel types}
\label{table_statistics}

\end{threeparttable}
\end{table}

\end{document}

答案2

与 @Mico 相反,我发现使用tabularray包有一些优势 :-)。它定义了talltblr表,相当于 ,threeparttable不同之处在于表的注释写在表序言中。使用它们,类似于 @Mico 在他的回答中所做的 (+1),您可以在列标题中使用缩写 COT 和 AOT,这样可以使列变窄。

\documentclass{article}
%\usepackage[xcdraw]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs,siunitx}
\NewTableCommand\SCC{\SetCell[c=2]{c}}

\begin{document}
    \begin{table}[ht!]
\begin{talltblr}[
caption = {Statistics of speed, heading and depth for OTB \& PS fishing vessel types},
  label = {table_statistics},
note{a} = {COT: Cumulative Operating Time, in hours},
note{b} = {AOT: Average Operating Time, in hours},
                  ]{colsep = 3pt,
                   colspec = {@{} l *{8}{X[c, si={table-format=3.2}]} @{}},
                  row{1,2} = {guard, c}
                    }
    \toprule
\SetCell[r=2]{l}    Statistic 
1   &\SCC   April 
        &   &\SCC   June
                &   &\SCC   September
                        &   &\SCC   October
                                & 8 \\
    \cmidrule[lr]{2-3} \cmidrule[lr]{4-5}
    \cmidrule[lr]{6-7} \cmidrule[lr]{8-9}
    & COT\TblrNote{a}   & AOT\TblrNote{b} & COT     & AOT   & COT   & AOT   & COT   & AOT   \\
    \midrule
Trawler      & 142.87   & 137.72 & 142.87 & 142.87  & -65.76 & -60.18 & 142.87 & 142.87 \\
Purse-seine  & 102.94   & 190.15 & 142.87 & 142.87  &  42.37 &  36.11 & 142.87 & 142.87 \\
    \bottomrule
\end{talltblr}
    \end{table}
\end{document}

在此处输入图片描述

答案3

在此处输入图片描述使用\thead来自 的命令非常容易makecell,它使您可以在标准列中进行换行:

\documentclass{article}
\usepackage{multirow}
\usepackage{tabularx,ragged2e,booktabs}
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize\bfseries}

\begin{document}

\begin{table}[h]
    \begin{tabularx}{\textwidth}{X *6{r}}
        \toprule \addlinespace[6pt]
        \bfseries Statistics &
        \multicolumn{2}{c}{\bfseries April} &
        \multicolumn{2}{c}{\bfseries June} \\
        \cmidrule(lr){2-3}
        \cmidrule(lr){4-5}
         & \thead{Cumulative\\ Operating\\ Time/h} & \thead{Average\\ Operating\\ Time/h} & \thead{Cumulative\\ Operating\\ Time/h} & \thead{Average\\ Operating\\ Time/h} \\
        \midrule
        Trawler & 142.87 & 137.72 & 142.87 & 142.87 \\
        Purse-seine & 102.94 & 190.15 & 142.87 & 142.87 \\
        \addlinespace[3pt]
        \toprule \addlinespace[6pt]
        \bfseries Statistics &
        \multicolumn{2}{c}{\bfseries September} &
        \multicolumn{2}{c}{\bfseries October}\\
        \cmidrule(lr){2-3}
        \cmidrule(lr){4-5}
             & \thead{Cumulative\\ Operating\\ Time/h} & \thead{Average\\ Operating\\ Time/h} & \thead{Cumulative\\ Operating\\ Time /h} & \thead{Average\\ Operating\\ Time/h} \\
        \midrule
        Trawler & -65.76 & -60.18 & 142.87 & 142.87 \\
        Purse seine & 42.37 & 36.11 & 142.87 & 142.87 \\
        \bottomrule
    \end{tabularx}
    \caption{Statistics of speed, heading and depth for OTB \& PS fishing vessel types}
    \label{table_statistics}
\end{table}

\end{document} 

答案4

您的表格的问题在于内容;没有足够的空间容纳这么长的标题。您可以清楚地看到这一点!此外,定义中的列数必须与数据中的列数相匹配。

有两种解决方案。首先,您可以垂直拆分长表达式,通常使用“内部tabulars”(参见代码)。另一个选项是使用\makecell{}来自包的makecell。即使现在表格适合页面,但在我看来,除非您有更多数据要添加,否则它并不平衡。

另一个解决方案,我认为稍微好一点,也是你在其他问题中使用的解决方案,是使用较短的文本,甚至使用首字母缩略词并在表格底部添加注释

在此处输入图片描述

代码

\documentclass{article}
\usepackage{tabularx,booktabs}

\newcommand\tabcell[1]{\begin{tabular}{c}#1\end{tabular}}


\begin{document}
\begin{table}[htb]
    \centering

    \begingroup
        \small
        \begin{tabularx}{\textwidth}{X *4{c}}
            \toprule \addlinespace[6pt]
            Statistics & \multicolumn{2}{c}{April} & \multicolumn{2}{c}{June} \\
            \cmidrule(lr){2-3} \cmidrule(lr){4-5}
            &
            \tabcell{Cumulative\\Operating\\Time/h} &
            \tabcell{Average\\Operating\\Time/h} &
            \tabcell{Cumulative\\Operating\\Time/h} &
            \tabcell{Average\\Operating\\Time/h} \\
            \midrule
            Trawler     & 142.87 & 137.72 & 142.87 & 142.87 \\
            Purse-seine & 102.94 & 190.15 & 142.87 & 142.87\\
            \addlinespace[3pt]
            \toprule \addlinespace[6pt]
            Statistics & \multicolumn{2}{c}{September} & \multicolumn{2}{c}{October} \\
            \cmidrule(lr){2-3} \cmidrule(lr){4-5}
            &
            \tabcell{Cumulative\\Operating\\Time/h} &
            \tabcell{Average\\Operating\\Time/h} &
            \tabcell{Cumulative\\Operating\\Time/h} &
            \tabcell{Average\\Operating\\Time/h} \\
            \midrule
            Trawler     & -65.76 & -60.18 & 142.87 & 142.87 \\
            Purse seine &  42.37 &  36.11 & 142.87 & 142.87 \\
            \bottomrule
        \end{tabularx}
        \caption{Statistics of speed, heading and depth for OTB \& PS fishing vessel types}
        \label{table_statistics}
    \endgroup
    
    \bigskip X \dotfill{} X\bigskip
    
    \begin{tabularx}{0.7\linewidth}{X *4{c}}
        \toprule \addlinespace[6pt]
        Statistics & \multicolumn{2}{c}{April} & \multicolumn{2}{c}{June} \\
        \cmidrule(lr){2-3} \cmidrule(lr){4-5}
        & COT & AOT & COT & AOT \\
        \midrule
        Trawler     & 142.87 & 137.72 & 142.87 & 142.87 \\
        Purse-seine & 102.94 & 190.15 & 142.87 & 142.87\\
        \addlinespace[3pt]
        \toprule \addlinespace[6pt]
        Statistics & \multicolumn{2}{c}{September} & \multicolumn{2}{c}{October} \\
        \cmidrule(lr){2-3} \cmidrule(lr){4-5}
        & COT & AOT & COT & AOT \\
        \midrule
        Trawler     & -65.76 & -60.18 & 142.87 & 142.87 \\
        Purse seine &  42.37 &  36.11 & 142.87 & 142.87 \\
        \bottomrule
        \addlinespace[3pt]
        \multicolumn{5}{@{}l}{COT: Cumulative Operating Time/h} \\
        \multicolumn{5}{@{}l}{AOT: Average Operating Time/h} \\
    \end{tabularx}
    \caption{Statistics of speed, heading and depth for OTB \& PS fishing vessel types}
    \label{table_statistics2}
\end{table}

\end{document}

相关内容