How to center LaTeX xtable output in full text width
Asked Answered
M

1

13

I am using tufte-handout (http://mirrors.ibiblio.org/CTAN/macros/latex/contrib/tufte-latex/sample-handout.pdf) to create a small report in latex. I have a file code.Rnw that I knit into code.tex. Below is my code.Rnw:

\documentclass[12pt,english,nohyper]{tufte-handout}
\usepackage{longtable}
\usepackage{geometry}

\begin{document}

<<include=FALSE>>=
library(ggplot2)
library(xtable)
@

\centerline{\Large\bf This is my Main Title}

<<echo=FALSE,results='asis'>>=
fname='plot1.pdf'
pdf(fname,width=4,height=4)
print(qplot(mpg,cyl,data=mtcars))
{dev.off();invisible()}
cat(sprintf('\\begin{marginfigure}
\\includegraphics[width=0.98\\linewidth]{%s}
\\caption{\\label{mar:hist}MPG vs CYL in MTCARS dataset.}
\\end{marginfigure}',sub('\\.pdf','',fname)))
@

This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report.

This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. This is the paragraph in my report. 
\bigskip{}

<<echo=FALSE,results='asis'>>=
x.big <- xtable(mtcars[1:20,1:4], label ='tab:mtcars',caption ='This is the mtcar dataset head.',align = c("rr|lr|r"))

print(x.big, tabular.environment ='longtable', floating = FALSE, include.rownames=FALSE)
@

\end{document}

This produces the following output:

this is my output

What I am trying to do is to allow the xtable output (Table 1) to be centered across the full text. By default, in the tufte-handout package, it seems to center the table in the left non-margin.

I have consulted several sources, including the one indicated in the first sentence of this current post. According to that reference, "Full page–width figures and tables may be placed in figure* or table* environments." I am unsure how to accomplish this, given that I am also knitting this report.

Monticule answered 7/7, 2015 at 1:32 Comment(0)
Q
5

You can work around this problem by wrapping the longtable in a tufte fullwidth environment. This workaround also seems to require a small hack (in line 2) to fix the hsize, but it appears to work as intended.

\begin{fullwidth}
\makeatletter\setlength\hsize{\@tufte@fullwidth}\makeatother
<<echo=FALSE,results='asis'>>=
x.big <- xtable(mtcars[1:20,1:4], label ='tab:mtcars',caption ='This is the mtcar dataset head.',align = c("rr|lr|r"))

print(x.big, tabular.environment ='longtable', floating = FALSE, include.rownames=FALSE)
@
\end{fullwidth}
Query answered 20/7, 2015 at 17:38 Comment(2)
Just a note to others that this solution might not work as expected if using \usepackage{floatrow}. I had that in my preamble, and was facing some issues. Once I removed that, it worked well. Thank you!Monticule
For anyone interested, I posted a follow-up to the current post. Since I had to deleted the \usepackage{floatrow} to get the table centered, then I also lost desired functions with figures and their captions. I have now been trying to determine how to successfully center both a table and figure in the tufte-handout. The follow-up post where this is explored is located here: #31710466Monticule

© 2022 - 2024 — McMap. All rights reserved.