2

(This question does not solve this because that was about centering within linewidth. I'm interested in the full page width.)

I'm producing a table like this:

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

The result looks like this:

enter image description here

I want to center this horizontally on the page. My first try was wrapping the chunk in \centerline{}, but that got me this:

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

The interesting part in the resulting .tex file looks like this:

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

}

If I manually edit that to this:

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

Then it works. But how can I accomplish this automatically? After reading this and this, I tried:

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 }

But the format of x is not as I got when calling print(ltable) earlier:

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

That is, this will output the brackets with numbers in front of every line and it will not produce a table. Can anyone help me with this?

Community
  • 1
  • 1
L42
  • 3,052
  • 4
  • 28
  • 49
  • Do you want the table to float? – CL. Apr 25 '16 at 15:59
  • I want the table to be positioned in the middle (horizontally speaking) of the whole page width. – L42 Apr 25 '16 at 16:07
  • Sure. But still it can either float (it appears where it fits, according to LaTeX's rules) or appear as part of the text, at a position corresponding to its position in the source code. – CL. Apr 25 '16 at 16:17
  • Sorry for not answering the question. I would like it to float, but a working solution with a fixed position could also work. – L42 Apr 25 '16 at 16:21

1 Answers1

3

Case 1: Non-floating table

If the table is not supposed to float, it is easy to enclose the tabular in a makebox as suggested 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

Case 2: Floating table

In this case, the best solution I found uses the adjustwidth environment from the changepage package.

First, define a new environment that encloses its content in an adjustwidth environment to remove the left margin.

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

Second, pass this new environment as latex.environments to print.xtable. (If there was a way to pass arguments to the environment in latex.environments, the custom environment would not be necessary at all.)

\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

(The screenshots contain some text that is not present in the code example. It is only there to visualize the page dimensions.)

Community
  • 1
  • 1
CL.
  • 14,577
  • 5
  • 46
  • 73
  • Thank you for answering. Neither approach actually centered the table on the whole page width for me. Case 1 centered the table using `\textwidth`, which is not what I want. Do you have any suggestions as to how I can tweak this to work on the paper width instead? Case 2 required some hit-and-miss with the adjustwidth value to get a table centered, but that same value will not center the next table in my document, which has a different width. – L42 Apr 25 '16 at 17:45
  • @L42 Did you test case 1? I think it works, except for a very small margin on the left side. Sorry for case 2, that was wrong, but I think I fixed it. You don't have to adjust the `adjustvalue` settings for each table but only once to match your document's left and right margins. I added some screenshots. – CL. Apr 25 '16 at 17:56
  • 1
    Yes I tested. For case 1: [code here](http://pastebin.com/Ld1grkCw) and [screenshot here](http://s31.postimg.org/fzcqkuiwr/case1.png). For new case 2: [code here](http://pastebin.com/mbN3uS1v) and [screenshot here](http://s31.postimg.org/rbect6tkr/case2.png). See the second table in case 2: It is too far to the right. – L42 Apr 25 '16 at 18:49
  • @L42 Case 1: That's weird; I cannot reproduce your screenshot. [This](http://i.stack.imgur.com/Aupla.png) is how it looks when I compile your code: Slightly shifted to the right, but far less then on your image. Sorry, I don't know the reason. Case 2: Nooo … this is not too far to the right, the table is just too wide. It simply does not fit on the page - this is not an alignment issue – it's rather about scaling. I'm afraid I cannot offer much more help. If you don't get more advice here, maybe tex.stackexchange.com is the right place for your question (at least the LaTex part). – CL. Apr 25 '16 at 20:24
  • Thanks for your help! For case 2: Yes, I realize the table is too wide. But my thinking is that if it is too wide, then it should go across the left and right edge at the same time. My second table there is crossing the right edge, but not the left. This makes me conclude that it cannot be centered correctly on the page. – L42 Apr 25 '16 at 20:57
  • 1
    @L42 It is centered, but only if it fits. If not, it overflows to the right. To see this clearly, try to compile my "case 2" example with this code `for (i in 20:11) { print(xtable(t(cars)[, 1:i]), latex.environments = "widestuff") }`. It demonstrates that all tables with less than 17 columns fit on the page and are correcly centered. Those with 17 or more columns are cut off at the right. – CL. Apr 25 '16 at 21:12