I would suggest either to rotate the Table, using the lscape package, or to resize your table (easier, IMHO) with \scalebox
from the graphics package. I often use the latter for "not too large" tables (say, 10 rows by 5 to 7 columns with custom headings), that won't fit in a rotated page. It has to be done from within your tex
file.
Here is a toy example:
In R:
x <- replicate(2, sample(LETTERS[1:10], 100, rep=T))
tab <- table(x[,1], x[,2])
library(xtable)
print(xtable(tab, digits=0), file="tab.tex", floating=FALSE)
Here we ask to write the resulting table in a file (you may have to change the full path to reflect your working tex directory), tab.tex
. This way, you don't have to edit your Latex file too much.
The scaling factor used here, 0.9, means 90% of text width. See the on-line help for further details. You can then compile the resulting file with pdflatex
.
There's a subtlety here: I didn't ask to get a floating table (see floating=FALSE
when calling xtable()
), bacause we can't use \scalebox
around a float. Should you want to add caption, label, etc., you just need to replace the above command with
\begin{table}[ht]
\begin{center}
\input{tab}
\end{center}
\end{table}
Finally, the landscape solution is easily obtained as
\documentclass{article}
\usepackage{lscape}
\begin{document}
\begin{landscape}
\input{tab}
\end{landscape}
\end{document}
Here, it will work with or without asking for a floating object when calling xtable()
in R.
Edit:
With the syntax you provided in your updated question, here's is how it should read:
cat("\\documentclass{article}\n\\usepackage{graphics}\n\\begin{document}\n", file="1.tex")
cat("\\scalebox{0.7}{", file="1.tex", append=TRUE)
print(xtable(tab), include.rownames=TRUE, floating=FALSE, file="1.tex", append=TRUE)
cat("}", file="1.tex", append=TRUE)
cat("\\end{document}\n", file="1.tex", append=TRUE)
Here, tab
refers to the two-by-two table from my example, replace it by table
if you named it like this (which is not a good idea because this is the name of an R function). The table is already aligned on the left margin. So, if you want to change that, a quick and dirty fix is to ask for right to left shift (I'm pretty sure TeXnicians know of a better way to do that):
cat("\\scalebox{0.7}{\\hskip-50pt", file="1.tex", append=TRUE)
Suggestion with package booktabs
for rules with better spacing and package siunitx
for formatting the numbers and aligning them in the columns at the decimal marker. The first column is not changed, because I have not understood it (e.g., is the plus sign a suffix/part of LR
or it is a "binary" operator?). The line between the data rows is replaced by some extra space to avoid the excessive use of rules.
Often table captions are above the tabular
, because the tables are read top to down. Package caption
fixes the caption spacing before and after for table captions above the tabular
.
\documentclass{article}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{siunitx}
\begin{document}
\begin{table}
\centering
\caption{Long-term Forecast Performance}
\label{tab:Dfor2}
\begin{tabular}{
c
S[table-format=-5.2]
S[table-format=6.2]
S[table-format=6.2]
S[table-format=-1.2]
S[table-format=1.2]
}
\toprule
& {ME} & {RMSE} & {MAE} & {MPE} & {MAPE} \\
\midrule
DSHW(7, 364) & 26197.65 & 88891.45 & 68771.39 & 1.66 & 4.33 \\
BATS(7, 365) & -46316.36 & 98041.36 & 77747.46 & -2.84 & 4.88 \\
\addlinespace
LR+ DSHW(7, 364) & 137934.59 & 165886.30 & 140175.06 & 8.42 & 8.55 \\
LR+ BATS(7, 365) & 40750.45 & 78797.68 & 54005.67 & 2.49 & 3.31 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
Best Answer
Use the
booktabs
package to produce a high quality table and thesiunitx
package to format units and produce alignment in cells; a little example that gives you the necessary information to build your table: