如何制作一个具有多个子标题列且跨越整个表格宽度的表格?

如何制作一个具有多个子标题列且跨越整个表格宽度的表格?

我尝试在表格环境中使用多个 tabularx 环境,但这会导致整个文本向左对齐且与列标题不匹配的问题,在下面的例子中是V1, V2, V3

我收到以下代码的错误(我尝试删除 \hline,但没有帮助):

Misplaced \noalign.: You have used a \hline command in the wrong place, probably outside a table. If the \hline command is written inside a table, try including \\ before it.
\begin{table}[]

    \begin{tabular*}{\textwidth}{SSSS} \hline
        {$ $} & {$metric$} & {$metric$} & {$metric$} \\ \hline
        \textbf{class 1}  & 0 & 0 & 0 \\
        \midrule
        \textbf{HEADING}
        \midrule
        \textbf{model (1)}  & 0  &  0  &  0\\ 
        \textbf{model (1)}  & 0  &  0   & 0  \\
        \textbf{model (1)}  & 0   & 0  &  0  \\
        \textbf{model (1)}  & 0  &  0 &  0 \\
        \midrule
        \textbf{model (1)}  & 0  &  0  &  0\\ 
        \textbf{model (1)}  & 0  &  0   & 0  \\
        \textbf{model (1)}  & 0   & 0  &  0  \\
        \textbf{model (1)}  & 0  &  0 &  0 \\
        \midrule
        \textbf{HEADING}
        \midrule
        \textbf{model (1)}  & 0  &  0  &  0 \\ 
        \textbf{model (1)}  & 0  &  0   & 0  \\
        \textbf{model (1)}  & 0   & 0  &  0  \\
        \textbf{model (1)}  & 0  &  0 &  0 \\
        \midrule
        \textbf{model (1)}  & 0  &  0  &  0\\ 
        \textbf{model (1)}  & 0  &  0   & 0  \\
        \textbf{model (1)}  & 0   & 0  &  0  \\
        \textbf{model (1)}  & 0  &  0 &  0 \\
        \bottomrule
    \end{tabular*}


    \caption{caption here}
    \label{table:results}
\end{table}

在此处输入图片描述

答案1

我相信您有意这样做(请注意,您tabularx根本没有使用。就我个人而言,我会tabular在这里使用正常方式:将列拉伸到满,只会使表格更难阅读,但这会显示它的全宽。

在此处输入图片描述


\documentclass{article}

\usepackage{siunitx}
\usepackage{array,booktabs}


\begin{document}
\begin{table}% don't use an empty argument []

    \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lSSS@{}} \\\toprule
% don't use math mode to get italics!!!!
%        {$ $} & {$metric$} & {$metric$} & {$metric$} \\ \hline
    & {\textit{metric}}&{\textit{metric}}&{\textit{metric}}\\\midrule
        \textbf{class 1}  & 0 & 0 & 0 \\
        \midrule
        &\multicolumn{3}{c}{\textbf{HEADING}}\\
        \midrule
        \textbf{model (1)}  & 0  &  0  &  0\\ 
        \textbf{model (1)}  & 0  &  0   & 0  \\
        \textbf{model (1)}  & 0   & 0  &  0  \\
        \textbf{model (1)}  & 0  &  0 &  0 \\
        \midrule
        \textbf{model (1)}  & 0  &  0  &  0\\ 
        \textbf{model (1)}  & 0  &  0   & 0  \\
        \textbf{model (1)}  & 0   & 0  &  0  \\
        \textbf{model (1)}  & 0  &  0 &  0 \\
        \midrule
        &\multicolumn{3}{c}{\textbf{HEADING}}\\
        \midrule
        \textbf{model (1)}  & 0  &  0  &  0 \\ 
        \textbf{model (1)}  & 0  &  0   & 0  \\
        \textbf{model (1)}  & 0   & 0  &  0  \\
        \textbf{model (1)}  & 0  &  0 &  0 \\
        \midrule
        \textbf{model (1)}  & 0  &  0  &  0\\ 
        \textbf{model (1)}  & 0  &  0   & 0  \\
        \textbf{model (1)}  & 0   & 0  &  0  \\
        \textbf{model (1)}  & 0  &  0 &  0 \\
        \bottomrule
    \end{tabular*}


    \caption{caption here}
    \label{table:results}
\end{table}

\end{document}

如果你使用

   \centering
   \begin{tabular}{lSSS} \\\toprule

然后它看起来像

在此处输入图片描述

答案2

此替代方案使用包nicematrix。我遵循了绘图的总体设计,并绘制了一条垂直线。

技术出版物中通常不鼓励使用垂直线,但在许多情况下它们当然很有用。有些软件包很难连接垂直线和水平线(见第二张图)。但 并非如此nicematrix

我添加了一个新的列类型,以允许将最后 3 列扩展为文本宽度的百分比。

最后,几个元素被强调,但没有明显的后果。还请注意“度量“。

笔记 要在 S 列下划线,请使用 包\uline中的下划线ulem。(加载为\usepackage[normalem]{ulem})。请参阅https://tex.stackexchange.com/a/159634/161015

A

连接线路时出现问题

X

\documentclass{article}

\usepackage{siunitx}
\usepackage{array,booktabs}

\usepackage{nicematrix}

\begin{document}
    
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1\textwidth}} % size of the column as a percent of textwidth    
    
\NiceMatrixOptions{cell-space-top-limit = 5pt,cell-space-bottom-limit = 5pt} %expand the cells vertically and horizontally  
    
\begin{table}
\centering
\begin{NiceTabular}{>\bfseries{c} | P{0.2} P{0.2}  P{0.2}}  % first column in bf
              & \textit{metric}&\textit{metric}&\textit{\underline{metric}}\\
    \midrule
    \textbf{class 1}  & 0 & 0 & 0 \\
    \midrule
              &\Block{1-3}{\textbf{\underline{HEADING}}}\\
    \midrule
    model (1) & 0  & 0  &  0 \\ 
    \underline{model (1)} & 0  &  0   & 0  \\
    model (1) & 0  & 0  &  0 \\
    model (1) & 0  & 0  &  0 \\
    \midrule
    model (1) & 0  & 0  & 0  \\ 
    model (1) & 0  & 0  & 0  \\
    model (1) & 0  & 0  & 0  \\
    model (1) & \underline{0}  &  0 &  0 \\
    \midrule
              &\Block{1-3}{\textbf{HEADING}}\\
    \midrule
    model (1) & 0  & 0  &  0  \\ 
    model (1) & 0  & 0  &  0  \\
    model (1) & 0  & 0  &  0  \\
    model (1) & 0  & 0  &  0  \\
    \midrule
    model (1) & 0  & 0  & 0  \\ 
    model (1) & 0  & 0  & 0  \\
    model (1) & 0  &  \underline{0}  &  0  \\
    model (1) & 0  & 0  &  0 \\
    \bottomrule
    \end{NiceTabular}

    \caption{caption here}
    \label{table:results}
\end{table}

\end{document}

相关内容