在 tabularx 表中使用 booktabs 绘制连续垂直线?

在 tabularx 表中使用 booktabs 绘制连续垂直线?

如何使用表格中的书签绘制连续的垂直线?最大化表格宽度的最简单方法是什么?有很多方法,非常令人困惑。

\documentclass{article}
\usepackage{makecell} % for bold in table using \small
\renewcommand\theadfont{\small} % for bold in table using \small
\usepackage{tabularx, ragged2e} 
\usepackage{booktabs}

\begin{document}

\begin{table}[!ht]
\centering
    \begin{tabularx}{\textwidth}{l>{\raggedright\arraybackslash}ccc|ccc}
    \toprule
    & \multicolumn{3}{c|}{\textbf{Paired Differences1}} & \multicolumn{3}{c}{\textbf{Paired Differences2}}\\
     \cmidrule{2-7}
    & \small {\textbf{Statistic}}
     & \thead{\small {\textbf{df}}}
     & \thead{\small {\textbf{Sig.}}}
         & \small {\textbf{Statistic}}
     & \thead{\small {\textbf{df}}}
     & \thead{\small {\textbf{Sig.}}}    \\ 
\midrule
    Difference & 44.20 & 14.36 & 4.54 & .957 & 10 & .746\\
        \bottomrule
    \end{tabularx}
\caption{Testing Testing Testing%
    \label{tab:test1234}% 
    }  
\end{table}

\end{document}

在此处输入图片描述

答案1

tabular*这是我的建议。我结合使用了和@{\extracolsep{\fill}},使表格的宽度与文本宽度一样宽,并均匀分布列之间的多余空白。我还删除了垂直线,并用\cmidrule两条相邻的线替换了一条,中间有一个小空白。为了清理代码,我还删除了重复出现的\small和 ,\textbf而是将其添加\bfseries\thedfont

在此处输入图片描述

\documentclass{article}
\usepackage{makecell} % for bold in table using \small
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\usepackage{tabularx, ragged2e} 
\usepackage{booktabs}

\begin{document}

\begin{table}[!ht]
    \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lcccccc}
    \toprule
    & \multicolumn{3}{c}{\textbf{Paired Differences1}} & \multicolumn{3}{c}{\textbf{Paired Differences2}}\\
     \cmidrule(r){2-4} \cmidrule(l){5-7}
    & \thead{Statistic}
     & \thead{df}
     & \thead{Sig.}
         & \thead{Statistic}
     & \thead{df}
     & \thead{Sig.}    \\ 
\midrule
    Difference & 44.20 & 14.36 & 4.54 & .957 & 10 & .746\\
        \bottomrule
    \end{tabular*}
\caption{Testing Testing Testing%
    \label{tab:test1234}% 
    }  
\end{table}

\end{document}

答案2

带有SX列类型的组合,没有垂直线,带有圆形数字......:

\documentclass{article}
\usepackage{booktabs, tabularx}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcx{O{1}m}
    {\multicolumn{#1}{>{\Centering\small\bfseries\hsize=#1\hsize}X}{#2}}
\usepackage{ragged2e}
\usepackage{siunitx}

\begin{document}
    \begin{table}[ht]
\centering
\setlength\tabcolsep{0pt}
\sisetup{round-integer-to-decimal,
         round-mode=places,
         table-format=2.2}
    \begin{tabularx}{\linewidth}{l *{6}{S} }
    \toprule
    & \mcx[3]{Paired Differences 1} 
    & \mcx[3]{Paired Differences 2}             \\
    \cmidrule(r){2-4}\cmidrule(l){5-7}
    & \mcx{Statistic}   & \mcx{df}  & \mcx{Sig.}
    & \mcx{Statistic}   & \mcx{df}  & \mcx{Sig.}    \\
    \midrule 
Difference 
    & 44.20 & 14.36 & 4.54  & 0.957 & 10     & 0.746 \\
        \bottomrule
    \end{tabularx}
\caption{Testing Testing Testing}
\label{tab:test1234}
    \end{table}
\end{document}

在此处输入图片描述

答案3

我宁愿用一个补充的空列来代替垂直线,以便两组列之间有明显的分隔。从美观角度来看,另一种可能性是删除水平线的垂直填充,并将其替换为\makegapedcells来自makecell代替,这会在水平线的顶部和底部添加一个垂直空间全部单元格。作为演示,我将垂直线替换为粗的浅灰色垂直线,我发现这比默认的细的黑色垂直线更赏心悦目。

\documentclass{article}
\usepackage{makecell} % for bold in table using \small
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\usepackage{tabularx, ragged2e}
\usepackage{booktabs}
\usepackage[table, svgnames]{xcolor}

\begin{document}

\begin{table}[!ht]
\centering
    \begin{tabularx}{\textwidth}{X>{\raggedright\arraybackslash}ccccccc}
    \toprule
    & \multicolumn{3}{c}{\textbf{Paired Differences1}} & & \multicolumn{3}{c}{\textbf{Paired Differences2}}\\
     \cmidrule(lr){2-4} \cmidrule(lr){6-8}
    & \thead{Statistic}
     & \thead{df }
     & \thead{ Sig. }
      & & \thead{Statistic}
     & \thead{df}
     & \thead{Sig.} \\
\midrule
    Difference & 44.20 & 14.36 & 4.54 & & .957 & 10 & .746\\
        \bottomrule
    \end{tabularx}
\caption{Testing Testing Testing%
    \label{tab:test1234}%
    }
\end{table}

\begin{table}[!ht]
\centering
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\setcellgapes{3pt}\makegapedcells
    \begin{tabularx}{\textwidth}{X>{\raggedright\arraybackslash}ccc!{\color{Gainsboro!50!Lavender}\vline width 0.75em}ccc}
    \toprule
    & \multicolumn{3}{c!{\color{Gainsboro!50!Lavender}\vline width 0.75em}}{\textbf{Paired Differences1}} & \multicolumn{3}{c}{\textbf{Paired Differences2}}\\\noalign{\vskip -0.033em}
     \cmidrule(lr{1.33em}){2-4} \cmidrule(lr){5-7}
    & \thead{Statistic}
     & \thead{df}
     & \thead{Sig.}
      & \thead{Statistic}
     & \thead{df}
     & \thead{Sig.} \\
    \noalign{\vskip-0.05em}
    \cmidrule[0.05em](r{0.9em}){1-4}\cmidrule[0.05em](l{0.15em}){5-7}
    Difference & 44.20 & 14.36 & 4.54 & .957 & 10 & .746\\
        \bottomrule
    \end{tabularx}
\caption{Testing Testing Testing%
    \label{tab:test1234}%
    }
\end{table}

\end{document} 

在此处输入图片描述

答案4

如果您确实想要绘制与 规则兼容的垂直规则booktabs(这完全不符合 的精神,您应该考虑的booktabs环境)。{NiceTabular}nicematrix

\documentclass{article}
\usepackage{nicematrix}
\usepackage{ragged2e} 
\usepackage{booktabs}

\begin{document}

\begin{table}[!ht]
\centering
\setlength{\tabcolsep}{7pt}
\begin{NiceTabular}{lccc|ccc}
\toprule
\RowStyle{\bfseries}
& \Block{1-3}{Paired Differences1} &&&\Block{1-3}{Paired Differences2}\\
 \cmidrule{2-7}
\RowStyle{\bfseries\small}
& Statistic
& df
& Sig.
& Statistic
& df
& Sig. \\ 
\midrule
Difference & 44.20 & 14.36 & 4.54 & .957 & 10 & .746\\
\bottomrule
\end{NiceTabular}
\caption{Testing Testing Testing%
    \label{tab:test1234}% 
    }  
\end{table}

\end{document}

上述代码的输出

相关内容