关于创建表的问题

关于创建表的问题

我想创建一个这样的表格在此处输入图片描述

然而我能得到的是这样的在此处输入图片描述

在我创建的表中,存在两个问题,分别是

  1. 中间垂直线太短,无法到达顶部或底部水平线;
  2. 第二行文字的位置太高,以致于文字接触到上方的水平线。

你能帮我改正这两个问题吗?

以下是代码:

\documentclass{article}
\usepackage{multirow,makecell,booktabs}

\begin{document}
\begin{table}
    \centering\scriptsize
    \begin{tabular}{ccccc|cccc}
        \toprule
        \multirowcell{2}{$\beta$} & \multicolumn{4}{c|}{The first stage (DeepLabV3+)} & \multicolumn{4}{c}{The second stage (DeepLabV3+)} \\ \Xcline{2-9}{0.05em}
        & \multicolumn{2}{c}{IoU/ \%} & \multicolumn{2}{c|}{CPA/ \%} & \multicolumn{2}{c}{IoU/ \%} & \multicolumn{2}{c}{CPA/ \%} \\ \midrule
        & Background & Leaf & Background & Leaf & Background & Disease &Background & Disease \\ \Xcline{1-9}{0.05em}
    \end{tabular}
\end{table}
\end{document}

答案1

该解决方案使用包nicematrix

\Block{nrows-ncols}{<content>}命令充当多行/多列组合,使内容居中。

C

向第二个表添加可选cell-space-limits参数,以垂直扩展单元格并删除(现在多余的)垂直线。

埃

\documentclass{article}

\usepackage{nicematrix}
\usepackage{booktabs}

\usepackage{caption}
\captionsetup[table]{% caption set up
    format=hang, 
    font=small,
    aboveskip=1ex,
    singlelinecheck=false
}
    
\begin{document}

\begin{table}
    \centering\scriptsize
    \caption{The results of varying $\beta$  for Adaptive loss (based on DeepLabV3+ with movilenetv2 as the backbone)}  
    \begin{NiceTabular}{c cccc|cccc}    
        \toprule         
        \Block{3-1}{$\beta$}& \Block{1-4}{The first stage (DeepLabV3+)}
        &   &   &           & \Block{1-4}{The second stage (DeepLabV3+)} 
        &   &   &                           \\ 
        \cmidrule(lr{1ex}){2-5}\cmidrule(lr{1ex}){6-9}
            & \Block{1-2}{IoU/\%}       &           &  \Block{1-2}{CPA/\%} &
            & \Block{1-2}{IoU/\%}       &           &  \Block{1-2}{CPA/\%}    \\ 
        \cmidrule(lr{1ex}){2-3} \cmidrule(lr{1ex}){4-5}
        \cmidrule(lr{1ex}){6-7} \cmidrule(lr{1ex}){8-9}
            & Background    & Leaf      & Background& Leaf 
            & Background    & Disease   & Background& Disease      \\ 
        \bottomrule
    \end{NiceTabular}
\end{table}

\begin{table}
    \centering\scriptsize
    \caption{The results of varying $\beta$  for Adaptive loss (based on DeepLabV3+ with movilenetv2 as the backbone)}
    \NiceMatrixOptions{cell-space-limits=2pt}   % expand the cells
    \begin{NiceTabular}{c cccc cccc}
        \toprule         
        \Block{3-1}{$\beta$}& \Block{1-4}{The first stage (DeepLabV3+)}
        &   &   &           & \Block{1-4}{The second stage (DeepLabV3+)} 
        &   &   &                           \\ 
        \cmidrule(lr{1ex}){2-5}\cmidrule(lr{1ex}){6-9}
        &\Block{1-2}{IoU/\%}        &           &  \Block{1-2}{CPA/\%} &
        & \Block{1-2}{IoU/\%}       &           &  \Block{1-2}{CPA/\%}    \\ 
        \cmidrule(lr{1ex}){2-3} \cmidrule(lr{1ex}){4-5}
        \cmidrule(lr{1ex}){6-7} \cmidrule(lr{1ex}){8-9}
        & Background    & Leaf      & Background& Leaf 
        & Background    & Disease   & Background& Disease      \\ 
        \bottomrule
    \end{NiceTabular}
\end{table}

\end{document

答案2

  • 您的问题可以通过使用tabularray包来解决
  • 您的表格设计的两种情况:
    • 无垂直线:
\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
\begin{table}
    \centering\scriptsize
    \begin{tblr}{colsep = 3pt,
                 colspec= {@{} c cccc cccc @{}},
                 }
        \toprule
\SetCell[r=3]{m}    $\beta$
    & \SetCell[c=4]{c}  The first stage (DeepLabV3+)
        &   &   &   & \SetCell[c=4]{c}  The second stage (DeepLabV3+)   
                        &   &   &                           \\ 
    \cmidrule[lr]{2-5}
    \cmidrule[l ]{6-9}
    & \SetCell[c=2]{c}  IoU/ \%
        &   & \SetCell[c=2]{c}  CPA/ \%
                &   & \SetCell[c=2]{c}  IoU/ \%
                        &   &  \SetCell[c=2]{c}  CPA/ \%    \\ 
    \cmidrule[lr]{2-3}\cmidrule[lr]{4-5}
    \cmidrule[lr]{6-7}\cmidrule[l]{8-9}
    & Background    & Leaf      & Background & Leaf 
    & Background    & Disease   & Background & Disease      \\ 
    \bottomrule
    \end{tblr}
\end{table}
\end{document}

在此处输入图片描述

  • 带有垂直线:
\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
\begin{table}
    \centering\scriptsize
    \begin{tblr}{colsep = 3pt,
                 colspec= {@{} c cccc|cccc @{}},
                 }
        \toprule
\SetCell[r=3]{m}    $\beta$
    & \SetCell[c=4]{c}  The first stage (DeepLabV3+)
        &   &   &   & \SetCell[c=4]{c}  The second stage (DeepLabV3+)   
                        &   &   &                           \\ 
    \midrule[gray]
    & \SetCell[c=2]{c}  IoU/ \%
        &   & \SetCell[c=2]{c}  CPA/ \%
                &   & \SetCell[c=2]{c}  IoU/ \%
                        &   &  \SetCell[c=2]{c}  CPA/ \%    \\ 
    \midrule
    & Background    & Leaf      & Background & Leaf 
    & Background    & Disease   & Background & Disease      \\ 
    \bottomrule
    \end{tblr}
\end{table}
\end{document}

在此处输入图片描述

相关内容