2016-04-25 15 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 } 

しかし、以前のprint(ltable)を呼び出すときに私が得たとしてxの形式ではありません。

この意志の出力に、前の番号のブラケットをある
> 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:非浮動テーブル

テーブルが浮くことになっていない場合はhere示唆されているように、makeboxtabularを同封するのは簡単です:

\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 foundchangepageパッケージのadjustwidth環境を使用します。

最初に、コンテンツをadjustwidth環境に囲む新しい環境を定義して、左余白を削除します。

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

第二に、print.xtableからlatex.environmentsとして、この新しい環境を渡します。 (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'を使ってテーブルの中心に置いてあります。これは私が望むものではありません。代わりに紙幅で作業するためにこれを微調整する方法についての提案はありますか?ケース2では、テーブルを中央に置くためにadjustwidthの値でいくつかのヒット・アンド・ミスが必要でしたが、その同じ値はドキュメントの次のテーブルの中央になりません。 – L42

+0

@ L42ケース1をテストしましたか?私はそれが動作すると思う、左側の非常に小さいマージンを除いて。申し訳ありませんが、ケース2の場合は間違っていましたが、私はそれを修正したと思います。各テーブルの 'adjustvalue'設定を調整する必要はなく、文書の左右のマージンに合わせて1回だけ調整します。私はいくつかのスクリーンショットを追加しました。 –

+1

はい私はテストしました。ケース1の場合:[コードはこちら](http://pastebin.com/Ld1grkCw)、[スクリーンショットはこちら](http://s31.postimg.org/fzcqkuiwr/case1.png)。新しいケース2の場合:[コードはこちら](http://pastebin.com/mbN3uS1v)と[スクリーンショットはこちら](http://s31.postimg.org/rbect6tkr/case2.png)。ケース2の2番目の表を参照してください。 – L42

関連する問題