这篇文章是使用 Stargazer 切断表格。我又遇到了一个关于桌子被截断的问题。不过这次似乎与桌子宽度有关。
我尝试充实代码,而之前我使用的是 R Studio 中各种软件包可生成的简单片段。这里的重点是,我一直在尝试让表格更好地适应空间,因为它目前超出了页面范围 - 这是低效的,因为它似乎不会向左移动。
我尝试使用“左对齐”,因为我看到了这里明显类似的问题将表格左移,使其适合页面看起来什么都没做。不幸的是,你会发现我把代码弄得一团糟。我完全打算学会独立使用它,但现在我正在即兴地做这件事。
\documentclass{article}
\usepackage[english]{babel}
\usepackage{booktabs,tabularx,caption,ragged2e}
\captionsetup{skip=0.333\baselineskip}
\newcolumntype{L}{>{\hsize=7.5\hsize}X}
\newlength\mylength
\settowidth\mylength{Multicultu} % width of 2nd col.
\begin{document}
\begin{table}[!htbp]
\setlength\tabcolsep{2pt} % default: 6pt
\caption{} \label{}
\begin{flushleft}
\begin{tabularx}{\textwidth}{@{} l p{\mylength} LLLLL @{}}
\toprule
& Test & Trust & Demographics & Immigration & Economic Model & Issue Satisfaction & Demographic No Country \\
\hline
1 & R2 & 0.016 & 0.2 & 0.086 & 0.007 & 0.021 & 0.027 \\
2 & Predicted & 83.20\% & 83.3\%\% & 83.40\% & 82.7\%\% & 83.20\% & 84.50\% \\
3 & AUC Index & 0.585 & 0.79 & 0.701 & 0.559 & 0.603 & 0.61 \\
4 & Sum of squared errors & 4861.786003 & 4773.002716 & 1847.897222 & 1249.544564 & 5009.179836 & 5310.168732 \\
5 & Expected value$|$H0 & 4862.80413 & 4777.131906 & 1835.302972 & 1249.164639 & 5003.713449 & 5311.013662 \\
6 & SD & 0.545436987 & 3.006760697 & 1.739686944 & 0.10557875 & 0.673923831 & 0.953354286 \\
7 & Z & -1.866627646 & -1.373301832 & 7.239377177 & 3.598496041 & 8.111283371 & -0.886270167 \\
8 & P & 0.061953604 & 0.169658533 & 4.51E-13 & 0.000320063 & 5.01E-16 & 0.375471961 \\
\bottomrule
\end{tabularx}
\end{flushleft}
\end{table}
\end{document}
答案1
发布的代码产生
! Extra alignment tab has been changed to \cr.
因为它有 7 个声明列但有 8 列数据。
添加一个额外的 L 来解决这个问题,然后你就会收到警告
Package tabularx Warning: X Columns too narrow (table too wide)
(tabularx) on input line 26.
因为
\newcolumntype{L}{>{\hsize=7.5\hsize}X}
每次迭代时强制每列的宽度比计算的宽度宽七倍半,因此tabularx
不可能收敛到所要求的表格宽度。
然后!
删除
\begin{table}[!htbp]
仅应在特殊情况下使用。
然后我添加了dcolumn
允许小数对齐的列(你可以改用siunitx
)
然后为标题定义一个命令,
那么它就有点适合了,但是它仍然太紧而无法阅读,你可以使用它\tiny
而不是\footnotesize
,但是你需要给出那么多小数位吗?
\documentclass{article}
\usepackage[english]{babel}
\usepackage{booktabs,tabularx,caption,ragged2e}
\captionsetup{skip=0.333\baselineskip}
\usepackage{dcolumn}
\newcommand\hd[1]{\multicolumn{1}{c}{\begin{tabular}{@{}c@{}}#1\end{tabular}}}
\begin{document}
\begin{table}[htbp]
\centering
\footnotesize
\setlength\tabcolsep{1pt} % default: 6pt
\caption{} \label{}
\begin{tabular}{@{}rp{2cm} *{6}{D..{1.9}}@{}}
\toprule
&
\hd{Test} &
\hd{Trust}&
\hd{Demo-\\graphics} &
\hd{Immi-\\gration}&
\hd{Economic\\Model}&
\hd{Issue\\Satisfaction}&
\hd{Demo-\\graphic\\No\\Country}\\
\hline
1 & R2 & 0.016 & 0.2 & 0.086 & 0.007 & 0.021 & 0.027 \\
2 & Predicted & 83.20\% & 83.3\%\% & 83.40\% & 82.7\%\% & 83.20\% & 84.50\% \\
3 & AUC Index & 0.585 & 0.79 & 0.701 & 0.559 & 0.603 & 0.61 \\
4 & Sum of\newline squared errors & 4861.786003 & 4773.002716 & 1847.897222 & 1249.544564 & 5009.179836 & 5310.168732 \\
5 & Expected value$|$H0 & 4862.80413 & 4777.131906 & 1835.302972 & 1249.164639 & 5003.713449 & 5311.013662 \\
6 & SD & 0.545436987 & 3.006760697 & 1.739686944 & 0.10557875 & 0.673923831 & 0.953354286 \\
7 & Z & -1.866627646 & -1.373301832 & 7.239377177 & 3.598496041 & 8.111283371 & -0.886270167 \\
8 & P & 0.061953604 & 0.169658533 & 4.51E-13 & 0.000320063 & 5.01E-16 & 0.375471961 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
\documentclass{article}
\usepackage[english]{babel}
\usepackage{booktabs,tabularx,caption,ragged2e}
\captionsetup{skip=0.333\baselineskip}
\usepackage{dcolumn}
\newcommand\hd[1]{\multicolumn{1}{c}{\begin{tabular}{@{}c@{}}#1\end{tabular}}}
\begin{document}
\begin{table}[htbp]
\centering
\footnotesize
\setlength\tabcolsep{1pt} % default: 6pt
\caption{} \label{}
\begin{tabular}{@{}rp{2cm} *{6}{D..{1.9}}@{}}
\toprule
&
\hd{Test} &
\hd{Trust}&
\hd{Demo-\\graphics} &
\hd{Immi-\\gration}&
\hd{Economic\\Model}&
\hd{Issue\\Satisfaction}&
\hd{Demo-\\graphic\\No\\Country}\\
\hline
1 & R2 & 0.016 & 0.2 & 0.086 & 0.007 & 0.021 & 0.027 \\
2 & Predicted & 83.20\% & 83.3\%\% & 83.40\% & 82.7\%\% & 83.20\% & 84.50\% \\
3 & AUC Index & 0.585 & 0.79 & 0.701 & 0.559 & 0.603 & 0.61 \\
4 & Sum of\newline squared errors & 4861.786003 & 4773.002716 & 1847.897222 & 1249.544564 & 5009.179836 & 5310.168732 \\
5 & Expected value$|$H0 & 4862.80413 & 4777.131906 & 1835.302972 & 1249.164639 & 5003.713449 & 5311.013662 \\
6 & SD & 0.545436987 & 3.006760697 & 1.739686944 & 0.10557875 & 0.673923831 & 0.953354286 \\
7 & Z & -1.866627646 & -1.373301832 & 7.239377177 & 3.598496041 & 8.111283371 & -0.886270167 \\
8 & P & 0.061953604 & 0.169658533 & 4.51E-13 & 0.000320063 & 5.01E-16 & 0.375471961 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
D..{2.9}
实际上,您需要一种允许使用减号的格式,但如上所述,对于表格来说,9 位小数似乎太多了。
\documentclass{article}
\usepackage[english]{babel}
\usepackage{booktabs,tabularx,caption,ragged2e}
\captionsetup{skip=0.333\baselineskip}
\usepackage{dcolumn}
\newcommand\hd[1]{\multicolumn{1}{c}{\begin{tabular}{@{}c@{}}#1\end{tabular}}}
\begin{document}
\begin{table}[htbp]
\centering
\footnotesize
\setlength\tabcolsep{1pt} % default: 6pt
\caption{} \label{}
\begin{tabular}{@{}r>{\raggedright}p{1.4cm} *{6}{D..{2.9}}@{}}
\toprule
&
\hd{Test} &
\hd{Trust}&
\hd{Demo-\\graphics} &
\hd{Immi-\\gration}&
\hd{Economic\\Model}&
\hd{Issue\\Satisfaction}&
\hd{Demo-\\graphic\\No\\Country}\\
\hline
1 & R2 & 0.016 & 0.2 & 0.086 & 0.007 & 0.021 & 0.027 \\
2 & Predicted & 83.20\% & 83.3\%\% & 83.40\% & 82.7\%\% & 83.20\% & 84.50\% \\
3 & AUC\\ Index & 0.585 & 0.79 & 0.701 & 0.559 & 0.603 & 0.61 \\
4 & Sum of\\ squared errors & 4861.786003 & 4773.002716 & 1847.897222 & 1249.544564 & 5009.179836 & 5310.168732 \\
5 & Expec-\\ted value$|$H0 & 4862.80413 & 4777.131906 & 1835.302972 & 1249.164639 & 5003.713449 & 5311.013662 \\
6 & SD & 0.545436987 & 3.006760697 & 1.739686944 & 0.10557875 & 0.673923831 & 0.953354286 \\
7 & Z & -1.866627646 & -1.373301832 & 7.239377177 & 3.598496041 & 8.111283371 & -0.886270167 \\
8 & P & 0.061953604 & 0.169658533 & 4.51E-13 & 0.000320063 & 5.01E-16 & 0.375471961 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
我通过以下更改来测试您的表格:
- 字体大小:
\small
- 表环境
tabularx
- 使用
S
列的类型为数字 - 减少
\tabcolsep
到零 - 考虑大卫·卡莱尔和米科评论(在他们的答案中)
结果是:
\documentclass{article}
\usepackage[showframe,
left=1cm,right=1cm]{geometry}
\usepackage[english]{babel}
\usepackage{booktabs, makecell, tabularx}
\renewcommand\theadfont{\small}
\usepackage{caption}
\usepackage{ragged2e}
\usepackage{siunitx}
\captionsetup{skip=0.333\baselineskip}
\newcolumntype{L}{>{\raggedright}X}
\begin{document}
\begin{table}%[!htbp]
\small
\setlength\tabcolsep{0pt} % default: 6pt
\caption{}
\label{tab:my problematic table}
\begin{tabularx}{\linewidth}{@{} l @{\ }
L @{\quad}
*{6}{S[table-format=4.6,
table-figures-exponent=1,
table-align-text-post=false,
table-align-exponent=false]}
@{} }
\toprule
& Test & Trust & {Demographics} & {Immigration}
& {\thead{Economic\\ Model}} & {\thead{Issue\\ Satisfaction}}
& {\thead{Demographic\\ No Country}} \\
\midrule
1 & R2 & 0.016 & 0.2 & 0.086 & 0.007 & 0.021 & 0.027 \\
2 & Predicted & 83.20\% & 83.3\%\% & 83.40\% & 82.7\%\% & 83.20\% & 84.50\% \\
3 & AUC Index & 0.585 & 0.79 & 0.701 & 0.559 & 0.603 & 0.61 \\
4 & Sum of squared errors
& 4861.786003 & 4773.002716 & 1847.897222 & 1249.544564 & 5009.179836 & 5310.168732 \\
5 & Expected value$|$H0
& 4862.80413 & 4777.131906 & 1835.302972 & 1249.164639 & 5003.713449 & 5311.013662 \\
6 & SD & 0.545436987 & 3.006760697 & 1.739686944 & 0.10557875 & 0.673923831 & 0.953354286 \\
7 & Z & -1.866627646 & -1.373301832 & 7.239377177 & 3.598496041 & 8.111283371 & -0.886270167\\
8 & P & 0.061953604 & 0.169658533 & 4.51e-13 & 0.000320063 & 5.01e-16 & 0.375471961 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
答案3
\documentclass{article}
\usepackage[left=1cm,right=1cm]{geometry}
\usepackage[english]{babel}
\usepackage{booktabs,tabularx,caption,ragged2e,longtable}
\captionsetup{skip=0.333\baselineskip}
\newcolumntype{L}{>{\hsize=7.5\hsize}X}
\newlength\mylength
\settowidth\mylength{Multicultu} % width of 2nd col.
\begin{document}
\begin{table}[!htbp]
\setlength\tabcolsep{2pt} % default: 6pt
\caption{} \label{}
\begin{flushleft}
\begin{longtable}[H]{clcccccc}
\toprule
& Test & Trust & Demographics & Immigration & Economic Model & Issue Satisfaction & Demographic No Country \\
\midrule
1 & R2 & 0.016 & 0.2 & 0.086 & 0.007 & 0.021 & 0.027 \\
2 & Predicted & 83.20\% & 83.3\%\% & 83.40\% & 82.7\%\% & 83.20\% & 84.50\% \\
3 & AUC Index & 0.585 & 0.79 & 0.701 & 0.559 & 0.603 & 0.61 \\
4 & Sum of squared errors & 4861.786003 & 4773.002716 & 1847.897222 & 1249.544564 & 5009.179836 & 5310.168732 \\
5 & Expected value$|$H0 & 4862.80413 & 4777.131906 & 1835.302972 & 1249.164639 & 5003.713449 & 5311.013662 \\
6 & SD & 0.545436987 & 3.006760697 & 1.739686944 & 0.10557875 & 0.673923831 & 0.953354286 \\
7 & Z & -1.866627646 & -1.373301832 & 7.239377177 & 3.598496041 & 8.111283371 & -0.886270167 \\
8 & P & 0.061953604 & 0.169658533 & 4.51E-13 & 0.000320063 & 5.01E-16 & 0.375471961 \\
\bottomrule
\end{longtable}
\end{flushleft}
\end{table}
\end{document}
答案4
您的桌子遇到了一些严重的问题。
请问,您在做什么,为什么小数点后显示 8 或 9 位数字?三位数应该足够了。要么手动执行舍入,要么加载包
siunitx
并让包的S
列类型为您执行舍入。这
>{\hsize=7.5\hsize}X
到底是什么意思呢?我看不出有什么理由不让六个(而不是五个)数据列的宽度相等。您有几个例子
\%\%
——它看起来很乱。flushleft
不需要使用环境。不要使用它。
在下表中,请注意六个数据列的主列类型为S
,以便将数字对齐到各自的小数点标记上并对数字进行舍入,如上所述。但是,六个标题单元格被包裹在\mC
指令中,以确保环境的整体宽度tabularx
为\textwidth
。观察标题中某些(但不是全部)文本的文本如何占据两行。