2

I have hundreds of latex tables I generated using Stargazer. Almost all of these tables can be rectified by wrapping tabular as such \resizebox{\textwidth}{!}{ \begin{tabular} ... \end{tabular}}. I have already attempted to make adjustments using the stargazer function.

However, going back to latex, I thought that I would be able to simply use the etoolbox package and apply \BeforeBeginEnvironment{tabular}{\resizebox{\textwidth}{!}{} \AfterEndEnvironment{tabular}{}}. My hopes would be that every time \begin and \end tabular shows up in my code, it is autcomatically wrapped accordingly. However, it did not work. I suspect it is because latex interprets the {, and } quite literally. Any suggestions? I do not want to have to modify each individual table manually as I find myself having to regenerate tables quite often.

5
  • Using \resizebox will also scale tables up to fit \textwidth. Is that what you want, or only to scale down to fit \textwidth?
    – Werner
    Feb 22 at 20:47
  • I typically want to scale down. I like all my tables fitting on the page, where as I don't mind if they are too small sometimes. Feb 22 at 20:56
  • 1
    @r-learning-machine If you really want to scale your tables, have a look at the adjustbox package, but please note that the result will most likely look like a ransom letter with all the different font sizes. See also tex.stackexchange.com/questions/425453/… for some more reasons why scaling tables might not be the best of ideas. Feb 22 at 21:03
  • @samcarter_is_at_topanswers.xyz using adjustbox along with my original approach with etoolbox seems to have worked. Thanks. Feb 22 at 21:25
  • 1
    @r-learning-machine You're welcome! ... and please don't show the result to anybody. Feb 22 at 21:41

1 Answer 1

4

You might do it, but don't blame me if something goes wrong, because some macro uses tabular internally… You've been warned.

\documentclass{article}
\usepackage{graphicx}

\usepackage{lipsum}% for context

\newsavebox{\scaletabular}

\AddToHook{env/tabular/before}{%
  \begin{lrbox}{\scaletabular}%
}
\AddToHook{env/tabular/after}{%
  \end{lrbox}\resizebox{\textwidth}{!}{\usebox{\scaletabular}}%
}

\begin{document}

\lipsum[1][1-4]

\begin{table}[htp]
\centering

\begin{tabular}{cccccccccc}
111111 & 222222 & 333333 & 444444 & 555555 & 666666 & 77777777 & 88888888 & 999999 & 000000\\
111111 & 222222 & 333333 & 444444 & 555555 & 666666 & 77777777 & 88888888 & 999999 & 000000\\
111111 & 222222 & 333333 & 444444 & 555555 & 666666 & 77777777 & 88888888 & 999999 & 000000\\
111111 & 222222 & 333333 & 444444 & 555555 & 666666 & 77777777 & 88888888 & 999999 & 000000\\
\end{tabular}

\caption{Horrible table}

\end{table}

\lipsum[2][1-4]

\begin{table}[htp]
\centering

\begin{tabular}{cc}
AA & BB \\
CC & DD \\
\end{tabular}

\caption{Preposterously horrible table}

\end{table}

\lipsum[3][1-4]

\end{document}

enter image description here

You see what could go wrong, don't you? Tables should never be scaled up or down. This is even more obvious when not all of them exceed the text width. Well, one could add a test to see whether the table is “narrow”, but I wouldn't follow this path anyway, so I'll stop here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .