5

I have a table below and want to set the width of the column based on the length of the text rather than setting it manually. Many thanks in advance. enter image description here

        \begin{table}
    \centering
    \footnotesize
    %\setlength{\tabcolsep}{1pt}
    \begin{tabularx}{\linewidth}{RCCCC} %{\linewidth}{@{}p{\dimexpr 3cm+\tabcolsep}@{\extracolsep{\fill}}*{4}{c}@{}} %{\linewidth}{@{}p{2.5cm}XcXc@{}}
        \toprule
    %   Variable   &  \multicolumn{4}{c}{Factor} \\ 
    Variable & \multicolumn{4}{c}{\normalsize Factor}\\
\cmidrule{2-5}
& BchMk &   CCP.0 & CCP.1 & X2-test\\
\cmidrule{2-5}
        Gender   & 76.27\% & 75.32\% & 80.31\%  & p-value $<$ 2.2e-16 \\
        Married   & 86.00\% & 85.19\% & 89.44\%   & p-value $<$ 2.2e-16 \\
        Employed  & 64.10\% & 65.31\% & 59.05\%   & p-value $<$ 2.2e-16 \\
        Education  & 32.07\%  & 26.17\% & 56.94\%   & p-value $<$ 2.2e-16 \\
        Party & 19.18\%  &  -----  &    -----   &   -----  \\
        HR    & 66.35\%  & 63.58\% & 78.05\%   &  p-value $<$ 2.2e-16 \\
        Fin.Knowledge  & 5.45\%  & 3.99\% & 11.61\%   &  p-value $<$ 2.2e-16 \\ %\addlinespace[1em]
        Fin.Inter & 18.46\%  & 18.80\% & 17.05\%   & p-value $=$ 0.001372\\ %\addlinespace[1em]
        Region-East & 48.26\% & 47.30\% & 52.31\% & p-value $=$ 9.402e-13 \\
        Region-Center & 24.48\% & 27.60\% & 25.82\% & p-value $=$ 0.004385 \\
        Region-West &   27.25\% & 25.10\% & 21.87\% & p-value $=$ 8.985e-08\\
    Formal & 11.65\% & 10.09\% & 18.22\% & p-value $<$ 2.2e-16 \\
    Informal & 13.96\% & 15.36\% &  8.10\% & p-value $<$ 2.2e-16 \\
    Both &  4.79\% & 4.72\% & 5.12\% & p-value $=$ 0.1761 \\
    No Loans & 69.58\% & 69.83\% & 68.57\% & p-value $=$ 0.05022 \\
    Total & 32765 & 26479 &     6286 &  -----  \\
    \end{tabularx}
    \vspace{1ex}
    \footnotesize
    \begin{tabularx}{\linewidth}{RCCCC} %{\linewidth}{@{}p{\dimexpr 2cm+\tabcolsep}@{\extracolsep{\fill}}*{4}{c}@{}}
        & \multicolumn{4}{c}{\normalsize Continuous}\\
        \cmidrule{2-5}
        & BchMk &   CCP.0 & CCP.1 & t-test\\
        \cmidrule{2-5}
    
Age & 53.9  &  53.46 &  55.72 &  2.26***\\
        & (14.28) & (14.07) & (15.01) &  (0.21)\\
Income & 69731.55 & 64184.65 & 93097.17 &   28912.52***\\
           & (175283.5) & (171285.19) &  (189449.94) &  (2611.07)\\
Networth & 745862.37 & 661085.85  & 1102972.99  &   441887.15***\\
 & (1603699.6) & (1499929.43) & (1941971.67) &  (26170.78)\\
    NW-HE & 708689.99 & 630009.07  & 1040123.66  &  410114.588*** \\
          & (1526658.33) & (1427490.00) & (1851839.12) & (24950.02) \\
Liquid Assets & 799474.99 & 711676.72 & 1169314.39 & 457637.67*** \\
              & (1633306.2) & (1526257.28) & (1980845.86) & (26686.67)\\    
    %Age & 53.9 (14.28) &  53.46(14.07) &   55.72(15.01) &  2.26 (0.21)***\\
    %Income & 69731.55(175283.5) & 64184.65 (171285.19) & 93097.17 (189449.94) &    28912.52 (2611.07)***\\
    %Networth & 745862.37(1603699.6) & 661085.85 (1499929.43) & 1102972.99 (1941971.67) &   441887.15 (26170.78)***\\
    %NW-HE & 708689.99(1526658.33) & 630009.07 (1427490.00) & 1040123.66 (1851839.12) &     410114.588 (24950.02)*** \\
    %Liquid Assets & 799474.99(1633306.2) & 711676.72 (1526257.28) & 1169314.39 (1980845.86) & 457637.67 (26686.67)*** \\
        \bottomrule
        \addlinespace
        \multicolumn{5}{@{}p{\textwidth}@{}}{\footnotesize Note: HR stands for Household Registration. NW-HE is net-worth minus home equity. All the asset variables (e.g. income, net-worth, NW-HE, and liquid assets are in Chinese renminbi (CNY).} \\
    \end{tabularx}
    \caption{Summary statistics}
    \label{tab:SampleDescription}
\end{table}
4
  • 2
    Just use a normal tabular with l and c columns. Jan 9, 2022 at 8:05
  • 3
    Please tell us how the R and C column types are defined. Please also tell us what a 'proportional' width is supposed to be, i.e., proportional to what?
    – Mico
    Jan 9, 2022 at 9:26
  • 2
    Off-topic: 2.2e-16 would appear to be the smallest positive number your statistical software package can represent. Instead of writing $<$ 2.2e-16, it may be more informative to write $\approx 0$.
    – Mico
    Jan 9, 2022 at 9:29
  • \newcolumntype{C}{>{\centering\arraybackslash}X} \newcolumntype{R}{>{\raggedright\arraybackslash}X} proportional to the width of the other colums Jan 9, 2022 at 9:53

3 Answers 3

3

Use a single tabular* environment. The note can be typeset outside of it.

\documentclass{article}
\usepackage{booktabs}

\begin{document}

\begin{table}[htp]
\footnotesize
\setlength{\tabcolsep}{0pt}

\begin{tabular*}{\linewidth}{
  @{\extracolsep{\fill}} l cccc
}
\toprule
Variable   &  \multicolumn{4}{c}{Factor} \\ 
\cmidrule{2-5}
& BchMk &   CCP.0 & CCP.1 & X2-test\\
\cmidrule{2-5}
        Gender   & 76.27\% & 75.32\% & 80.31\%  & p-value $<$ 2.2e-16 \\
        Married   & 86.00\% & 85.19\% & 89.44\%   & p-value $<$ 2.2e-16 \\
        Employed  & 64.10\% & 65.31\% & 59.05\%   & p-value $<$ 2.2e-16 \\
        Education  & 32.07\%  & 26.17\% & 56.94\%   & p-value $<$ 2.2e-16 \\
        Party & 19.18\%  &  ---  &    ---   &   ---  \\
        HR    & 66.35\%  & 63.58\% & 78.05\%   &  p-value $<$ 2.2e-16 \\
        Fin.Knowledge  & 5.45\%  & 3.99\% & 11.61\%   &  p-value $<$ 2.2e-16 \\ %\addlinespace[1em]
        Fin.Inter & 18.46\%  & 18.80\% & 17.05\%   & p-value $=$ 0.001372\\ %\addlinespace[1em]
        Region-East & 48.26\% & 47.30\% & 52.31\% & p-value $=$ 9.402e-13 \\
        Region-Center & 24.48\% & 27.60\% & 25.82\% & p-value $=$ 0.004385 \\
        Region-West &   27.25\% & 25.10\% & 21.87\% & p-value $=$ 8.985e-08\\
    Formal & 11.65\% & 10.09\% & 18.22\% & p-value $<$ 2.2e-16 \\
    Informal & 13.96\% & 15.36\% &  8.10\% & p-value $<$ 2.2e-16 \\
    Both &  4.79\% & 4.72\% & 5.12\% & p-value $=$ 0.1761 \\
    No Loans & 69.58\% & 69.83\% & 68.57\% & p-value $=$ 0.05022 \\
    Total & 32765 & 26479 &     6286 &  ---  \\[3ex]
        & \multicolumn{4}{c}{Continuous}\\
        \cmidrule{2-5}
        & BchMk &   CCP.0 & CCP.1 & t-test\\
        \cmidrule{2-5}
    
Age & 53.9  &  53.46 &  55.72 &  2.26***\\
        & (14.28) & (14.07) & (15.01) &  (0.21)\\
Income & 69731.55 & 64184.65 & 93097.17 &   28912.52***\\
           & (175283.5) & (171285.19) &  (189449.94) &  (2611.07)\\
Networth & 745862.37 & 661085.85  & 1102972.99  &   441887.15***\\
 & (1603699.6) & (1499929.43) & (1941971.67) &  (26170.78)\\
    NW-HE & 708689.99 & 630009.07  & 1040123.66  &  410114.588*** \\
          & (1526658.33) & (1427490.00) & (1851839.12) & (24950.02) \\
Liquid Assets & 799474.99 & 711676.72 & 1169314.39 & 457637.67*** \\
              & (1633306.2) & (1526257.28) & (1980845.86) & (26686.67)\\    
    %Age & 53.9 (14.28) &  53.46(14.07) &   55.72(15.01) &  2.26 (0.21)***\\
    %Income & 69731.55(175283.5) & 64184.65 (171285.19) & 93097.17 (189449.94) &    28912.52 (2611.07)***\\
    %Networth & 745862.37(1603699.6) & 661085.85 (1499929.43) & 1102972.99 (1941971.67) &   441887.15 (26170.78)***\\
    %NW-HE & 708689.99(1526658.33) & 630009.07 (1427490.00) & 1040123.66 (1851839.12) &     410114.588 (24950.02)*** \\
    %Liquid Assets & 799474.99(1633306.2) & 711676.72 (1526257.28) & 1169314.39 (1980845.86) & 457637.67 (26686.67)*** \\
        \bottomrule
    \end{tabular*}

\smallskip

Note: HR stands for Household Registration. NW-HE is net-worth minus home equity. 
All the asset variables (e.g. income, net-worth, NW-HE, and liquid assets are in 
Chinese renminbi (CNY).

\caption{Summary statistics}
\label{tab:SampleDescription}

\end{table}

\end{document}

enter image description here

Don't forget the p specifier for \begin{table}.

5

I have a table below and want to set the width of the column based on the length of the text rather than setting it manually.

If you use \extracolsep{\fill} inside tabular* environment, the extra space is evenly distributed without considering the column widths. But if you use X[-1] inside tblr environment of tabularray package, the extra space is distributed based on the column widths.

\documentclass{article}

\usepackage{array}
\usepackage{tabularray}

\setlength{\parindent}{0pt}

\begin{document}

When you use \verb!\extracolsep{\fill}! inside \texttt{tabular*} environment,
the extra space is evenly distributed without considering the column widths:

\medskip

\setlength{\tabcolsep}{0pt}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}ccccc}
\hline
X & XX & XXX & XXXX & XXXXX \\
\hline
\end{tabular*}

\bigskip

When you use \verb!X[-1]! inside \texttt{tblr} environment,
the extra space is distributed based on the column widths:

\medskip

\begin{tblr}{
  colsep=0pt,
  colspec={|X[-1,c]|X[-1,c]|X[-1,c]|X[-1,c]|X[-1,c]|},
}
\hline
X & XX & XXX & XXXX & XXXXX \\
\hline
\end{tblr}

\end{document}

enter image description here

3

Here's a solution that employs two tabular environments.

enter image description here

\documentclass{article}
\usepackage{booktabs,array}
\newcommand{\px}{\phantom{0}}

\begin{document}
\begin{table}[p]
    \caption{Summary statistics\strut}
    \label{tab:SampleDescription}

    \centering
    \begin{tabular}{@{} l cccc @{}}
    \toprule
    Variable & \multicolumn{4}{c@{}}{\normalsize Factor}\\
    \cmidrule(l){2-5}
    & BchMk &   CCP.0 & CCP.1 & $p$-value of X2-test\\
    \midrule
    Gender       & 76.27\% & 75.32\% & 80.31\%  &  $\approx0$ \\
    Married      & 86.00\% & 85.19\% & 89.44\%   &  $\approx0$ \\
    Employed     & 64.10\% & 65.31\% & 59.05\%   &  $\approx0$ \\
    Education    & 32.07\%  & 26.17\% & 56.94\%   &  $\approx0$ \\
    Party        & 19.18\%  &  ---  &    ---   &   ---  \\
    Household Reg. & 66.35\%  & 63.58\% & 78.05\%   &   $\approx0$ \\
    Fin.\ Knowledge& \px5.45\%  & \px3.99\% & 11.61\%   &   $\approx0$ \\
    Fin.\ Interm.  & 18.46\%  & 18.80\% & 17.05\%   &  0.001372\\
    Region-East  & 48.26\% & 47.30\% & 52.31\% &  9.402e-13 \\
    Region-Center& 24.48\% & 27.60\% & 25.82\% &  0.004385 \\
    Region-West  & 27.25\% & 25.10\% & 21.87\% &  8.985e-08\\
    Formal       & 11.65\% & 10.09\% & 18.22\% &  $\approx0$ \\
    Informal     & 13.96\% & 15.36\% & \px8.10\% &  $\approx0$ \\
    Both         & \px4.79\% & \px4.72\% & \px5.12\% &  0.1761 \\
    No Loans     & 69.58\% & 69.83\% & 68.57\% &  0.05022 \\ 
    \addlinespace
    Total        & 32765 & 26479 & 6286 &  ---  \\
    \bottomrule
    \end{tabular}

    \bigskip\bigskip
    \begin{tabular}{@{} lcccc @{}} 
    \toprule
    & \multicolumn{4}{c@{}}{Continuous}\\
    \cmidrule(l){2-5}
    & BchMk &   CCP.0 & CCP.1 & $t$-test\\
    \midrule
    
    Age & 53.9  &  53.46 &  55.72 &  2.26$^{***}$\\
        & (14.28) & (14.07) & (15.01) &  (0.21)\\
    Income & 69731.55 & 64184.65 & 93097.17 &   28912.52$^{***}$\\
           & (175283.5) & (171285.19) &  (189449.94) &  (2611.07)\\
    Networth & 745862.37 & 661085.85  & 1102972.99  &   441887.15$^{***}$\\
             & (1603699.6) & (1499929.43) & (1941971.67) &  (26170.78)\\
    NW$-$HE & 708689.99 & 630009.07  & 1040123.66  &  410114.588$^{***}$ \\
          & (1526658.33) & (1427490.00) & (1851839.12) & (24950.02) \\
    Liquid Assets & 799474.99 & 711676.72 & 1169314.39 & 457637.67$^{***}$ \\
                  & (1633306.2) & (1526257.28) & (1980845.86) & (26686.67)\\    
    \midrule
    \multicolumn{5}{@{}p{0.97\textwidth}@{}}{\footnotesize\raggedright Notes: NW$-$HE is net worth minus home equity. All asset variables (e.g., income, net worth, NW$-$HE, and liquid assets) are in Chinese renminbi (CNY).} \\
   \bottomrule
    \end{tabular}
\end{table}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .