在 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為兩條相鄰的線,中間有一個小空白。為了清理程式碼,我還刪除了重複出現的 ot\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

S與和列類型的組合X,沒有垂直線,帶有四捨五入的數字...:

\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

我寧願用補充空列替換垂直線,以便在兩組列之間有清晰的分隔。從美學角度來看,另一種可能性可能是刪除水平規則的垂直填充,並將其替換為\makegapedcellsfrom 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}

上述程式碼的輸出

相關內容