7

请考虑以下 MWE

library(xtable)
DF <- as.data.frame(UCBAdmissions)
print(xtable(DF, align="p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}"), sanitize.text.function = function(x){x}, 
      table.placement="!htp", include.rownames=FALSE, 
      tabular.environment='longtable',floating=FALSE)

我想设置我longtable喜欢的对齐方式

\begin{longtable}{p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}}

仍然当我尝试将参数传递给xtable我得到的对象时

Warning message:
In .alignStringToVector(value) : Nonstandard alignments in align string
Error in print(xtable(DF, align = "p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}"),  : 
  error in evaluating the argument 'x' in selecting a method for function 'print': Error in `align<-.xtable`(`*tmp*`, value = "p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}") : 
  "align" must have length equal to 5 ( ncol(x) + 1 )

我知道我应该为第 5 列添加对齐方式(如何?),但我也不理解错误消息。我应该清理字符串吗?

4

1 回答 1

12

我无法对此进行测试,但我认为您需要对字符串中的反斜杠应用标准 R 转义,删除无关的“\”并添加缺少的“竖线”(|)。然后align<-成功,只有一个警告:

xtb <- xtable(DF, 
       table.placement="!htp", include.rownames=FALSE, 
       tabular.environment='longtable',floating=FALSE)
align(xtb) <- "p{0.4\\textwidth}|p{0.15\\textwidth}|p{0.15\\textwidth}| p{0.15\\textwidth}"
#Warning message:
#In .alignStringToVector(value) : Nonstandard alignments in align string
 print(xtb)

或者:

xtb <- xtable(DF, type="latex",
      table.placement="!htp", include.rownames=FALSE, 
      tabular.environment='longtable',floating=FALSE, align= c("p{0.15\\textwidth}", 
"p{0.4\\textwidth}", "p{0.15\\textwidth}|", "p{0.15\\textwidth}", "p{0.15\\textwidth}"
))
于 2014-01-01T02:25:19.900 回答