从Hmisc包中的latex()创建的表是水平左对齐而不是在pdf文档中以水平方式居中

时间:2013-05-07 07:43:41

标签: r latex hmisc

使用R-studio和Knitr创建pdf我无法将表格置于水平中心。从下面的例子可以看出,使用xtable()可以正常工作,但latex() - tabels都是左对齐的。据我了解Hmisc文档,从latex()创建的表应该是自动地进行水平居中,但我必须做错了。

\documentclass{article}

\begin{document}

<<>>=
library(Hmisc)
library(tables)
library(xtable)
@


The tables are all left-aligned:
<<results='asis'>>=
latex(    tabular( (Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data=iris )  )
@

<<results='asis'>>=
latex(    tabular( (Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data=iris ),center="center"   )
@

<<results='asis'>>=
latex(    tabular( (Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data=iris ),center="centering"   )
@


I have tried to use the fig.align option, but it does not do it:
<<results='asis',fig.align='center'>>=
latex(    tabular(   (Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data=iris      )    )
@


with xtable it automatically centers:
<<results='asis'>>=
xtable(table(Puromycin$conc, Puromycin$state))
@

\end{document}

R版本3.0.0(2013-04-03)

平台:x86_64-w64-mingw32 / x64(64位)

1 个答案:

答案 0 :(得分:5)

我没有时间浏览latex.sHmisc中的代码,但在此之前,请随时将您的数据块包装到居中环境中。不是最干净的解决方案,但它完成了工作。

\begin{centering}
  <<results='asis'>>=
  latex(tabular((Species + 1) ~ (n=1) + Format(digits=2)*(Sepal.Length + Sepal.Width)*(mean + sd), data=iris ))
@
\end{centering}

这会产生一个居中的表格。