1

I am having so much trouble in making a table.

I know that \thread about the split the content in a cell into 2 row. But not successful.

Basically, I want to draw a table like this below:

enter image description here

The output of mine:

enter image description here

I am using

\begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}Xllrr}

My Full MWE:

\documentclass[12pt,oneside]{book}

\usepackage[showframe]{geometry}
\usepackage{amsmath}
\usepackage{ragged2e}
\usepackage{makecell, multirow, tabularx}
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\renewcommand\theadgape{}
\usepackage[svgnames, table]{xcolor}
\usepackage{hhline, boldline}
\usepackage{seqsplit, caption} %for table spacing to second row
\usepackage{booktabs, ragged2e} % Use booktabs rules and get rid of vertical rules, ragged2e to ragged text
\usepackage{siunitx} %for table spacing to second row
\usepackage{threeparttable} %to add footnote below table
\usepackage{tabulary}
\usepackage{graphicx}

\begin{document}

\begin{table}[h!]
\centering
    \begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}Xllrr}

    % \begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}Xrrrrrr}


    \toprule
    & & & \multicolumn{2}{c}{\textbf{Cholestrrol Levellll}} \\
     \cmidrule{4-5}
     & &  & \thead{{\textbf{Equal}} \\ \textbf{variances} \\ \textbf{assumed}}
     & \thead{{\textbf{Equal}} \\ \textbf{variances not} \\ \textbf{assumed}}\\
\midrule

    \multicolumn{1}{l}{Levene's Test for Equality of Variances} & F 
    & & 0.314 &   \\
    & Sig. & & 0.579 & \\

\midrule

    \multicolumn{1}{l}{t-test for Equality of Means} & t
    & & 2.428 & 2.428   \\
    & df & & 38 & 34.886 \\
    & Sig. (2-tailed) &  & 0.579 & 0.64273\\
    & 95\% Confidence &  Lower & 0.579 & 0.64273\\

\bottomrule

\end{tabularx}
\end{table}
\end{document} 
aan
  • 2,663
  • The problems come from the \multicolumn{1}{l}{... in the leftmost column (which is of X type). Things look much better if you replace them by \multicolumn{1}{X}{..., or just use no \multicolumn there at all. –  Nov 10 '19 at 01:08

1 Answers1

2

The left-most column is "special", it is of the X type, meaning it adjusts is size to make the title as wide as you want it to be. Now, when you use

\multicolumn{1}{l}{Levene's Test for Equality of Variances}

and a second statement of that sort, you kill that poor X column. This makes David Carlisle unhappy, and the table go berserk. If you remove these \multicolumns in the first column, you get

\documentclass[12pt,oneside]{book}
\usepackage[showframe]{geometry}
\usepackage{amsmath}
\usepackage{makecell, multirow, tabularx}
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\renewcommand\theadgape{}
\usepackage{booktabs, ragged2e} % Use booktabs rules and get rid of vertical rules, ragged2e to ragged text

\begin{document}

\begin{table}[h!]
\centering
    \begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}Xllrr}

    % \begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}Xrrrrrr}


    \toprule
    & & & \multicolumn{2}{c}{\textbf{Cholestrrol Levellll}} \\
     \cmidrule{4-5}
     & &  & \thead{{\textbf{Equal}} \\ \textbf{variances} \\ \textbf{assumed}}
     & \thead{{\textbf{Equal}} \\ \textbf{variances not} \\ \textbf{assumed}}\\
\midrule

    Levene's Test for Equality of Variances
    & F 
    & & 0.314 &   \\
    & Sig. & & 0.579 & \\

\midrule

    t-test for Equality of Means
    & t
    & & 2.428 & 2.428   \\
    & df & & 38 & 34.886 \\
    & Sig. (2-tailed) &  & 0.579 & 0.64273\\
    & 95\% Confidence &  Lower & 0.579 & 0.64273\\

\bottomrule

\end{tabularx}
\end{table}
\end{document} 

enter image description here

In general, a \multicolumn{1}{<type>}{...} makes only sense if type deviates from the type specified in the header. However, in your example, the only entries of the X column were \multicolumn entries.

  • Thanks. Brillant. Is there a way to make second column similar to first column too? For example 95\% Confidence Interval of the Differene? – aan Nov 10 '19 at 01:21
  • @aan You can have more than one X column, if that's what you are asking. –  Nov 10 '19 at 01:23
  • @aan Or do you want to make the second column >{\raggedright\arraybackslash}l? (I do not understand the question.) –  Nov 10 '19 at 01:30
  • Thanks. I got your answer. I tried \begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash}XXlrr} works well. However, a second question came out which is: https://tex.stackexchange.com/questions/515754/how-to-make-a-small-blank-line-row-in-a-table-because-my-content-overlap-the – aan Nov 10 '19 at 01:31