答案1
示例 1
这是您附上的图片中的表格。我已将两个表格设置在同一个表格环境中,以将它们放在一起。
在我看来,他们并不专业,只是在 Word 或 Excel 中拼凑一些东西的模仿者。例如,第一个表格中的蓝色行表示该行是标题,但显然不是。“标题”是第 1 列和第 3 列,第 2 列和第 4 列是数据。
第二个表格中的交替阴影也是错误的。第一行和第一列是“标题”,但由于阴影,该效果消失了。
我已更新代码,以便两个表格至少具有相等的宽度(相对于线宽计算)。我还做了一些其他调整以增加一致性:
\documentclass{article}
\usepackage{array}
\usepackage{lmodern}
\usepackage[svgnames, table]{xcolor}
\newcolumntype{B}{>{\bfseries}wr{\dimexpr((\linewidth/4))}}
\newcolumntype{R}{>{$}wr{\dimexpr((\linewidth/4)-4\tabcolsep)}<{$}}
\newcolumntype{b}{>{\bfseries}wr{\dimexpr((\linewidth/7)-2\tabcolsep)}}
\newcolumntype{s}{>{$}wr{\dimexpr((\linewidth/7)-2\tabcolsep)}<{$}}
\setlength{\extrarowheight}{2pt}
\renewcommand{\arraystretch}{1.2}
\setlength{\tabcolsep}{4pt}
\newcommand{\thead}[1]{\multicolumn{1}{r}{\bfseries #1}}
\newcommand{\pz}{\boldmath{$<|z|$}}
\begin{document}
\begin{table}
\small
\rowcolors{3}{white}{lightgray!25}
\begin{tabular}{Bwr{\dimexpr((\linewidth/4)-4\tabcolsep)}BR}
\multicolumn{4}{@{}l}
{\textcolor{darkgray}{\bfseries Probit Regression Results}}\\
\rowcolor{LightBlue}Dep.\,Variable: & spi1 & No.\,Observations: & 254 \\
Model: & Probit & Df Residuals: & 248 \\
Methods: & MLE & Df Model: & 5 \\
Date: & Thu, 16 Apr 2020 & Pseudo R-squ.: & 0.1311 \\
Time: & 00:35:44 & Log-Likelihood: & -149.65 \\
converge: & True & LL.Null: & -172.23 \\
Covariance Type: & nonrobust & LLR p-value & 1.341\mbox{e-}08 \\
\end{tabular}
\bigskip
\rowcolors{1}{lightgray!25}{white}
\begin{tabular}{b*{6}{s}}
& \thead{coef} & \thead{std err} & \thead{z} & \thead{P\pz} & \thead{[0.025} & \thead{0.975]} \\
const & -5.1870 & 1.270 & -4.083 & 0.000 & -7.677 & -2.697 \\
vol & -10.0795 & 18.930 & -0.532 & 0.594 & -47.182 & 27.023 \\
rec & -0.8608 & 0.394 & -2.183 & 0.029 & -1.634 & -0.088 \\
spread & 0.3881 & 0.087 & 4.483 & 0.000 & 0.218 & 0.558 \\
DP & 11.3808 & 2.526 & 4.505 & 0.000 & 6.429 & 16.332 \\
EP & -1.3759 & 1.721 & -0.799 & 0.424 & -4.750 & 1.998 \\
\end{tabular}
\end{table}
\end{document}
示例 2
在这里,我建议一种更传统的布局,您可以考虑删除第一个表格中的水平规则,并用一些空白替换该规则:
\documentclass{article}
\usepackage{array}
\usepackage{lmodern}
\usepackage[svgnames, table]{xcolor}
\newcolumntype{A}{>{\itshape}wl{\dimexpr((\linewidth/4))}}
\newcolumntype{Q}{>{$}wr{\dimexpr((\linewidth/4)-4\tabcolsep)}<{$}}
\newcolumntype{a}{>{\itshape}wr{\dimexpr((\linewidth/7)-2\tabcolsep)}}
\newcolumntype{q}{>{$}wr{\dimexpr((\linewidth/7)-2\tabcolsep)}<{$}}
\newcommand{\thead}[1]{\multicolumn{1}{r}{\itshape #1}}
\newcommand{\pz}{$<|z|$}
\arrayrulecolor{lightgray}
\setlength{\extrarowheight}{2pt}
\renewcommand{\arraystretch}{1.2}
\begin{document}
\begin{table}
\small
\setlength{\tabcolsep}{10pt}
\begin{tabular}{@{}Awr{\dimexpr((\linewidth/4)-4\tabcolsep)}|AQ@{}}
\multicolumn{4}{@{}l}
{\textcolor{darkgray}{\bfseries Probit Regression Results}}\\ \hline
Dep.\,Variable & spi1 & No.\,Observations & 254 \\
Model & Probit & Df Residuals & 248 \\
Methods & MLE & Df Model & 5 \\
Date & Thu, 16 Apr 2020 & Pseudo R-squ. & 0.1311 \\
Time & 00:35:44 & Log-Likelihood & -149.65 \\
Converge & True & LL.Null & -172.23 \\
Covariance Type & nonrobust & LLR p-value & 1.341\mbox{e-}08 \\
\end{tabular}
\bigskip
\bigskip
\setlength{\tabcolsep}{6pt}
\begin{tabular}{@{}a|*{6}{q}@{}}
& \thead{coef} & \thead{std err} & \thead{z} & \thead{P\pz} & \thead{[0.025} & \emph{0.975]} \\ \hline
const & -5.1870 & 1.270 & -4.083 & 0.000 & -7.677 & -2.697 \\
vol & -10.0795 & 18.930 & -0.532 & 0.594 & -47.182 & 27.023 \\
rec & -0.8608 & 0.394 & -2.183 & 0.029 & -1.634 & -0.088 \\
spread & 0.3881 & 0.087 & 4.483 & 0.000 & 0.218 & 0.558 \\
DP & 11.3808 & 2.526 & 4.505 & 0.000 & 6.429 & 16.332 \\
EP & -1.3759 & 1.721 & -0.799 & 0.424 & -4.750 & 1.998 \\
\end{tabular}
\end{table}
\end{document}
答案2
我的建议是使宽度相等。交替使用颜色可能看起来很吸引人,但实际上并没有帮助。(数据取自其他答案。)
\documentclass{article}
\usepackage{booktabs,siunitx}
\newsavebox{\toptabular}
\begin{document}
\begin{table}
\centering
\sbox{\toptabular}{%
\begin{tabular}{ @{} l r @{\hspace{3em}} l r @{} }
\toprule
\multicolumn{4}{@{}l}{\bfseries Probit Regression Results} \\
\midrule
Dep.\@ Variable & spi1 & No.\@ Observations & \num{254} \\
Model & Probit & Df Residuals & \num{248} \\
Methods & MLE & Df Model & \num{5} \\
Date & Thu, 16 Apr 2020 & Pseudo R-squ. & \num{0.1311} \\
Time & 00:35:44 & Log-Likelihood & \num{-149.65} \\
Converge & True & LL-Null & \num{-172.23} \\
Covariance Type & nonrobust & LLR p-value & \num{1.341e-08} \\
\bottomrule
\end{tabular}%
}
\usebox{\toptabular}
\bigskip
\begin{tabular*}{\wd\toptabular}{
@{\extracolsep{\fill}}
l
S[table-format=-2.4]
S[table-format=2.3]
S[table-format=-1.3]
S[table-format=1.3]
S[table-format=-2.3,table-align-text-pre=false,table-space-text-pre={[}]
S[table-format=-1.3,table-space-text-post={]}]% the minus also covers the second digit
@{}
}
\toprule
& {coef} & {std err} & {$z$} & {$P>|z|$} & [0.025 & 0.975] \\
\midrule
const & -5.1870 & 1.270 & -4.083 & 0.000 & -7.677 & -2.697 \\
vol & -10.0795 & 18.930 & -0.532 & 0.594 & -47.182 & 27.023 \\
rec & -0.8608 & 0.394 & -2.183 & 0.029 & -1.634 & -0.088 \\
spread & 0.3881 & 0.087 & 4.483 & 0.000 & 0.218 & 0.558 \\
DP & 11.3808 & 2.526 & 4.505 & 0.000 & 6.429 & 16.332 \\
EP & -1.3759 & 1.721 & -0.799 & 0.424 & -4.750 & 1.998 \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}
答案3
您正在寻找这样的东西吗?
\documentclass[a4paper,12pt]{article}
\usepackage{fontspec}
\setmainfont{Calibri}
\begin{document}
\begin{tabular}{r r r r}
\textbf{Dep. Veriable:} & spi\textbf{1} & \textbf{No. Observation:} & 254\\
\textbf{Model:} & Probit & \textbf{Df Residual:} & 248\\
\textbf{Method:} & MLE & \textbf{Df Model:} & 5\\
\textbf{Date:} & Thu, 16 Apr 2020 & \textbf{Pseudo R-squ:} & 0.1311\\
\textbf{Time:} & 00:35:44 & \textbf{Log-Likelihood:} & -149.65\\
\textbf{Converged:} & True & \textbf{LL-Null:} & -172.23\\
\textbf{Convariance Type:} & nonorobust & \textbf{LLR p-value:} & 1.31e-08
\end{tabular}
\end{document}
答案4
我修改了 Sveinung 的回答使用booktabs
和siunitx
。首先,布局:
\documentclass{article}
\usepackage{array}
\usepackage{lmodern}
\usepackage[svgnames, table]{xcolor}
\newcolumntype{B}{>{\bfseries}r}
\newcolumntype{R}{>{$}r<{$}}
\setlength{\extrarowheight}{2pt}
\renewcommand{\arraystretch}{1.2}
\usepackage{booktabs}
\usepackage{siunitx}
\newcommand{\thead}[1]{\multicolumn{1}{r}{\bfseries #1}}
\newcommand{\pz}{\boldmath{$<|z|$}}
\begin{document}
\centering
\begin{table}
\begin{tabular}{BrBR}
\multicolumn{4}{c}{Probit Regression Results}\\
\toprule
Dep. Variable: & spi1 & No. Observations: & \tablenum[table-figures-integer = 3]{254} \\
Model: & Probit & Df Residuals: & \tablenum[table-figures-integer = 3]{248} \\
Methods: & MLE & Df Model: & \tablenum[table-figures-integer = 3]{5} \\
Date: & Thu, 16 Apr 2020 & Pseudo R-squ.: & \tablenum[table-figures-integer = 3]{0.1311} \\
Time: & 00:35:44 & Log-Likelihood: & \tablenum[table-figures-integer = 3]{-149.65} \\
converge: & True & LL.Null: & \tablenum[table-figures-integer = 3]{-172.23} \\
Covariance Type: & nonrobust & LLR p-value & \tablenum[table-figures-integer = 3]{1.341e-08} \\
\bottomrule
\end{tabular}
\bigskip
\begin{tabular}{lSSSSSS}
\toprule
& \multicolumn{1}{c}{coef} & \multicolumn{1}{c}{std err} & \multicolumn{1}{c}{z} & \multicolumn{1}{c}{P\pz} & \multicolumn{1}{c}{[0.025} & \multicolumn{1}{c}{0.975]} \\
\midrule
const & -5.1870 & 1.270 & -4.083 & 0.000 & -7.677 & -2.697 \\
vol & -10.0795 & 18.930 & -0.532 & 0.594 & -47.182 & 27.023 \\
rec & -0.8608 & 0.394 & -2.183 & 0.029 & -1.634 & -0.088 \\
spread & 0.3881 & 0.087 & 4.483 & 0.000 & 0.218 & 0.558 \\
DP & 11.3808 & 2.526 & 4.505 & 0.000 & 6.429 & 16.332 \\
EP & -1.3759 & 1.721 & -0.799 & 0.424 & -4.750 & 1.998 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
第一个表格显示你可以集成siunitx
到任意表格中,第二个表格显示你可以使用siunitx
自己的列类型,以便更好、更轻松地排版。我也支持这个想法产生以编程方式创建基本表格是一个很好的开始,但您可能想要更多样式设置,这些样式设置大多是手动完成的,除非您有数百个这样的表格。
现在,让我们添加颜色,即使它看起来不那么专业:
\documentclass{article}
\usepackage{array}
\usepackage{lmodern}
\usepackage[svgnames, table]{xcolor}
\newcolumntype{B}{>{\bfseries}r}
\newcolumntype{R}{>{$}r<{$}}
\setlength{\extrarowheight}{2pt}
\renewcommand{\arraystretch}{1.2}
\usepackage{booktabs}
\usepackage{siunitx}
\newcommand{\thead}[1]{\multicolumn{1}{r}{\bfseries #1}}
\newcommand{\pz}{\boldmath{$<|z|$}}
\begin{document}
\centering
\begin{table}
\begin{tabular}{BrBR}
\multicolumn{4}{c}{Probit Regression Results}\\
\toprule
Dep. Variable: & spi1 & No. Observations: & \tablenum[table-figures-integer = 3]{254} \\
Model: & Probit & Df Residuals: & \tablenum[table-figures-integer = 3]{248} \\
Methods: & MLE & Df Model: & \tablenum[color=red,table-figures-integer = 3]{5} \\
Date: & Thu, 16 Apr 2020 & Pseudo R-squ.: & \tablenum[table-figures-integer = 3]{0.1311} \\
Time: & 00:35:44 & Log-Likelihood: & \tablenum[color=green,table-figures-integer = 3]{-149.65} \\
converge: & True & LL.Null: & \tablenum[negative-color = red,table-figures-integer = 3]{-172.23} \\
Covariance Type: & nonrobust & LLR p-value & \tablenum[negative-color = red,table-figures-integer = 3]{1.341e-08} \\
\bottomrule
\end{tabular}
\bigskip
\begin{tabular}{lS[color=purple]SS[color=orange]SS[color=blue]S}
\toprule
& \multicolumn{1}{c}{coef} & \multicolumn{1}{c}{std err} & \multicolumn{1}{c}{z} & \multicolumn{1}{c}{P\pz} & \multicolumn{1}{c}{[0.025} & \multicolumn{1}{c}{0.975]} \\
\midrule
const & -5.1870 & 1.270 & -4.083 & 0.000 & -7.677 & -2.697 \\
vol & -10.0795 & 18.930 & -0.532 & 0.594 & -47.182 & 27.023 \\
rec & -0.8608 & 0.394 & -2.183 & 0.029 & -1.634 & -0.088 \\
spread & 0.3881 & 0.087 & 4.483 & 0.000 & 0.218 & 0.558 \\
DP & 11.3808 & 2.526 & 4.505 & 0.000 & 6.429 & 16.332 \\
EP & -1.3759 & 1.721 & -0.799 & 0.424 & -4.750 & 1.998 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
显然,可以为每一列“全局”指定颜色,也可以单独指定,或者基于一些条件指定siunitx
。