修复表格中第一行和最后一行按小数点对齐的问题

修复表格中第一行和最后一行按小数点对齐的问题

我正在尝试对齐表格中的第一行和最后一行,其余数据都是数字,并按小数位对齐。我找到了一种解决这个问题的办法,但正在寻找一种更好的方法来解决这个问题,任何帮助都将不胜感激!

当我按照下面的代码对数字进行对齐时,最后一行的标签和顶行的编号没有居中。

\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\begin{document}

\begin{center}
    \begin{table}
        \centering 
        {\footnotesize
            \begin{tabular}{r|d{2.4}|d{2.4}|d{2.4}|d{2.4}|d{2.4}|d{2.4}|c}
                \hline
                \hline
                sample \# & 1  & 2 & 3 & 4 & 5 & 6 &
                $\cdots$  \\ 
                \hline
                Variance (Wavelet Coef.) &  -1.3971 & 4.5459 &  3.8660
                & 3.4566
                & 0.3292 & 0.3901 & $\cdots$   
                \\
                Skewness (Wavelet Coef.)  & 3.3191 & 8.1674&-2.6383
                & 9.5228
                & -4.4552  & -0.1428 & $\cdots$ 
                \\
                Kurtosis (Wavelet Coef.)
                & -1.3927 & -2.4586 & 1.9242
                & -4.0112
                & 4.5718 & -0.0319 & $\cdots$ \\ 
                Entropy (Wavelet Coef.) & -1.9948 &-1.4621& 0.1065
                & -3.5944
                & -0.9888 &  0.3508 & $\cdots$ 
                \\ 
                Genuine or Forged & 1 & 0  & 0  & 0  & 0  & 1 & $\cdots$ \\
                \hline
                \hline
            \end{tabular}
        }
    \end{table}
\end{center}

\end{document}

在此处输入图片描述

(请注意,第一行和最后一行的数字(标题和标签)没有居中)

我可以尝试做一个非常好的黑客攻击,如下所示,我定义最小列宽以使第一行和最后一行居中,并定义一种新的列类型:

    \renewcommand{\arraystretch}{1.2}\newcolumntype{C}[1]{>{\centering\arraybackslash}p{1cm}|}  

我手动设置了列宽(1cm)并调整了小数点前后的位数(在表格的列定义中将其设置为 d{1.2})。

有一个更好的方法吗?

\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\begin{document}

\begin{center}
    \begin{table}
        \centering 
        \renewcommand{\arraystretch}{1.2}\newcolumntype{C}[1]{>{\centering\arraybackslash}p{1cm}|}
        {\footnotesize
            \begin{tabular}{r|d{1.2}|d{1.2}|d{1.2}|d{1.2}|d{1.2}|d{1.2}|c}
                \hline
                \hline
                sample \# & \multicolumn{1}{C|}{1}   & \multicolumn{1}{C|}{2} & \multicolumn{1}{C|}{3}  & \multicolumn{1}{C|}{4} & \multicolumn{1}{C|}{5}   & \multicolumn{1}{C|}{6} &
                $\cdots$  \\ 
                \hline
                Variance (Wavelet Coef.) &  -1.3971 & 4.5459 &  3.8660
                & 3.4566
                & 0.3292 & 0.3901 & $\cdots$   
                \\
                Skewness (Wavelet Coef.)  & 3.3191 & 8.1674&-2.6383
                & 9.5228
                & -4.4552  & -0.1428 & $\cdots$ 
                \\
                Kurtosis (Wavelet Coef.)
                & -1.3927 & -2.4586 & 1.9242
                & -4.0112
                & 4.5718 & -0.0319 & $\cdots$ \\ 
                Entropy (Wavelet Coef.) & -1.9948 &-1.4621& 0.1065
                & -3.5944
                & -0.9888 &  0.3508 & $\cdots$ 
                \\ 
                Genuine or Forged & \multicolumn{1}{C|}{1} & \multicolumn{1}{C|}{0}  & \multicolumn{1}{C|}{0}  & \multicolumn{1}{C|}{0}  & \multicolumn{1}{C|}{0}  & \multicolumn{1}{C|}{1}  & $\cdots$ \\
                \hline
                \hline
            \end{tabular}
        }   
    \end{table}
\end{center}

在此处输入图片描述

请注意,这种黑客手段很有帮助,第一行和最后一行是对齐的,但是有没有更好的方法来做到这一点?

答案1

一个简单的方法\multicolumn{1}{c|}{...}也可以用于在类型列中水平居中单元格d。在下面的 MWE 中,我还提供了一种使用 的替代方法siunitx

在第三个表中,我删除了垂直线并\hline用包中的水平线替换了命令booktabs,同时稍微减少了值以\tabcolsep确保表适合文本宽度。

在此处输入图片描述

\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}

\usepackage{siunitx}
\usepackage{booktabs}

\begin{document}

    \begin{table}
        \centering 
        {\footnotesize
            \begin{tabular}{r|d{2.4}|d{2.4}|d{2.4}|d{2.4}|d{2.4}|d{2.4}|c}
                \hline
                \hline
                sample \# & \multicolumn{1}{c|}{1}  & \multicolumn{1}{c|}{2} & \multicolumn{1}{c|}{3} & \multicolumn{1}{c|}{4} & \multicolumn{1}{c|}{5} & \multicolumn{1}{c|}{6} &
                $\cdots$  \\ 
                \hline
                Variance (Wavelet Coef.) &  -1.3971 & 4.5459 &  3.8660
                & 3.4566
                & 0.3292 & 0.3901 & $\cdots$   
                \\
                Skewness (Wavelet Coef.)  & 3.3191 & 8.1674&-2.6383
                & 9.5228
                & -4.4552  & -0.1428 & $\cdots$ 
                \\
                Kurtosis (Wavelet Coef.)
                & -1.3927 & -2.4586 & 1.9242
                & -4.0112
                & 4.5718 & -0.0319 & $\cdots$ \\ 
                Entropy (Wavelet Coef.) & -1.9948 &-1.4621& 0.1065
                & -3.5944
                & -0.9888 &  0.3508 & $\cdots$ 
                \\ 
                Genuine or Forged & \multicolumn{1}{c|}{1} & \multicolumn{1}{c|}{0}  & \multicolumn{1}{c|}{0}  & \multicolumn{1}{c|}{0}  & \multicolumn{1}{c|}{0}  & \multicolumn{1}{c|}{1} & $\cdots$ \\
                \hline
                \hline
            \end{tabular}
        }
    \end{table}



    \begin{table}
        \centering 
        {\footnotesize
            \begin{tabular}{r|*{6}{S[table-format=2.4]}|c}
                \hline
                \hline
                sample \# & {1}  & {2} & {3} & {4} & {5} & {6} &
                $\cdots$  \\ 
                \hline
                Variance (Wavelet Coef.) &  -1.3971 & 4.5459 &  3.8660
                & 3.4566
                & 0.3292 & 0.3901 & $\cdots$   
                \\
                Skewness (Wavelet Coef.)  & 3.3191 & 8.1674&-2.6383
                & 9.5228
                & -4.4552  & -0.1428 & $\cdots$ 
                \\
                Kurtosis (Wavelet Coef.)
                & -1.3927 & -2.4586 & 1.9242
                & -4.0112
                & 4.5718 & -0.0319 & $\cdots$ \\ 
                Entropy (Wavelet Coef.) & -1.9948 &-1.4621& 0.1065
                & -3.5944
                & -0.9888 &  0.3508 & $\cdots$ 
                \\ 
                Genuine or Forged & {1} & {0}  & {0}  & {0}  & {0}  & {1} & $\cdots$ \\
                \hline
                \hline
            \end{tabular}
        }
    \end{table}

        \begin{table}
        \footnotesize \setlength{\tabcolsep}{3pt}
        \centering 
            \begin{tabular}{l*{6}{S[table-format=-1.4]}c}
                \toprule
                sample \# & {1}  & {2} & {3} & {4} & {5} & {6} &
                $\cdots$  \\ 
                \midrule
                Variance (Wavelet Coef.) &  -1.3971 & 4.5459 &  3.8660
                & 3.4566
                & 0.3292 & 0.3901 & $\cdots$   
                \\
                Skewness (Wavelet Coef.)  & 3.3191 & 8.1674&-2.6383
                & 9.5228
                & -4.4552  & -0.1428 & $\cdots$ 
                \\
                Kurtosis (Wavelet Coef.)
                & -1.3927 & -2.4586 & 1.9242
                & -4.0112
                & 4.5718 & -0.0319 & $\cdots$ \\ 
                Entropy (Wavelet Coef.) & -1.9948 &-1.4621& 0.1065
                & -3.5944
                & -0.9888 &  0.3508 & $\cdots$ 
                \\ 
                Genuine or Forged & {1} & {0}  & {0}  & {0}  & {0}  & {1} & $\cdots$ \\
                \bottomrule
            \end{tabular}
    \end{table}


\end{document}

相关内容