TransWikia.com

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

TeX - LaTeX Asked by Miroslav Palanský on December 31, 2020

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}

3 Answers

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).

Answered by frougon on December 31, 2020

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:

enter image description here

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}

Answered by Marijn on December 31, 2020

I just spent a lot of time sorting through this so I'm adding a comment to show what worked or me using the xtable package in R.

Using a resizebox{} or defining a new environment with a resizebox{} works well for resizing a table, as @Marijn notes. However, it doesnt seem to work well with xtable outside of simple cases, because xtable ends the custom environment right before the end{table}, rather than right after end{tabular}. So adding things like captions causes problems.

Others have noted this problem elsewhere.

It seems like the best option is to use the scalebox argument to xtable, and play with the output until you get it to the right size. This is what the package maintainers recommend.

Answered by Amadou Kone on December 31, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP