2016-04-25 46 views
2

This question不解決這個問題,因爲這是有關線寬內居中,我感興趣的是全幅。)xtable和knitr:如何在整頁寬度上居中對齊表格?

我產生一個表是這樣的:

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

也就是說,這將輸出與前面的數字括號的每一行,它不會產生一個表。誰能幫我這個?

+0

你想表浮動? –

+0

我希望桌子位於整個頁面寬度的中間(水平方向)。 – L42

+0

當然。但它仍然可以浮動(根據LaTeX的規則,它會出現在適合的位置),或者作爲文本的一部分出現在與源代碼中位置相對應的位置。 –

回答

3

案例1:非浮動表

如果表是不應該浮動,很容易在makebox包圍tabular的建議here

\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使用changepage軟件包中的adjustwidth環境。

首先,定義一個新環境,將其內容封裝在一個adjustwidth環境中以刪除左邊距。

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

其次,通過這種新的環境latex.environmentsprint.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

(截圖包含一些文本是不存在的代碼示例。它只是在那裏可視化的頁面尺寸。)

+0

謝謝你的回答。這兩種方法實際上都無法將表格集中在整個頁面寬度上。情況1使用'\ textwidth'居中表,這不是我想要的。你有什麼建議,我可以調整這個工作的紙張寬度,而不是?第二種情況需要使用調整寬度值來獲取表格居中,但是相同的值不會將文檔中的下一個表格居中,該表格具有不同的寬度。 – L42

+0

@ L42您是否測試過案例1?我認爲它是有效的,除了在左邊非常小的餘量。對不起,第二種情況,這是錯誤的,但我認爲我解決了它。您不必爲每個表調整'adjustvalue'設置,而只調整一次以匹配文檔的左右頁邊距。我添加了一些截圖。 –

+1

是的,我測試過。案例1:[code here](http://pastebin.com/Ld1grkCw)和[這裏截圖](http://s31.postimg.org/fzcqkuiwr/case1.png)。對於新的情況2:[code here](http://pastebin.com/mbN3uS1v)和[這裏截圖](http://s31.postimg.org/rbect6tkr/case2.png)。請參閱情況2中的第二個表格:這太靠右了。 – L42

相關問題