xtable 和 knitr:如何在整个页面宽度上居中 table?

xtable and knitr: How to center table on full page width?

没有解决这个问题,因为那是关于在线宽内居中。我对整个页面宽度感兴趣。)

我正在制作这样的 table:

\documentclass{article}

\begin{document}

<<tabletest, message=F, warning=F, echo = F, results = "asis">>=
library(readr)
library(xtable)
# Create dummy data.frame
values <- 1:14
df <- t(data.frame(values, values))
colnames(df) <- paste("column", values, sep="")
rownames(df) <- c("row1", "row2")
ltable <- xtable(
  x = df
)
print(ltable)
@

\end{document}

结果如下所示:

我想在页面上将其水平居中。我的第一次尝试是将块包装在 \centerline{} 中,但这让我得到了这个:

Runaway argument?
{ \begin {table}[ht] \centering \begin {tabular}{rrrrrrrrrrrrrrr} \hline \ETC.
! Paragraph ended before \centerline was complete.
<to be read again> 
                   \par

生成的 .tex 文件中有趣的部分如下所示:

\centerline{
% latex table generated in R 3.2.5 by xtable 1.8-2 package
% Mon Apr 25 16:40:48 2016
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrrrrrrrrrr}
  \hline
 & column1 & column2 & column3 & column4 & column5 & column6 & column7 & column8 & column9 & column10 & column11 & column12 & column13 & column14 \ 
  \hline
row1 &   1 &   2 &   3 &   4 &   5 &   6 &   7 &   8 &   9 &  10 &  11 &  12 &  13 &  14 \ 
  row2 &   1 &   2 &   3 &   4 &   5 &   6 &   7 &   8 &   9 &  10 &  11 &  12 &  13 &  14 \ 
   \hline
\end{tabular}
\end{table}

}

如果我手动将其编辑为:

% latex table generated in R 3.2.5 by xtable 1.8-2 package
% Mon Apr 25 16:40:48 2016
\begin{table}[ht]
\centerline{
\begin{tabular}{rrrrrrrrrrrrrrr}
  \hline
 & column1 & column2 & column3 & column4 & column5 & column6 & column7 & column8 & column9 & column10 & column11 & column12 & column13 & column14 \ 
  \hline
row1 &   1 &   2 &   3 &   4 &   5 &   6 &   7 &   8 &   9 &  10 &  11 &  12 &  13 &  14 \ 
  row2 &   1 &   2 &   3 &   4 &   5 &   6 &   7 &   8 &   9 &  10 &  11 &  12 &  13 &  14 \ 
   \hline
\end{tabular}
}
\end{table}

然后就可以了。但是我怎样才能自动完成这个呢?看完this and this,我试了:

x <- capture.output(print(xtable(ltable)))
x <- gsub("\\centering", "", x, fixed=TRUE) # Remove \centering
x <- gsub("\begin{table}[ht]", "\begin{table}[ht]\centerline{", x, fixed=TRUE) # Add \centerline{
x <- gsub("\end{table}", "\end{table}}", x, fixed=TRUE) # Add ending }

但是 x 的格式与我之前调用 print(ltable) 时得到的格式不同:

> print(x)
 [1] "% latex table generated in R 3.2.5 by xtable 1.8-2 package"                                                                                                                                                                                                                                                                             
 [2] "% Mon Apr 25 16:51:02 2016"

也就是说,这将输出每行前面带有数字的括号,并且不会产生table。谁能帮我解决这个问题?

案例 1:非浮动 table

如果 table 不应该浮动,很容易按照建议将 tabular 包含在 makeboxhere:

\documentclass{article}
\begin{document}

<<echo = F, results = "asis">>=
library(xtable)

cat("\makebox[\textwidth]{")
print(xtable(t(cars)[, 1:16]), floating = FALSE)
cat("}")

@
\end{document}

案例 2:浮动 table

在这种情况下,best solution I found 使用 changepage 包中的 adjustwidth 环境。

首先,定义一个新环境,将其内容包含在 adjustwidth 环境中以移除左边距。

\newenvironment{widestuff}{\begin{adjustwidth}{-4.5cm}{-4.5cm}\centering}{\end{adjustwidth}}

其次,将这个新环境作为 latex.environments 传递给 print.xtable。 (如果 latex.environments 中有一种方法可以将参数传递给环境,则根本不需要自定义环境。)

\documentclass{article}
\usepackage{changepage}
\newenvironment{widestuff}{\begin{adjustwidth}{-4.5cm}{-4.5cm}\centering}{\end{adjustwidth}}
\begin{document}
<<echo = F, results = "asis">>=
library(xtable)
print(xtable(t(cars)[, 1:16]), latex.environments = "widestuff")

print(xtable(t(cars)[, 1:12]), latex.environments = "widestuff")
@
\end{document}

(屏幕截图包含一些代码示例中不存在的文本。它只是为了可视化页面尺寸。)