[Tex/LaTex] Fitting column width to text

tablestabularx

I have the following table:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{booktabs,array, caption,fixltx2e}
\usepackage[flushleft]{threeparttable}
\usepackage{tablefootnote}
\usepackage[margin=2cm]{geometry}% http://ctan.org/pkg/geometry
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}% raggedleft column X
\begin{document}
\begin{table}[htb!]
    \caption{Unit root tests}
    \label{tbl:unitroot}
    \renewcommand{\arraystretch}{1.2}%
    \begin{tabularx}{\textwidth}{l*{7}{Y}}
        \toprule
        \multicolumn{8}{l}{\textbf{Confidence Indicators}} \\
        \midrule
        Level & ADF & PP & KPSS& Differenced& ADF& PP& KPSS\\ 
        \hline
        Market & 360 & 101.77 & 10.71  & $\Delta$ Market & 360 & 101.77 & 10.71  \\ 
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\ 
        Manufacturing & 360 & -7.33 & 12.63  & $\Delta$ Manufacturing & 360 & 7.33 & 12.63  \\
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\
        Construction & 360 & -17.23 & 23.43  & $\Delta$ Retail trade & 360 & 3.40 & 13.84 \\
        (p-value) & 360 & 101.77 & 10.71& (p-value) & 360 & 101.77 & 10.71  \\  
        Services  & 216 & 3.99 & 19.19 & $\Delta$ Services  & 216 & 3.99 & 19.19  \\
        (p-value) & 360 & 101.77 & 10.71  & (p-value) & 360 & 101.77 & 10.71  \\  
        Consumer & 360 & -9.31 & 8.64  & $\Delta$ Consumer & 360 & -9.31 & 8.64  \\
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\  
    \end{tabularx}
    \begin{tabularx}{\linewidth}{l*{7}{Y}}
        \toprule
        \multicolumn{8}{l}{\textbf{Stock Returns}} \\
        \midrule
        Level & ADF & PP & KPSS& Differenced& ADF& PP& KPSS\\ 
        \hline
        Market & 360 & 101.77 & 10.71  & $\Delta$ Market & 360 & 101.77 & 10.71  \\ 
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\ 
        Manufacturing & 360 & -7.33 & 12.63  & $\Delta$ Manufacturing & 360 & 7.33 & 12.63  \\
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\
        Construction & 360 & -17.23 & 23.43  & $\Delta$ Retail trade & 360 & 3.40 & 13.84 \\
        (p-value) & 360 & 101.77 & 10.71& (p-value) & 360 & 101.77 & 10.71  \\  
        Services  & 216 & 3.99 & 19.19 & $\Delta$ Services  & 216 & 3.99 & 19.19  \\
        (p-value) & 360 & 101.77 & 10.71  & (p-value) & 360 & 101.77 & 10.71  \\  
        Consumer & 360 & -9.31 & 8.64  & $\Delta$ Consumer & 360 & -9.31 & 8.64  \\
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\  
        \hline
    \end{tabularx}
    \end{table}
\end{document}

enter image description here

I am interested in fitting the fifth column to its text. Just to look like the first column. I have tried to change some of the tabularx parameters but faild to do the desired output.

Best Answer

Don't use tabularx for numeric tables, it is designed to set the target width for line breaking within a cell.

If you just use tabular and r it all fits, you could improve with c for headings and other small improvements, but simply not using my code is the main improvement you need here,

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{booktabs,array, caption,fixltx2e}
\usepackage[flushleft]{threeparttable}
\usepackage{tablefootnote}
\usepackage[margin=2cm]{geometry}% http://ctan.org/pkg/geometry
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}% raggedleft column X
\begin{document}
\begin{table}[htb!]
    \caption{Unit root tests}
    \label{tbl:unitroot}
    \renewcommand{\arraystretch}{1.2}%
    \begin{tabular}{l*{7}{r}}
        \toprule
        \multicolumn{8}{l}{\textbf{Confidence Indicators}} \\
        \midrule
        Level & ADF & PP & KPSS& Differenced& ADF& PP& KPSS\\ 
        \hline
        Market & 360 & 101.77 & 10.71  & $\Delta$ Market & 360 & 101.77 & 10.71  \\ 
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\ 
        Manufacturing & 360 & -7.33 & 12.63  & $\Delta$ Manufacturing & 360 & 7.33 & 12.63  \\
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\
        Construction & 360 & -17.23 & 23.43  & $\Delta$ Retail trade & 360 & 3.40 & 13.84 \\
        (p-value) & 360 & 101.77 & 10.71& (p-value) & 360 & 101.77 & 10.71  \\  
        Services  & 216 & 3.99 & 19.19 & $\Delta$ Services  & 216 & 3.99 & 19.19  \\
        (p-value) & 360 & 101.77 & 10.71  & (p-value) & 360 & 101.77 & 10.71  \\  
        Consumer & 360 & -9.31 & 8.64  & $\Delta$ Consumer & 360 & -9.31 & 8.64  \\
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\  
    \end{tabular}


    \begin{tabular}{l*{7}{l}}
        \toprule
        \multicolumn{8}{l}{\textbf{Stock Returns}} \\
        \midrule
        Level & ADF & PP & KPSS& Differenced& ADF& PP& KPSS\\ 
        \hline
        Market & 360 & 101.77 & 10.71  & $\Delta$ Market & 360 & 101.77 & 10.71  \\ 
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\ 
        Manufacturing & 360 & -7.33 & 12.63  & $\Delta$ Manufacturing & 360 & 7.33 & 12.63  \\
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\
        Construction & 360 & -17.23 & 23.43  & $\Delta$ Retail trade & 360 & 3.40 & 13.84 \\
        (p-value) & 360 & 101.77 & 10.71& (p-value) & 360 & 101.77 & 10.71  \\  
        Services  & 216 & 3.99 & 19.19 & $\Delta$ Services  & 216 & 3.99 & 19.19  \\
        (p-value) & 360 & 101.77 & 10.71  & (p-value) & 360 & 101.77 & 10.71  \\  
        Consumer & 360 & -9.31 & 8.64  & $\Delta$ Consumer & 360 & -9.31 & 8.64  \\
        (p-value) & 360 & 101.77 & 10.71 & (p-value) & 360 & 101.77 & 10.71  \\  
        \hline
    \end{tabular}
    \end{table}
\end{document}
Related Question