我在使用乳胶创建的表格中遇到了问题。
问题是列中的值未正确居中。前面带有减号的值比其他值更靠右。是否可以将它们围绕小数点居中,以便看起来更整洁?
答案1
您已经在使用具有该功能的一个包。siunitx
引入列说明符S
,它将列与小数点对齐,并将数字格式化为您的\sisetup
设置。
我进一步优化了你的代码:
\documentclass{article}
\usepackage{amsmath}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage{siunitx}
\newcommand{\nm}[1]{\textnormal{#1}}
\begin{document}
\begin{landscape}
\begin{table}[h!]
\centering
\begin{tabular}{lSSSSSSSS}
\toprule
\multicolumn{1}{l}{Model} &\multicolumn{4}{r}{Model1} &\multicolumn{2}{r}{Model2} &\multicolumn{2}{r}{Model3} \\
\cmidrule(lr){2-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9}
& {$n=1$} & {$n=2$} & {$n=3$} & {$n=4$} & {$n=5$} & {$n=6$} & {$n=7$} & {$n=8$} \\
\midrule
Mean(alg)$^*$ & 1.281 & 1.031 & 0.779 & 0.631 & 0.886 & 0.727 & 1.324 & 1.3145 \\
Median(alg) & 0.281 & 0.223 & 0.214 & 0.192 & 0.177 & 0.162 & 0.242 & 0.228 \\
Mean(length) in $\rho$ & 11 & 91 & 327 & 489 & 76 & 64 & 86 & 90 \\
Median(length) in $\rho$ & 5.3 & 39 & 210 & 400 & 37 & 32 & 31.7 & 35 \\
Cor(alg,deh) & -0.332 & -0.30 & -0.31 & -0.25 & -0.28 & -0.31 & -0.20 & -0.24 \\
\midrule
$^*$ \\
\bottomrule
\end{tabular}
\caption{Mean}
\label{beta}
\end{table}
\end{landscape}
\end{document}
选择
现在每个单元格都左对齐,但您必须用 替换所有-
内容\mi
。
\documentclass{article}
\usepackage{mathtools}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{array}
\newcolumntype{L}{>{\phantom{$\mathbin{-}$}$}l<{$}}
\newcommand{\mi}{\mathllap{\mathbin{-}}}
\newcommand{\nm}[1]{\textnormal{#1}}
\begin{document}
\begin{landscape}
\begin{table}[h!]
\centering
\begin{tabular}{lLLLLLLLL}
\toprule
\multicolumn{1}{l}{Model} &\multicolumn{4}{r}{Model1} &\multicolumn{2}{r}{Model2} &\multicolumn{2}{r}{Model3} \\
\cmidrule(lr){2-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9}
& n=1 & n=2 & n=3 & n=4 & n=5 & n=6 & n=7 & n=8 \\
\midrule
Mean(alg)$^*$ & 1.281 & 1.031 & 0.779 & 0.631 & 0.886 & 0.727 & 1.324 & 1.3145 \\
Median(alg) & 0.281 & 0.223 & 0.214 & 0.192 & 0.177 & 0.162 & 0.242 & 0.228 \\
Mean(length) in $\rho$ & 11 & 91 & 327 & 489 & 76 & 64 & 86 & 90 \\
Median(length) in $\rho$ & 5.3 & 39 & 210 & 400 & 37 & 32 & 31.7 & 35 \\
Cor(alg,deh) & \mi0.332 & \mi0.30 & \mi0.31 & \mi0.25 & \mi0.28 & \mi0.31 & \mi0.20 & \mi0.24 \\
\midrule
$^*$ \\
\bottomrule
\end{tabular}
\caption{Mean}
\label{beta}
\end{table}
\end{landscape}
\end{document}
答案2
是的。dcolumn 包就是为此而设计的。请参阅此处的简短易懂的文档(2 页):http://www.ctan.org/pkg/dcolumn
答案3
这是使用该包的解决方案。除了对 8 列dcolumn
使用带数字的列类型外,我还建议对第一列使用。d
l
\documentclass{article}
\usepackage[margin=1in]{geometry} % choose margins to suit your needs
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D..{#1}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % a handy shortcut macro
\begin{document}
%\begin{landscape} % not needed for this example
\begin{table} [h!]
\centering
\begin{tabular}{ l *{2}{d{2.3}} *{2}{d{3.3}} *{3}{d{2.3}} d{2.4} }
\toprule
& \multicolumn{4}{c}{Model 1}
& \multicolumn{2}{c}{Model 2}
& \multicolumn{2}{c}{Model 3} \\
\cmidrule(lr){2-5} \cmidrule(lr){6-7} \cmidrule(l){8-9}
& \mc{$n=1$} & \mc{$n=2$} & \mc{$n=3$} & \mc{$n=4$}
& \mc{$n=5$} & \mc{$n=6$} & \mc{$n=7$} & \mc{$n=8$} \\
\midrule
Mean(alg)$^*$ & 1.281 & 1.031 & 0.779& 0.631 &0.886& 0.727& 1.324&1.3145\\
Median(alg) &0.281 & 0.223 & 0.214 & 0.192& 0.177&0.162 & 0.242&0.228\\
Mean(length) in $\rho$ &11 & 91 & 327 & 489 &76&64&86&90\\
Median(length) in $\rho$ &5.3 & 39 & 210 & 400&37&32&31.7&35 \\
Cor(alg,deh) &-0.332 & -0.30 & -0.31 & -0.25 &-0.28&-0.31 & -0.20&-0.24\\
\midrule[\heavyrulewidth]
\multicolumn{9}{l}{$^*$ \footnotesize Some explanations} \\
\bottomrule
\end{tabular}
\caption{Mean} \label{beta}
\end{table}
%\end{landscape}
\end{document}