dcolumn 不会对齐负数和正数

dcolumn 不会对齐负数和正数

我正在使用 R 分析数据,并使用 R 中的 xtable 包为相关矩阵生成 TeX 输出。我还在 TeX 中实现了 dcolumn 包,以便能够按小数点对齐数据。我的数据有正数和负数。看来生成的输出对齐得比较正确。正数按小数点与其他正数对齐,负数也与其他负数对齐。我认为问题与为负数添加额外的非数字字符(负号)有关,因此字符串比正数大。有人能帮我纠正这个问题吗?

我的代码如下:

% latex table generated in R 2.14.1 by xtable 1.7-0 package
% Tue Mar 20 13:44:16 2012
\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{d}{D{.}{.}{2.2}}
\newcolumntype{s}{D{.}{.}{1.2}}
\usepackage{color}
\usepackage{underscore}
\usepackage{rotating}
\usepackage{caption}
\usepackage{dcolumn}
\usepackage{siunitx}
\begin{document}
\begin{sidewaystable}[ht]
\caption*{Correlation Matrix}
\begin{center}
\scalebox{0.4}{
\begin{tabular}{|rr*{39}{d}|}   
\hline
   &     &   1   &   2   &   3   &   4   &   5   &   6   &   7   &   8   &   9   &   10       &   11   &   12   &   13   &   14   &   15   &   16   &  17   &   18   &   19   &   20   &       21   &   22   &   23   &   24   &   25   &   26   &   27   &   28   &   29   &   30   &       31   &   32   &   33   &   34   &   35   &   36   &   37   &   38   &   39   \\ \hline


\multicolumn{1}{|r|}{1}   &  \multicolumn{1}{r|}{ APCcom }  &   \\
&  \multicolumn{1}    {c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.09 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.10 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c|}{ \textcolor{black}{ 1.00 } }  \\  

\multicolumn{1}{|r|}{2}   &  \multicolumn{1}{r|}{ APCemp }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.33 } }  &   \\
&  \multicolumn{1}{c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.18 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.73 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.14 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c|}{ \textcolor{black}{ 1.00 } }  \\  

\multicolumn{1}{|r|}{3}   &  \multicolumn{1}{r|}{ APCrule }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.20 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.33 } }  &   \\
&  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.81 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.23 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c|}{ \textcolor{black}{ 1.00 } }  \\  

\multicolumn{1}{|r|}{4}   &  \multicolumn{1}{r|}{ ATFtot }  &  \multicolumn{1}{c}{ \textcolor{black}{ -0.03 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.27 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.17 } }  &   \\
&  \multicolumn{1}{c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.40 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.40 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.09 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c|}{ \textcolor{black}{ 1.00 } }  \\  

\multicolumn{1}{|r|}{5}   &  \multicolumn{1}{r|}{ flstot }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.37 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.24 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.02 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ -0.32 } }  &   \\
&  \multicolumn{1}{c}{ \textcolor{black}{ 0.10 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.10 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.08 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.05 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.21 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.02 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c|}{ \textcolor{black}{ 1.00 } }  \\  

% ... table shortened for space considerations
\hline
\end{tabular}
}
\caption*{r-values below the diagonal, p-values above, \\
significant r-values and p-values appear in \textcolor{red}{red}}
\label{Matrix}
\end{center}
\end{sidewaystable}
\end{document}

答案1

你最好使用以下设施siunitx:这是一个最小的例子

\begin{tabular}{|r|l|SS|}
\hline
\multicolumn{2}{|c}{}   &   1  & 2 \\
\hline
1 & APCcom  &                  & \color{red} 0.01 \\
2 & APCemp  & \color{red} 0.33 &                  \\
3 & APCrule & \color{red} 0.20 & \color{red} 0.33 \\
4 & ATFtot  &            -0.03 & \color{red} 0.27 \\
\hline
\end{tabular}

您可以在此基础上继续发展。 在此处输入图片描述

声明d列然后覆盖它们\multicolumn{1}{c}{...}是导致问题的主要原因。

答案2

负数的原因是——实际上,全部编号是不是在小数点上对齐是因为每个数字目前都被嵌套的and语句双重隐藏,无法被dcolumn和包看到。siunitx\multicolumn{}{}{}\textcolor{}{}

您指出,该表以目前复杂的格式由 生成R。您能否想出如何指示R(i) 抑制所有那些不需要的和实际上适得其反的\multicolumn语句,以及 (ii) 生成\color{red} xyz\color{black} abc语句而不是\textcolor{red}{xyz}\textcolor{black}{abc}?(实际上,因为“黑色”是默认R颜色,如果使用的颜色是黑色,您可能希望指示不要提供任何与颜色相关的声明。)

通过这些修改,您可以使用包S的列类型siunitx来使所有数字在小数点标记上整齐对齐。对于您的特定表格,我会不是建议您使用dcolumn包的d列类型,因为它不能正确处理颜色变化命令,至少在没有大量额外编程的情况下不能。相反,列类型可以轻松S处理命令,正如@egreg的回答所证明的那样。\color{red}

最后,一个建议与对齐小数点上的数字无关,但与具有 41 [!] 列的表格的整体易读性有关。LaTeX 在环境中默认的列间空白量tabular10pt(或2\tabcolsep);这加起来等于很多如果有 40 个列间距,则空白的量。如果要减少空白量,可以使用以下指令

\setlength\tabcolsep{3pt}  % default: 5pt

反过来,您可以将命令的第一个参数\scalebox从 0.4 更改为大约 0.55,同时仍使整个表格适合文本块。希望您的读者会感谢您做出这一改变。

相关内容