表格多列和多行位移(和垂直线分隔)

表格多列和多行位移(和垂直线分隔)

我在格式化多列多行的表格 latex 表格时遇到了很大的问题。特别是垂直线分隔和最后一部分居中。我对表格还很陌生,所以请随时给我任何可以帮助我的建议。

这是我的 MWE:

\documentclass[final,5p,twocolumn]{elsarticle}

\usepackage{float, enumitem, amsmath} 
\usepackage{booktabs, makecell, multicol, multirow, threeparttable, tabularx, subcaption, booktabs, colortbl, color}
\usepackage{adjustbox}
\usepackage{todonotes}
\usepackage{caption}
\captionsetup{belowskip=-5pt}
\captionsetup[table]{justification=raggedright,singlelinecheck=off}
\newcommand*\rot{\rotatebox{90}}

\begin{document}

\clearpage
\onecolumn
\begin{table} \centering
\caption{Simple caption.}
\label{tab:setTag}
\begin{adjustbox}{width=0.9\textwidth,center=\textwidth}
\begin{threeparttable}
\begin{tabular}{@{}l|lcc@{}}
\toprule
& \multicolumn{1}{l}{\bfseries Results} & \bfseries Method 1 & \bfseries Method 2 \\
\midrule
& &\multicolumn{2}{c}{First section}\\ \cmidrule{3-4} 
\multirow{14}{*}{\rotatebox[origin=c]{90}{~Description 1}}& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4}

& &\multicolumn{2}{c}{Second section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4}

& &\multicolumn{2}{c}{Third section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 

\rowcolor{gray!10}
& &\multicolumn{2}{c}{First section}\\ \cmidrule{3-4} 
\rowcolor{gray!10}
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 
\rowcolor{gray!10}
& &\multicolumn{2}{c}{Second section}\\ \cmidrule{3-4} 
\rowcolor{gray!10}
& Very long name & Something pretty long too & Something else even longer than before \\
\rowcolor{gray!10}
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 
\rowcolor{gray!10}
& &\multicolumn{2}{c}{Third section}\\ \cmidrule{3-4} 
\rowcolor{gray!10}
& Very long name & Something pretty long too & Something else even longer than before \\
\rowcolor{gray!10}
\multirow{-8}{*}{\rotatebox[origin=c]{90}{~Description 2}}& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4}

& &\multicolumn{2}{c}{First section}\\ \cmidrule{3-4} 
\multirow{1}{*}{\rotatebox[origin=c]{90}{~Description 3}}& Very long name & Something pretty long too & Something else even longer than before \\
\cmidrule{3-4}
\end{tabular}
\end{threeparttable}
\end{adjustbox}
\end{table}

\end{document}

在此处输入图片描述

答案1

像这样?

在此处输入图片描述

  • 包中定义的规则在booktabs规则的上方和下方有小的间隙,因此不适用于带有垂直线的表格。
  • 目前尚不清楚,threeparttable当您不使用任何功能时,为什么会使用该表。
  • 我不会使用scalebox,最好使用表格环境,因为tabularx您可以规定表格宽度。使用其X列类型时,此类列中单元格中的文本会自动分成更多行(当然,您p{<width>}也可以使用列来实现这一点)。
  • 最好使用浮动环境,而不是从\twocolumn文档切换到\onecolumn(然后再切换回来)table*。使用它,表格将出现在插入文本的下一页的顶部。
\documentclass[final,5p,twocolumn]{elsarticle}

\usepackage{float, enumitem, amsmath}
\usepackage{rotating}     % new
\usepackage{booktabs, makecell, multirow, tabularx, threeparttable}
\renewcommand\theadfont{\small\bfseries}
\renewcommand\theadgape{} % new
\usepackage[table]{xcolor}% changed, instead of color, colortbl
%\usepackage{adjustbox} % not used in this MWE
%\usepackage{todonotes} % not used in this MWE

\usepackage{caption}
\captionsetup{belowskip=-5pt}
\captionsetup[table]{justification=raggedright,singlelinecheck=off}
%\usepackage{subcaption} % not used in this MWE

\begin{document}
    \begin{table*} 
    \centering
    \settowidth\rotheadsize{\theadfont Description 3 }
    \renewcommand\arraystretch{1.1}
\caption{Simple caption.}
\label{tab:setTag}
%\begin{adjustbox}{width=0.9\textwidth,center=\textwidth}
%\begin{threeparttable}
\begin{tabularx}{\linewidth}{l | l *{2}{>{\centering\arraybackslash}X}}
    \Xhline{1pt}
    & \thead[l]{Results} 
        & \thead{Method 1} & \thead{Method 2}           \\
    \Xhline{0.7pt}
    &   & \multicolumn{2}{c}{First section}             \\ 
    \Xcline{3-4}{0.6pt}
\multirow{16}{*}{\rothead{Description 1}}
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.6pt}
    &   & \multicolumn{2}{c}{Second section}             \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.6pt}
    &   & \multicolumn{2}{c}{Third section}             \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.7pt}
\rowcolor{gray!10}
    &   & \multicolumn{2}{c}{First section}             \\
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.7pt}
\rowcolor{gray!10}
    &   &\multicolumn{2}{c}{Second section}             \\ 
    \Xcline{3-4}{0.7pt}
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.7pt}
\rowcolor{gray!10}
    &   &\multicolumn{2}{c}{Third section}          \\ 
    \Xcline{3-4}{0.6pt}
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\
    \rowcolor{gray!10}
\multirow{-8}{*}{\rothead{Description 2}}
    & Very long name & Something pretty long too & Something else even longer than before \\
%    \Xcline{3-4}{0.7pt}
%    &   &\multicolumn{2}{c}{First section}      \\ 
%    \Xcline{3-4}{0.7pt}
%\multirow{1}{*}{\rotatebox[origin=c]{90}{~Description 3}}
%    & Very long name & Something pretty long too & Something else even longer than before \\
%    \Xcline{3-4}{0.7pt}
    \Xhline{1pt}
\end{tabularx}
%\end{threeparttable}
%\end{adjustbox}
    \end{table*}
\end{document}

附录: 一个可能的解决方案或表格的第三部分,其中第一列中的“描述 3”被缩写“D. 3”所替代。表格前两部分中的“描述 1 (D. 1)”和“描述 2 (D. 2)”给出了它们的含义。

\documentclass[final,5p,twocolumn]{elsarticle}

\usepackage{float, enumitem, amsmath}
\usepackage{rotating}
\usepackage{booktabs, makecell, multirow, tabularx, threeparttable}
\renewcommand\theadfont{\small\bfseries}
\renewcommand\theadgape{}
\usepackage[table]{xcolor}

\usepackage{caption}
\captionsetup{belowskip=-5pt}
\captionsetup[table]{justification=raggedright,singlelinecheck=off}
\usepackage{subcaption}

\begin{document}
    \begin{table*} 
    \centering
    \settowidth\rotheadsize{\theadfont Description 3 (D. 3)}
    \renewcommand\arraystretch{1.1}
\caption{Simple caption.}
\label{tab:setTag}
\begin{tabularx}{\linewidth}{l | l *{2}{>{\centering\arraybackslash}X}}
    \Xhline{1pt}
    & \thead[l]{Results} 
        & \thead{Method 1} & \thead{Method 2}           \\
    \Xhline{0.7pt}
    &   & \multicolumn{2}{c}{First section}             \\ 
    \Xcline{3-4}{0.6pt}
\multirow{16}{*}{\rothead{Description 1 (D.\,1)}}
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.6pt}
    &   & \multicolumn{2}{c}{Second section}             \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.6pt}
    &   & \multicolumn{2}{c}{Third section}             \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    & Very long name & Something pretty long too & Something else even longer than before \\
    \Xcline{3-4}{0.7pt}
\rowcolor{gray!10}
    &   & \multicolumn{2}{c}{First section}             \\
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.8pt}
\rowcolor{gray!10}
    &   &\multicolumn{2}{c}{Second section}             \\ 
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\ \Xcline{3-4}{0.8pt}
\rowcolor{gray!10}
    &   &\multicolumn{2}{c}{Third section}          \\ 
\rowcolor{gray!10}
    & Very long name & Something pretty long too & Something else even longer than before \\
\rowcolor{gray!10}
\multirow{-8}{*}{\rothead{Description 2 (D.\,2)}}
    & Very long name & Something pretty long too & Something else even longer than before \\
    \Xcline{3-4}{0.6pt}
\settowidth\rotheadsize{\theadfont(D.\,3)} % <--- changed (reduced) \rothead size
\multirow{2}{*}{\rothead{(D.\,3)}}
    &   &\multicolumn{2}{c}{First section}      \\ 
    & Very long name & Something pretty long too & Something else even longer than before \\[1.5ex]
    \Xhline{1pt}
\end{tabularx}
    \end{table*}
\end{document}

在此处输入图片描述

答案2

{NiceTabular}(≥ 4.0)的环境nicematrix与 兼容booktabs

您可以使用 来完成所有的块(\multicolumnmultirow\Block

只需一条指令即可为行着色[code-before = \rowcolor{gray!10}{18-25}]

最后您可以使用 Tikz 和nicematrix数组下创建的节点制定您想要的任何规则。

\documentclass{article}

\usepackage{booktabs}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{NiceTabular}{wc{5mm}LCC}[code-before = \rowcolor{gray!10}{18-25}]
\toprule
& \bfseries Results & \bfseries Method 1 & \bfseries Method 2 \\
\midrule
& &\Block{1-2}{First section}\\ \cmidrule{3-4} 
\Block{14-1}{\rotate Description 1}
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4}
%
& &\Block{1-2}{Second section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4}
%
& &\Block{1-2}{Third section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 
%
\Block{8-1}{\rotate Description 2}& &\Block{1-2}{First section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 
%
& &\Block{1-2}{Second section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \cmidrule{3-4} 
%
& &\Block{1-2}{Third section}\\ \cmidrule{3-4} 
& Very long name & Something pretty long too & Something else even longer than before \\
& Very long name & Something pretty long too & Something else even longer than before \\ \bottomrule
\CodeAfter
\tikz \draw (row-1-|col-2) -- (row-26-|col-2) ;
\end{NiceTabular}

\end{document}

上述代码的结果

相关内容