这可能是我正在努力解决的一个非常基本的问题 - 我正在尝试创建一个简单的表格(从 Stata 输出),但最后一列被反复推到下一行 - 删除最后一列是唯一不被推到下一行的方法。这是表格:
Variables & \shortstack{No\\Preference} & \shortstack{Ask\\ORS} & \shortstack{Ask\\antibiotics} & \shortstack{Purchase\\elsewhere} & P-Value \\
Age & 43.1 & 43.2 & 44.8 & 43.1 & 0.410 \\
Male & 0.833 & 0.818 & 0.864 & 0.833 & 0.551 \\
Provider has MBBS & 0.271 & 0.312 & 0.313 & 0.271 & 0.654 \\
Patients per day & 35.2 & 34.0 & 35.1 & 35.2 & 0.349 \\
Diarrhea case per week & 8.48 & 7.49 & 8.46 & 8.48 & 0.570 \\
Years of experience & 16.7 & 17.4 & 18.2 & 16.7 & 0.396 \\
Works at other facility & 0.034 & 0.034 & 0.041 & 0.034 & 0.670 \\
Beds & 2.10 & 2.25 & 2.23 & 2.10 & 0.398 \\
Dispenses medications & 0.461 & 0.378 & 0.388 & 0.461 & 0.059 \\
ORS available at baseline & 0.572 & 0.545 & 0.564 & 0.572 & 0.945 \\
Correct treatment & 0.887 & 0.899 & 0.877 & 0.887 & 0.758 \\
Has electricity & 0.996 & 0.988 & 0.992 & 0.996 & 0.340 \\
Consultation Fee & 79.0 & 84.3 & 86.1 & 79.0 & 0.526 \\
Fee for ORS (if available) & 13.9 & 14.3 & 16.2 & 13.9 & 0.536 \\
这是我用来将其插入到文档中的代码:
\begin{table}[!htbp]
\resizebox{\textwidth}{!}{\begin{tabular}{lcccc}
\toprule
\input{Tables/table1.tex}
\\ \bottomrule % 08 Dec 2021. Add these slashes.
\addlinespace[.2ex]
\end{tabular}}
\end{table}
最终输出结果如下:
我怎样才能将所有列放在一行?谢谢!
答案1
两个例子可以说明如何编写表格:
- 使用
siunitx
包,定义S
数字在小数点对齐的列类型 - 使用
c
列,此处数字居中
两种情况下均使用tabular*
table 环境,列之间的距离由宏 确定extracolsep
。使用它,就\resizebox
不再需要了:
\documentclass{article}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%
\usepackage{lipsum} % for dummy text
%---------------------------------------------------------------%
\usepackage{booktabs, makecell}
\usepackage{siunitx}
\begin{document}
\begin{table}[!ht]
\centering
\setlength\tabcolsep{0pt}
\sisetup{table-format=2.3}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}} l *{5}{S} }
\toprule
Variables & {\makecell{No\\Preference}}
& {\makecell{Ask\\ORS}}
& {\makecell{Ask\\antibiotics}}
& {\makecell{Purchase\\elsewhere}}
& {P-Value} \\
\midrule
Age & 43.1 & 43.2 & 44.8 & 43.1 & 0.410 \\
Male & 0.833 & 0.818 & 0.864 & 0.833 & 0.551 \\
Provider has MBBS
& 0.271 & 0.312 & 0.313 & 0.271 & 0.654 \\
Patients per day
& 35.2 & 34.0 & 35.1 & 35.2 & 0.349 \\
Diarrhea case per week
& 8.48 & 7.49 & 8.46 & 8.48 & 0.570 \\
Years of experience
& 16.7 & 17.4 & 18.2 & 16.7 & 0.396 \\
Works at other facility
& 0.034 & 0.034 & 0.041 & 0.034 & 0.670 \\
Beds & 2.10 & 2.25 & 2.23 & 2.10 & 0.398 \\
Dispenses medications
& 0.461 & 0.378 & 0.388 & 0.461 & 0.059 \\
ORS available at baseline
& 0.572 & 0.545 & 0.564 & 0.572 & 0.945 \\
Correct treatment
& 0.887 & 0.899 & 0.877 & 0.887 & 0.758 \\
Has electricity
& 0.996 & 0.988 & 0.992 & 0.996 & 0.340 \\
Consultation Fee
& 79.0 & 84.3 & 86.1 & 79.0 & 0.526 \\
Fee for ORS (if available)
& 13.9 & 14.3 & 16.2 & 13.9 & 0.536 \\
\bottomrule
\end{tabular*}
\end{table}
\begin{table}[!ht]
\centering
\setlength\tabcolsep{0pt}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}} l *{5}{c} }
\toprule
Variables & \makecell{No\\Preference}
& \makecell{Ask\\ORS}
& \makecell{Ask\\antibiotics}
& \makecell{Purchase\\elsewhere}
& P-Value \\
\midrule
Age & 43.1 & 43.2 & 44.8 & 43.1 & 0.410 \\
Male & 0.833 & 0.818 & 0.864 & 0.833 & 0.551 \\
Provider has MBBS
& 0.271 & 0.312 & 0.313 & 0.271 & 0.654 \\
Patients per day
& 35.2 & 34.0 & 35.1 & 35.2 & 0.349 \\
Diarrhea case per week
& 8.48 & 7.49 & 8.46 & 8.48 & 0.570 \\
Years of experience
& 16.7 & 17.4 & 18.2 & 16.7 & 0.396 \\
Works at other facility
& 0.034 & 0.034 & 0.041 & 0.034 & 0.670 \\
Beds & 2.10 & 2.25 & 2.23 & 2.10 & 0.398 \\
Dispenses medications
& 0.461 & 0.378 & 0.388 & 0.461 & 0.059 \\
ORS available at baseline
& 0.572 & 0.545 & 0.564 & 0.572 & 0.945 \\
Correct treatment
& 0.887 & 0.899 & 0.877 & 0.887 & 0.758 \\
Has electricity
& 0.996 & 0.988 & 0.992 & 0.996 & 0.340 \\
Consultation Fee
& 79.0 & 84.3 & 86.1 & 79.0 & 0.526 \\
Fee for ORS (if available)
& 13.9 & 14.3 & 16.2 & 13.9 & 0.536 \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}
(红线显示页面布局)