0

I was trying to use the answer from here to ensure my tables fit to the width of my page. I'd like to redefine the tabular environment so that this change is applied automatically if the table's width exceeds the page width. Is there any way to do this?

user41177
  • 375
  • 3
    I wouldn't do this. This lead to inconsistent font size of table texts. Rather redesign table that use prescribed width (=\textwidth). – Zarko Sep 13 '23 at 05:30
  • 3
    Just because a particular answer gets a lot of upvotes does not mean that its proposed solution method can or should be applied willy-nilly. Did you maybe notice the "Never use \resizebox with tables" comment below the answer you've provided a link to? If you did, did you pause and ponder whether you should heed this admonition? – Mico Sep 13 '23 at 05:42
  • 2
  • 4
    never use \resizebox on tables. – David Carlisle Sep 13 '23 at 06:02
  • 3
    I just sacrificed 1 of my reputation points to downvote that old answer (and if I could downvote it 300 times for 300 Reputation I'd do so instantly). I finally found (one of) the roots for all those scaling-table code fragments. That thing should be pruned from the internet! – Skillmon Sep 13 '23 at 06:47
  • @Mico I looked at other threads. Not specifically that one. I tried using tabular* and tabularx but I was not able to get the desired results. – user41177 Sep 13 '23 at 21:51
  • @user41177 - Please post a new query that provides many more details about one or more of the tables you wish to create. Without such information, it's well-nigh impossible to determine what's causing the table-exceeds-the-width-of-the-text-block problem. – Mico Sep 13 '23 at 22:01

1 Answers1

0

Just for fun:

\documentclass{article}
\usepackage{graphicx}
\usepackage{environ}
\usepackage{showframe}% alignment tool

\NewEnviron{tabscale}{\bgroup% local changes \normalsize \sbox9{\BODY}% \ifdim\wd9<\textwidth \usebox9 \else %\usebox9 \small \tabcolsep=0.86\tabcolsep \sbox9{\BODY}% \ifdim\wd9<\textwidth \usebox9 \else %\usebox9 \footnotesize \tabcolsep=0.86\tabcolsep \sbox9{\BODY}% \ifdim\wd9<\textwidth \usebox9 \else %\usebox9 \scriptsize \tabcolsep=0.86\tabcolsep \sbox9{\BODY}% \ifdim\wd9<\textwidth \usebox9 \else %\usebox9 \tiny \tabcolsep=0.86\tabcolsep \sbox9{\BODY}% \ifdim\wd9<\textwidth \usebox9 \else \resizebox{\textwidth}{!}{\BODY}% by this point you are illegible anyway \fi \fi \fi \fi \fi \egroup}

\begin{document}

\begin{table} \centering \begin{tabscale} \begin{tabular}{cccccccccccc} The & quick & red & fox & jumped & over & the & lazy & brown & dog & and & grinned \end{tabular} \end{tabscale} \end{table}

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Thank you for this. I really appreciate you taking the time to do this. It seems like the concept of what I am trying to do is not recommended. – user41177 Sep 14 '23 at 19:49
  • There are other ways to make tabulars smaller. And I won't be offenced if tyou throw out all the steps except the first and last. I just thought it would be cute to throw them all into one environ. – John Kormylo Sep 14 '23 at 20:16