使列宽适合文本

使列宽适合文本

我有下表:

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

在此处输入图片描述

我感兴趣的是将第五列与其文本相匹配。只是为了看起来像第一列。我尝试更改一些参数,tabularx但未能获得所需的输出。

答案1

不要使用tabularx用于数字表,它旨在设置目标宽度换行在一个细胞内。

如果你只是使用tabular并且r一切都适合,你可以通过c 标题和其他小的改进来改进,但根本不使用我的代码是你在这里需要的主要改进,

在此处输入图片描述

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

答案2

对于包含数字数据的表格,我建议使用siunitx。无需人为地加宽表格以覆盖所有文本宽度(但tabular*如果确实需要,您可以使用表格)。

唯一棘手的设置是在第三列,其中的格式-2.2似乎更好(减号比数字宽)。语法很简单:-表示某行中存在减号,2.2表示数字在整数部分有两位数,在小数部分有两位数。小数点对齐是自动的。还请注意,S 列中的标题必须用括号括起来。

我认为使用单个表格也更好。

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{booktabs,array, caption,fixltx2e}
\usepackage[flushleft]{threeparttable}
\usepackage{tablefootnote}
\usepackage[margin=2cm]{geometry}

\usepackage{siunitx}

\begin{document}

\begin{table}[htb!]
\centering

\caption{Unit root tests} \label{tbl:unitroot}

\begin{tabular}{
  @{}
  l
  S[table-format=3.0]
  S[table-format=-2.2]
  S[table-format=2.2]
  l
  S[table-format=3.0]
  S[table-format=3.2]
  S[table-format=2.2]
  @{}
}
\toprule
\textit{Level} & {ADF} & {PP} & {KPSS} &
  \textit{Differenced} & {ADF} & {PP} & {KPSS} \\
\midrule
\multicolumn{8}{c}{\textbf{Confidence Indicators}} \\
\midrule
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 \\
\addlinespace
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 \\
\addlinespace
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 \\
\addlinespace
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 \\
\addlinespace
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 \\
\midrule[\heavyrulewidth]
\multicolumn{8}{c}{\textbf{Stock Returns}} \\
\midrule
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 \\
\addlinespace
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 \\
\addlinespace
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 \\
\addlinespace
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 \\
\addlinespace
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 \\
\bottomrule
\end{tabular}

\end{table}
\end{document}

在此处输入图片描述

答案3

我会从表格中完全删除一列,并重新构建标题,以便让读者清楚地了解报告的测试是如何组织的。我还将使用该dcolumn包将数字条目对齐到相应的小数点。要使表格材料跨越文本块的宽度,请考虑使用环境tabular*

另外:作为一名时间序列计量经济学家,我立即注意到“KPSS”中的“P”以及“PP”中的第一个“P”表示我的博士导师名字的首字母——“Phillips”。:-)

在此处输入图片描述

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{booktabs,array, caption,fixltx2e}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}}
\usepackage[margin=2cm]{geometry}% 
\begin{document}
\begin{table}[htb!]
    \caption{Unit root tests}
    \label{tbl:unitroot}
    \renewcommand{\arraystretch}{1.2}
    \setlength\tabcolsep{0pt}
    \begin{tabular*}{\textwidth}{@{}
           l @{\extracolsep{\fill}} c *{2}{d{3.2}} c *{2}{d{3.2}} @{} }
        \toprule
        Variable & \multicolumn{3}{c}{Level} & \multicolumn{3}{c}{Differenced}\\
        \cmidrule{2-4} \cmidrule{5-7}
        & \mc{ADF} & \mc{PP} & \mc{KPSS} 
        & \mc{ADF} & \mc{PP} & \mc{KPSS}\\ 
        \midrule
        \addlinespace
        \multicolumn{7}{l}{\textbf{Confidence Indicators}} \\[0.5ex]
        Market & 360 & 101.77 & 10.71  &  360 & 101.77 & 10.71  \\ 
        (p-value) & 360 & 101.77 & 10.71 &  360 & 101.77 & 10.71  \\ 
        Manufacturing & 360 & -7.33 & 12.63  &  360 & 7.33 & 12.63  \\
        (p-value) & 360 & 101.77 & 10.71 &  360 & 101.77 & 10.71  \\
        Construction & 360 & -17.23 & 23.43  &  360 & 3.40 & 13.84 \\
        (p-value) & 360 & 101.77 & 10.71&  360 & 101.77 & 10.71  \\  
        Services  & 216 & 3.99 & 19.19 &  216 & 3.99 & 19.19  \\
        (p-value) & 360 & 101.77 & 10.71  &  360 & 101.77 & 10.71  \\  
        Consumer & 360 & -9.31 & 8.64  &  360 & -9.31 & 8.64  \\
        (p-value) & 360 & 101.77 & 10.71 &  360 & 101.77 & 10.71  \\[2ex]
        \multicolumn{7}{l}{\textbf{Stock Returns}} \\[0.5ex]
        Market & 360 & 101.77 & 10.71  &  360 & 101.77 & 10.71  \\ 
        (p-value) & 360 & 101.77 & 10.71 &  360 & 101.77 & 10.71  \\ 
        Manufacturing & 360 & -7.33 & 12.63  &  360 & 7.33 & 12.63  \\
        (p-value) & 360 & 101.77 & 10.71 &  360 & 101.77 & 10.71  \\
        Construction & 360 & -17.23 & 23.43  &  360 & 3.40 & 13.84 \\
        (p-value) & 360 & 101.77 & 10.71&  360 & 101.77 & 10.71  \\  
        Services  & 216 & 3.99 & 19.19 &  216 & 3.99 & 19.19  \\
        (p-value) & 360 & 101.77 & 10.71  &  360 & 101.77 & 10.71  \\  
        Consumer & 360 & -9.31 & 8.64  &  360 & -9.31 & 8.64  \\
        (p-value) & 360 & 101.77 & 10.71 &  360 & 101.77 & 10.71  \\  
        \bottomrule
    \end{tabular*}
    \end{table}
\end{document}

相关内容