5

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

leandriis
  • 62,593
aan
  • 2,663
  • 2
    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 '19 at 17:27
  • 5
    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 '19 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\theadcommands small. You can go even further and remove all the\textbfinside of\theadcommands, if you use\renewcommand\theadfont{\small\bfseries}` – leandriis Aug 11 '19 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 '19 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 '19 at 17:49
  • @ leandriis, I would like the extra white space to be equally distributed between all columns. How can I do that? – aan Aug 11 '19 at 18:14
  • You can do this by using \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lccc|ccc} instead of \begin{tabularx}... and \end{tabular*} instead of \end{tabularx}. – leandriis Aug 11 '19 at 18:23
  • I can try steak to tabularx, using \begin{tabularx}{\textwidth}{l>{\raggedright\arraybackslash}XXXXXX} – aan Aug 11 '19 at 18:24
  • This will change the horizontal alignment of the columns containing the numbers. With c you requested them to be centered, while the contents of X type columns are justified. – leandriis Aug 11 '19 at 18:28
  • @leandriis, thanks. i think tabular* better in this case than tabularx where is alight to content to centre. There are so many tabular, tabular*, tabularx, tabulary....i guess more to come – aan Aug 11 '19 at 18:30
  • For an overvies over table related packages you might want to have a look at this excellent list: Which tabular packages do which tasks and which packages conflict? – leandriis Aug 11 '19 at 18:58

4 Answers4

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

Zarko
  • 296,517
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

Bernard
  • 271,350
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}
leandriis
  • 62,593
1

If you actually want to draw vertical rules compatible with the rules of booktabs (which is not at all in the spirit of booktabs, you should consider the environment {NiceTabular} of 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}

Output of the above code

F. Pantigny
  • 40,250