3

How can we draw a continuous vertical line using booktabs in table? What is the simplest method to maximise the width of a table? There are lots of methods, very confusing.

\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}

enter image description here

  • 1
    A tabularx environment requires at least one X column to make sense. This answers you second question. As to the first, booktabs is not adapted to vertical rules. For which reason do you ue it? – Bernard Aug 11 at 17:27
  • 4
    To quote from the booktabs manual: "You will not go far wrong if you remember two simple guidelines at all times: 1. Never, ever use vertical rules. [...]" Therefore, I'd recommend to either stick to this rule, or, if you prefer vertical lines, use \hline instead of teh booktabs horizontal lines. – leandriis Aug 11 at 17:39
  • Please also note, that oyu can remove \small from a \thead commands, as you already used \renewcommand\theadfont{\small}´ to automatically make all text in \thead` commands small. You can go even further and remove all the \textbf inside of \thead commands, if you use \renewcommand\theadfont{\small\bfseries} – leandriis Aug 11 at 17:43
  • Regarding "the simplest method to maximise the width of a table" where would you like the extra white space to be? Between column 1 and 2, or 4 and 5 or equally distributed between all columns? Are there entries in the first column that are wider than the shown one? Why would wou even want your table to be wieder than it currently is? – leandriis Aug 11 at 17:48
  • 1
    Regarding the vertical lines, you might also want to have a loom at Vertical table lines are discontinuous with booktabs – leandriis Aug 11 at 17:49
4

Here is my suggestion. I have used tabular* in combination with @{\extracolsep{\fill}} to make the table as wide as the textwidth and to evenly distribute the excess white space between the columns. I have also removed the vertical line and replaced the single \cmidrule by two adjacent ones with a small white space inbetween. In order to clean up the code, I have also removed the repeated occurences ot \small and \textbf and instead added \bfseries to \thedfont:

enter image description here

\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}
5

With combination of S and X columns type, without vertical lines, with rounded numbers ... :

\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}

enter image description here

4

I would rather replace the vertical line with a supplementary empty column to have a clear separation between the two group of columns. Another possibility, aesthetically, might be to delete the vertical padding of horizontal rules, and replace it with the \makegapedcells command from makecell, which adds a vertical space at the top and bottom of all cells. As a demonstration, I replaced the vertical line with thick, light grey vrule, which I find more pleasing to the eye than the default thin, black, vertical rule.

\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} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.