R 如何使xtable不忽略whitspace或分别对齐小数点?

R 如何使xtable不忽略whitspace或分别对齐小数点?,r,latex,r-markdown,xtable,R,Latex,R Markdown,Xtable,我将要编写一个函数,该函数将生成回归表列表的屏幕输出和LaTeX/PDF输出,并且后者的小数点对齐有问题 从这个玩具表regmat 1 2 3 4 (Intercept) " 33.99 ***" " 3370.83 ***" " 39.42 ***&qu

我将要编写一个函数,该函数将生成回归表列表的屏幕输出和LaTeX/PDF输出,并且后者的小数点对齐有问题

从这个玩具表
regmat

                1                 2                 3                 4                
(Intercept)     "      33.99 ***" "    3370.83 ***" "      39.42 ***" "      46.80 ***"
                "      (1.89)   " "    (260.49)   " "      (2.64)   " "      (6.37)   "
cyl             "       ---     " "       ---     " "      -1.51 ** " "       ---     "
                "       ---     " "       ---     " "      (0.42)   " "       ---     "
factor(am)1     "       ---     " "     180.92    " "       0.18    " "       0.19    "
                "       ---     " "    (139.63)   " "      (1.30)   " "      (1.49)   "
factor(cyl)6    "      -4.26 ** " "    -303.13 *  " "       ---     " "      -1.28    "
                "      (1.39)   " "    (140.73)   " "       ---     " "      (1.53)   "
factor(cyl)8    "      -6.07 ***" "    -216.37    " "       ---     " "      -1.39    "
                "      (1.65)   " "    (228.43)   " "       ---     " "      (2.16)   "
hp              "       ---     " "      -3.21 *  " "       ---     " "      -0.10 ** "
                "       ---     " "      (1.37)   " "       ---     " "      (0.03)   "
wt              "      -3.21 ***" "    -249.68 ** " "      -3.13 ** " "      -7.12 ** "
                "      (0.75)   " "     (88.56)   " "      (0.91)   " "      (2.24)   "
wt:hp           "       ---     " "       ---     " "       ---     " "       0.02 *  "
                "       ---     " "       ---     " "       ---     " "      (0.01)   "
Deviance        "    1126.05    " "11260471.88    " "    1126.05    " "    1126.05    "
Df              "      31       " "      31       " "      31       " "      31       "
Log-Likelihood  "     -73.31    " "    -217.60    " "     -73.99    " "     -67.34    "
AIC             "     156.62    " "     449.20    " "     157.99    " "     150.69    "
BIC             "     163.95    " "     459.46    " "     165.32    " "     162.41    "
Resid. deviance "     183.06    " " 1510255.92    " "     191.05    " "     126.07    "
Resid. Df       "      28       " "      26       " "      28       " "      25       "
Num. obs.       "      32       " "      32       " "      32       " "      32       "
我用
as.data.frame(regmat)
获得了一个漂亮的屏幕输出

小数点都是对齐的,即使在带括号的行中,下GOF部分中的整数在点之前结束

现在我想要一个类似的LaTeX/PDF输出
xtable
使用
rmarkdown
和此代码

library(xtable)
xtbl <- xtable(regmat, comment=FALSE)
align(xtbl) <- c("L{2.4cm}", rep(c("L{2cm}@{\\hskip 0.1cm}"), 4))
print(xtbl,
      tabular.environment="tabularx",
      width=".5\\textwidth",
      hline.after=c(-1, 0, 16, nrow(regmat)),
      comment=FALSE)
获取此失败的LaTeX/PDF表格结果

看起来不错,但是可以看到左对齐从第一个非空格字符开始,所有空格似乎都被忽略了

rep(c(“L{2cm}{\\hskip 0.1cm}”)
中,我尝试了
L
c
R
、和
p
标题.tex,但它们都没有对齐小数点处的值,即似乎没有使用为对齐小数点而编写的包
\usepackage{dcolumn}

我不在乎
dcolumn
,不过,我只是觉得忽略我的空格很奇怪

如何使用
dcolumn
或不忽略空格,在LaTeX PDF表格中获得与上述屏幕输出相同的对齐方式?

资料
regmat我有一段时间没有使用
xtable
,但是我刚才问的答案可能会有帮助。@eipi10实际上,我在我的问题中提到了这个问题。这是一个不同的尝试,当你仔细看的时候,GOF实际上没有对齐。啊,我错过了!有很多最近的软件包试图让ta使用R创建ble更容易。我没有使用过,但有在小数点上对齐的方法。@eipi10我尝试过
库(huxtable);set_align(huxtable(regmat),value=“.”
但似乎不起作用(可能是由于
”--“
单元格).@jay.sf您的问题是您的表中已经有很多空格。huxtable在进行对齐时会尝试尊重这些空格。如果您执行
regmat2
library(xtable)
xtbl <- xtable(regmat, comment=FALSE)
align(xtbl) <- c("L{2.4cm}", rep(c("L{2cm}@{\\hskip 0.1cm}"), 4))
print(xtbl,
      tabular.environment="tabularx",
      width=".5\\textwidth",
      hline.after=c(-1, 0, 16, nrow(regmat)),
      comment=FALSE)
---
output: 
  pdf_document:
    includes:
      in_header: "X:/<myDir>/header.tex"
classoption: 
- a4paper
- landscape
geometry: margin=2cm
header-includes: 
- \pagenumbering{gobble}
- \usepackage{dcolumn}
---
\usepackage{array}
\usepackage{tabularx}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\
\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\
\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\
\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{P}[1]{>{\raggedright\tabularxbackslash}p{#1}}
regmat <- structure(c("      33.99 ***", "      (1.89)   ", "       ---     ", 
"       ---     ", "       ---     ", "       ---     ", "      -4.26 ** ", 
"      (1.39)   ", "      -6.07 ***", "      (1.65)   ", "       ---     ", 
"       ---     ", "      -3.21 ***", "      (0.75)   ", "       ---     ", 
"       ---     ", "    1126.05    ", "      31       ", "     -73.31    ", 
"     156.62    ", "     163.95    ", "     183.06    ", "      28       ", 
"      32       ", "    3370.83 ***", "    (260.49)   ", "       ---     ", 
"       ---     ", "     180.92    ", "    (139.63)   ", "    -303.13 *  ", 
"    (140.73)   ", "    -216.37    ", "    (228.43)   ", "      -3.21 *  ", 
"      (1.37)   ", "    -249.68 ** ", "     (88.56)   ", "       ---     ", 
"       ---     ", "11260471.88    ", "      31       ", "    -217.60    ", 
"     449.20    ", "     459.46    ", " 1510255.92    ", "      26       ", 
"      32       ", "      39.42 ***", "      (2.64)   ", "      -1.51 ** ", 
"      (0.42)   ", "       0.18    ", "      (1.30)   ", "       ---     ", 
"       ---     ", "       ---     ", "       ---     ", "       ---     ", 
"       ---     ", "      -3.13 ** ", "      (0.91)   ", "       ---     ", 
"       ---     ", "    1126.05    ", "      31       ", "     -73.99    ", 
"     157.99    ", "     165.32    ", "     191.05    ", "      28       ", 
"      32       ", "      46.80 ***", "      (6.37)   ", "       ---     ", 
"       ---     ", "       0.19    ", "      (1.49)   ", "      -1.28    ", 
"      (1.53)   ", "      -1.39    ", "      (2.16)   ", "      -0.10 ** ", 
"      (0.03)   ", "      -7.12 ** ", "      (2.24)   ", "       0.02 *  ", 
"      (0.01)   ", "    1126.05    ", "      31       ", "     -67.34    ", 
"     150.69    ", "     162.41    ", "     126.07    ", "      25       ", 
"      32       "), .Dim = c(24L, 4L), .Dimnames = list(c("(Intercept)", 
" ", "cyl", "   ", "factor(am)1", "     ", "factor(cyl)6", "       ", 
"factor(cyl)8", "         ", "hp", "           ", "wt", "             ", 
"wt:hp", "               ", "Deviance", "Df", "Log-Likelihood", 
"AIC", "BIC", "Resid. deviance", "Resid. Df", "Num. obs."), c("1", 
"2", "3", "4")))