# Resize the whole table to fit linewidth, not just the tabular

Is there a way to resize the whole table element? I know there is resizebox which can resize the tabular element, as discussed here: Is there a way to slightly shrink a table, including font size, to fit within the column boundaries?. However, when I generate tables directly from R or Stata into a TeX file (which I then input in my code), I need to manually add the resizebox command around the tabular every time I re-generate the table. Here is a minimal (non-working but illustrative) example of what I'd like to do:

\documentclass{article}
\begin{document}

\resizebox{\linewidth}{!}{
\input{table.tex}
}
\end{document}


where table.tex is a separate file containing only a table (this file is generated by a statistical software):

\begin{table}[]
\begin{tabular}{cc}
X  & Y \\
Z & A
\end{tabular}
\end{table}

• Welcome to TeX.SE! Resizing tables that it will have text width cause also resizing font inside table. If natural width of table is relative small in comparison to text width, resulted table will have much larger fonts as is used in document text. Beside this if table has different width, each table will have different font size ... do you really like to have such document? I would not do this. – Zarko Jun 24 at 19:49

The output of programs and libraries used to generate the tables is often customizable. For example in R with the xtable package, you can add extra environments, which you can use for the resizing.

R code:

library(xtable)
A = matrix(c('X','Y','Z','A'),nrow=2,byrow=TRUE)
print(xtable(A),
include.rownames=FALSE,
include.colnames=FALSE,
hline.after=NULL,
latex.environments=c("myresizeenv"))


LaTeX code with output of the xtable command included:

\documentclass{article}
\usepackage{graphicx}
\usepackage{environ}
\NewEnviron{myresizeenv}{\resizebox{\linewidth}{!}{\BODY}}
\begin{document}

\begin{table}[ht]
\begin{myresizeenv}
\begin{tabular}{ll}
X & Y \\
Z & A \\
\end{tabular}
\end{myresizeenv}
\end{table}

\end{document}


Result is a huge 2x2 table:

Note that this example shows (as has been commented in the other answer) that automatic resizing may not be a good idea, because the result can be much too large or too small (or in general inconsistent).

For a bit more consistency xtable provides a scalebox key, which at least ensures that the table fonts are all the same size (if you use the same scale factor for all tables of course).

> print(xtable(A),include.rownames=FALSE,include.colnames=FALSE,hline.after=NULL,scalebox=0.8)
% latex table generated in R 3.4.4 by xtable 1.8-4 package
% Tue Jun 25 00:11:37 2019
\begin{table}[ht]
\centering
\scalebox{0.8}{
\begin{tabular}{ll}
X & Y \\
Z & A \\
\end{tabular}
}
\end{table}


Welcome to TeX.SE!

First and foremost: obviously, Zarko's remark applies: automatic resizing can easily lead to ugly and/or unreadable output. I'll answer your question on technical grounds, but you should consider whether the result is typographically acceptable.

The table environment produces a float, contrary to tabular which creates a box. Resizing a float doesn't really make sense, but resizing a box does. So, let's first change your table.tex like this:

\begin{tabular}{cc}
X & Y \\
Z & A
\end{tabular}

• With this and main.tex unchanged except for the required \usepackage{graphicx}, your example works. Of course, you'll have no float anymore. You should also remove unwanted spaces from this main.tex—see below.

• If you want to keep the floating behavior, and thus the table environment, put it around the \resizebox command, like this:

\documentclass{article}
\usepackage{graphicx}
\begin{document}

\begin{table}
\resizebox{\linewidth}{!}{%
\input{table.tex}%
}% here, the percent sign could be removed (end of paragraph)
\end{table}
\end{document}


Beware of unwanted spaces: when you write

\resizebox{\linewidth}{!}{
...


there is a space at the beginning of what you resize (contributed by the end-of-line). Better use:

\resizebox{\linewidth}{!}{%


In some places, spaces are ignored (for instance, at the end of a paragraph [where TeX adds an implicit \unskip], in vertical mode or after \begin{table} or \begin{tabular}{...}), but not everywhere; in this case, you would have an unwanted space without the percent sign. You can compare this:

\documentclass{article}
\usepackage{graphicx}

\begin{document}
\noindent \resizebox{\linewidth}{!}{a}
\end{document}


with that:

\documentclass{article}
\usepackage{graphicx}

\begin{document}
\noindent \resizebox{\linewidth}{!}{
a}
\end{document}


The added space is clearly visible on the output obtained with the latter (the a is smaller and not centered anymore: there is an interword space on its left). Obviously, due to TeX rules regarding input handling, you get the same result with this:

...
\noindent \resizebox{\linewidth}{!}{ a}
...


I only used the version with a newline before the a because this is the input style used in your example (and exhibits the spacing problem).