[Tex/LaTex] How to shrink tables so they fit in one page’s length

tables

I have seen a similar question to this one:

However, I do not want to resize my table so it fits inside the width of a page (I already do that without converting it to an image), I want to shrink my table's height so it does not occupy two pages (or more).

I saw some possible solutions like longtable but they split my table into 2 and I do not want that.

Am I forced to convert my table to an image by using the \resizebox from the graphicsx package, or do I have other options?

Best Answer

You are not forced to convert your long table in an image using \resizebox although this command is supplied by the graphicx package. The table will be still a table.

You can define the width and/or the height of the resized table with this command. Note that fixing both measures the fonts may be are not reduced proportionally, so usually is better left one dimension as {!}.

You can also reduce the vertical spacing \arraystretch to reduce the font as less as possible, and may be using another font as \tiny to change sightly the font proportions.

Vertical lines are always evil, but in this case also the horizontal lines for the same reason, specially when \arraystretch is reduced , except the header lines and the bottom line. The package booktabs make a good job for this task. Experiment with this minimal working example:

\documentclass{article}
\usepackage{graphicx,booktabs}
\begin{document}
\centering
{\tiny\renewcommand{\arraystretch}{.8}
\resizebox{!}{.35\paperheight}{%
\begin{tabular}{ccccc}
\toprule
    11 & 12 & 13 & 14 & 15\\
\midrule
    21 & 22 & 23 & 24 & 25\\
    31 & 32 & 33 & 34 & 35\\
    41 & 42 & 43 & 44 & 45\\
    51 & 52 & 53 & 54 & 55\\
    61 & 62 & 63 & 64 & 65\\
    71 & 72 & 73 & 74 & 75\\
    81 & 82 & 83 & 84 & 85\\
    91 & 92 & 93 & 94 & 95\\
    101 & 102 & 103 & 104 & 105\\
    111 & 112 & 113 & 114 & 115\\
    121 & 122 & 123 & 124 & 125\\
    131 & 132 & 133 & 134 & 135\\
    141 & 142 & 143 & 144 & 145\\
    151 & 152 & 153 & 154 & 155\\
    161 & 162 & 163 & 164 & 165\\
    171 & 172 & 173 & 174 & 175\\
    181 & 182 & 183 & 184 & 185\\
    191 & 192 & 193 & 194 & 195\\
    201 & 202 & 203 & 204 & 205\\
    211 & 212 & 213 & 214 & 215\\
    221 & 222 & 223 & 224 & 225\\
    231 & 232 & 233 & 234 & 235\\
    241 & 242 & 243 & 244 & 245\\
    251 & 252 & 253 & 254 & 255\\
    261 & 262 & 263 & 264 & 265\\
    271 & 272 & 273 & 274 & 275\\
    281 & 282 & 283 & 284 & 285\\
    291 & 292 & 293 & 294 & 295\\
    301 & 302 & 303 & 304 & 305\\
    311 & 312 & 313 & 314 & 315\\
    321 & 322 & 323 & 324 & 325\\
    331 & 332 & 333 & 334 & 335\\
    341 & 342 & 343 & 344 & 345\\
    351 & 352 & 353 & 354 & 355\\
    361 & 362 & 363 & 364 & 365\\
    371 & 372 & 373 & 374 & 375\\
    381 & 382 & 383 & 384 & 385\\
    391 & 392 & 393 & 394 & 395\\
    401 & 402 & 403 & 404 & 405\\
    411 & 412 & 413 & 414 & 415\\
    421 & 422 & 423 & 424 & 425\\
    431 & 432 & 433 & 434 & 435\\
    441 & 442 & 443 & 444 & 445\\
    451 & 452 & 453 & 454 & 455\\
    461 & 462 & 463 & 464 & 465\\
    471 & 472 & 473 & 474 & 475\\
    481 & 482 & 483 & 484 & 485\\
    491 & 492 & 493 & 494 & 495\\
    501 & 502 & 503 & 504 & 505\\
\bottomrule
\end{tabular}}}
\end{document}

MWE

Note that some like:

\resizebox{!}{\textheight}{ ...}

will not produce a table as long as the text heigth, because you are defining here only the height of the box, but in the starred form, it refers to the height + depth, that is:

\resizebox*{!}{\textheight}{ ...}

will make the table as long as \textheight (556.4pt in the above MWE).

Another command that you can use, from the same package graphicx, is \scalebox. The problem here is that you must have some idea of how the scaling should be, or experiment with the scaling values until you obtain what you want. To test it, simple replace in the MWE the \resizebox command by:

\scalebox{3}[2.2]{...}

Edit: The "Dimension too large" error of your comment is a problem of the table design, not of the \resizebox command. LaTeX cannot wrap the text inside left, right and centered columns. For columns with long text, you must use the p{width} or m{width} columns in a tabular environment. Another option is the L,C,R columns of tabulary environment or the X column of the tabularx environment. These environments need the package of the same name and the total width of the table as first argument. For example:

\usepackage{tabulary}
...

\begin{tabulary}{\linewidth}{RCLL}
...
\end{tabulary}

I change your code to use tabularx with custom columns that use a different font, colors and justification. I added also row colors to avoid vertical and most horizontal lines, for the rest use those of the booktabs package and not \hline. Thi is the result:

MWE

\documentclass[a4paper]{article}
\usepackage{microtype}
\usepackage[margin=2.54cm]{geometry}
\usepackage{graphicx,booktabs,tabularx}
\usepackage[table]{xcolor}

\newcolumntype{R}{>{\leavevmode\color{magenta!70!black}\ignorespaces\sffamily\bfseries}p{2.5cm}}%
\newcolumntype{H}{>{\leavevmode\color{orange!30!black}\ignorespaces\raggedright\arraybackslash\sffamily}X}%
\newcolumntype{J}{>{\leavevmode\color{green!30!black}\ignorespaces\sffamily}X}%
\newcolumntype{W}{>{\leavevmode\color{blue!30!black}\ignorespaces\raggedleft\arraybackslash\sffamily}X}%

\rowcolors{2}{purple!05}{olive!05}
\begin{document}
\centering
{\resizebox*{\textwidth}{\textheight}{%
\renewcommand{\arraystretch}{2}
\begin{tabularx}{\textwidth}{RHJW}
\toprule\rowcolor{white}
    & \textbf{Hibernate OGM} & \textbf{EclipseLink NoSQL} & \textbf{DataNucleus}\\\midrule
  Goal & Complement JPA with NoSQL, key-value stores & Integrates in the father project main goal of providing a complete persistence solution & Being a standards compliant and efficient JPA and JDO platform\\
  NoSQL and Datastores supported & Infinispan, EHCache, MongoDB & MongoDB, Oracle NoSQL, Oracle AQ, JMS, XML files & Google Big Table, MongoDB, Cassandra, Excel, OOXML, ODF, XML, HBase, AppEngine/DataStore, Neo4j, JSON, Amazon S3, GoogleStorage, LDAP, NeoDatis, db4o\\
  Operations supported & Object Oriented queries (JP-QL), CRUD of entities, Polymorphic entities, Embeddable objects, Basic types (partial), Unidirectional and Bidirectional relationships (partial), Collections, Hibernate Search queries, JPA and Hibernate ORM API & Object Oriented Queries, Polymorphic entities, Basic types, Unidirectional relationships, Collections, JPA (partial), Complex hierarchical, Indexed hierarchical data, Mapped hierarchical data, CRUD operations, Embedded objects and collections, Inheritance, Subset of JP-QL and Criteria API, Denormalization & CRUD operations, Embedded objects and collections, Inheritance, Relationships (Unidirectional and Bidirectional), Queries for JP-QL, JDOQL and SQL (partial),  Basic types, Joins.\\
  No support for & Denormalization, Complex joins and aggregations & Joins & Aggregations? (not specified in documentation)\\
  Future & High performance sequence generator, parallel key fetching, support for Map/Reduce, more NoSQL classes, better mixing of NoSQL and RDBMS & ? & JPA2.1 full feature list, Official support for Cassandra, Considering a plugin for REDIS\\
  Commercial support & Red Hat & Oracle (via TopLink) & Supported by DataNucleus team\\
  Documentation & Scattered, inactive forums, official documentation lacking & Bureaucratic forums, information is complete and gathered mainly in the official website & Active forums, acceptable official documentation, but the big advantage comes from user support in form of blogs and posts scattered around the Internet\\\bottomrule
\end{tabularx}}}
\end{document}