我正在努力解决表格中的换行符和水平和垂直居中问题。我试图让表 1 中的“合成控制组样本 A”和表 2 中的“对数消费者价格指数”自动换行(请参阅下面的 MWE)。我还想让行垂直居中,以防出现换行符。您有什么建议,让我可以自动为长表达式换行,使其看起来更好?您有什么想法,让我可以垂直居中单行?请在下面找到我的代码。非常感谢您抽出时间和帮助!
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage{array}
\usepackage{booktabs}
\usepackage{tabulary}
\usepackage{multirow}
\usepackage{dcolumn}
\usepackage{tabularht}
\begin{document}
\begin{table}[h]
\centering{
\caption{Composition of Synthetic Control Groups}
}
\begin{tabular}{rrlr}
\toprule
\multicolumn{2}{p{14.925em}}{\textbf{Synthetic Control Group Sample A}} & \multicolumn{2}{p{14.925em}}{\textbf{Synthetic Control Group Sample B}} \\
\midrule
\midrule
\multicolumn{1}{l}{\textbf{State}} & \multicolumn{1}{l}{\textbf{Percent}} & \textbf{State} & \multicolumn{1}{l}{\textbf{Percent}} \\
\midrule
\multicolumn{1}{l}{A} & 53.0\% & A & 9.5\% \\
\multicolumn{1}{l}{B} & 12.2\% & B & 19.7\% \\
\multicolumn{1}{l}{C} & 34.8\% & C & 5.8\% \\
& & D & 58.7\% \\
& & E & 6.2\% \\
\bottomrule
\end{tabular}%
\end{table}
\begin{table}[h]
\centering{
\caption{Descriptive Statistics}
}
\begin{tabular}{lrrrrrr}
\toprule
\textbf{Variables} & \multicolumn{1}{c}{\textbf{N}} & \multicolumn{1}{c}{\textbf{Mean}} & \multicolumn{1}{c}{\textbf{Median}} & \multicolumn{1}{c}{\textbf{Std. Dev.}} & \multicolumn{1}{c}{\textbf{Min}} & \multicolumn{1}{c}{\textbf{Max}} \\
\midrule
\midrule
Logarithm debt pc & 90 & 8.584028 & 8.779948 & 0.6039792 & 6.947824 & 9.336767 \\
Logarithm GDP pc & 90 & 10.03278 & 10.15797 & 0.6830409 & 7.906916 & 10.77783 \\
Unemployment rate & 90 & 0.0883556 & 0.077 & 0.0484822 & 0.029 & 0.204 \\
Logarithm consumer price index & 90 & 4.549728 & 4.555454 & 0.0623881 & 4.430817 & 4.646312 \\
\bottomrule
\end{tabular}%
\end{table}
\end{document}
答案1
欢迎来到 TeX.SE!
使用tabularray
包装很简单:
\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}
\usepackage[skip=0.33\baselineskip]{caption}
\begin{document}
\begin{table}[ht]
\centering
\caption{Composition of Synthetic Control Groups}
\sisetup{table-format=2.1{\%}}
\begin{tblr}{width=0.7\linewidth,
colspec = {X[c] X[c, si] X[c] X[c, si]},
row{1,2} = {font=\bfseries},
hspan = minimal
}
\toprule
\SetCell[c=2]{c} Synthetic Control Group Sample A
& & \SetCell[c=2]{c} Synthetic Control Group Sample B
& \\
\midrule
State & {{{Percent}}}
& State & {{{Percent}}} \\
\midrule
A & 53.0\,\% & A & 9.5\,\% \\
B & 12.2\,\% & B & 19.7\,\% \\
C & 34.8\,\% & C & 5.8\,\% \\
& & D & 58.7\,\% \\
& & E & 6.2\,\% \\
\bottomrule
\end{tblr}%
\end{table}
\begin{table}[ht]
\centering
\caption{Descriptive Statistics}
\sisetup{round-mode=places,
round-precision=4,
table-format=2.4}
\begin{tblr}{colsep=4pt,
colspec = {@{} X[2,l,m] c *{5}{X[0.8, c, si]} @{}},
row{1} = {font=\bfseries, m},
}
\toprule
Variables & N & {{{Mean}}} & {{{Median}}} & {{{Std. Dev.}}}
& {{{Min}}} & {{{Max}}} \\
\midrule
Logarithm debt pc
& 90 & 8.584028 & 8.779948 & 0.6039792
& 6.947824 & 9.336767 \\
Logarithm GDP pc
& 90 & 10.03278 & 10.15797 & 0.6830409
& 7.906916 & 10.77783 \\
Unemployment rate
& 90 & 0.0883556 & 0.077 & 0.0484822
& 0.029 & 0.204 \\
Logarithm consumer price index
& 90 & 4.549728 & 4.555454 & 0.0623881
& 4.430817 & 4.646312 \\
\bottomrule
\end{tblr}
\end{table}
\end{document}
由于第二张表格比较宽,小数位数也不同,所以数字被四舍五入到小数点后 5 位。这样这张表格就完全适合页面了。