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

时间:2016-04-25 15:00:39

标签: r latex knitr xtable

This question没有解决这个问题,因为那是关于线宽的中心。我对整页宽度感兴趣。)

我正在制作这样一张桌子:

\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}

结果如下:

enter image description here

我想在页面上水平居中。我的第一次尝试是将块包裹在\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}

然后它有效。但是我怎么能自动完成这个呢?阅读thisthis后,我尝试了:

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"

也就是说,这将输出每行前面带数字的括号,它不会产生表格。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:3)

案例1:非浮动表

如果表格不应该浮动,可以很容易将tabular括在makebox here中{/ 3}}:

\documentclass{article}
\begin{document}

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

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

@
\end{document}

Output case 1

案例2:浮动表

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

首先,定义一个新环境,在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}

Output case 2

(截图中包含一些代码示例中没有的文字。只是可视化页面尺寸。)