0

I have this output:

enter image description here

Problem

Instead of hardcoding the resize parameter '0.8' in \resizebox{0.8\columnwidth}{!}{[...]} - I would like to make some sort of automatic resize function.

I have these two requirements:

  • I want the table to fill as much as possible/the rest of the page (in height/length of the paper)
  • But only to the extend, that the columnwidt of the table does not exceed the textwidth

Any idea of how to write this function in Latex? Or links to sources which may help me further with this task.

Tex Code

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphics}
\usepackage{float}


\begin{document}

\section{Text}
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 


\section{Table}
\begin{table}[H]
\resizebox{0.8\columnwidth}{!}{
\begin{tabular}{lllll}
\hline
\multicolumn{1}{|l|}{1}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{2}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{3}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{4}  & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{5}  & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{6}  & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                         &                                &                                &                                &                                \\ \hline
\multicolumn{1}{|l|}{7}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{8}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{9}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{10} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{11} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\multicolumn{1}{|l|}{12} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
\end{tabular}
}
\end{table}

\end{document}
5
  • 2
    Have a look at tabularx.
    – NBur
    Commented Feb 5, 2021 at 9:10
  • 3
    Why do you use all these \multicolumn: only for the rules?
    – NBur
    Commented Feb 5, 2021 at 9:10
  • 6
    applying resizebox to tables should be avoided in almost all cases it is only ever a last resort and even then avoid it, certainly never want to apply it automatically. It produces inconsistent font sizes and rule widths. Commented Feb 5, 2021 at 9:13
  • To NBur, I just generated the table in tablesgenerator.com, it was faster than writing it myself. I gueess it's an leftover from last time I used tablegenerator Commented Feb 5, 2021 at 9:18
  • To David Charlisle, thanks for the warning. I didn't came across this problem yet and have tried with different parameters. Do you have any other solution to resize the table? Commented Feb 5, 2021 at 9:20

3 Answers 3

2

If you want your table to fit a given width, tabularx (thanks @David Carlisle!) is the way to go.

To simplify, I set up a new column type.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphics}
\usepackage{float}

\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X} % from array package

\usepackage{lipsum}

\begin{document}
    
    \section{Text}
    \lipsum[2]
    
    \section{Table}
    \begin{table}[H]
        \resizebox{0.8\columnwidth}{!}{
            \begin{tabular}{lllll}
                \hline
                \multicolumn{1}{|l|}{1}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{2}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{3}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{4}  & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{5}  & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{6}  & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                                         &                                &                                &                                &                                \\ \hline
                \multicolumn{1}{|l|}{7}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{8}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{9}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{10} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{11} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{12} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
            \end{tabular}
        }
    \end{table}
    \begin{table}[H]
            \begin{tabularx}{\columnwidth}{|l*{4}{|C}|}
                \hline
                1  & Something & Something & Something & Something \\ \hline
                2  & Something & Something & Something & Something \\ \hline
                3  & Something & Something & Something & Something \\ \hline
                4  & Something & Something & Something & Something \\ \hline
                5  & Something & Something & Something & Something \\ \hline
                6  & Something & Something & Something & Something \\ \hline
                   &           &           &           &           \\ \hline
                7  & Something & Something & Something & Something \\ \hline
                8  & Something & Something & Something & Something \\ \hline
                9  & Something & Something & Something & Something \\ \hline
                10 & Something & Something & Something & Something \\ \hline
                11 & Something & Something & Something & Something \\ \hline
                12 & Something & Something & Something & Something \\ \hline
            \end{tabularx}
    \end{table}
    
\end{document}

enter image description here

6
  • Well, it's not exactly what I was searching. I would like to adjust it in the height aswell Commented Feb 5, 2021 at 9:37
  • Could you elaborate the {|l*{4}{|C}|} part Commented Feb 5, 2021 at 9:38
  • 1
    What about @David's warning? If you need there is the ltablex package, merging the longtable and tabularx to deal with tabular on multi pages and with the X column type.
    – NBur
    Commented Feb 5, 2021 at 9:46
  • 1
    The | are for vertical rules, *{num}{columntype} says I want num columns with the same columntype
    – NBur
    Commented Feb 5, 2021 at 9:47
  • Thans for the clarification. I'm trying to work around the warning so instead of resizing, then using tabularx as you proposed, but adjusting in the height also by inserting air in the height. (Sorry for the bad explanation, but that is what I'm trying to accompilish, if I succeeds it will sure be more beautifull than my attempt with resize) Commented Feb 5, 2021 at 9:55
1

As supplement to @Nbur answer ...

  • as already mentioned, use of use of the \resizebox lead to inconsistency between fonts size in table an in other text.
  • use of multicolumn{1}{...}{...} are just clutter in code
  • use of [H] as placement specification is dangerous. It can result in unexpected result as undesired empty spaces.
  • it is not clear, what you imagine with vertical stretching of table ... it also lead to change font size
  • do you really want to have an very ugly designed table?

Answer of @Nbur shows the way, how you can write a table. Here is just a cleanup of used code and some improvements in height of table:

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\section{Text}
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla

\section{Table}
    \begin{table}[ht]
    \renewcommand\arraystretch{1.3} % <---
\begin{tabularx}{\linewidth}{|l|C|C|X|X|}
    \hline
1   & Something  & Something  & Something  & Something  \\ \hline
2   & Something  & Something  & Something  & Something  \\ \hline
3   & Something  & Something  & Something  & Something  \\ \hline
4   & Something  & Something  & Something  & Something  \\ \hline
5   & Something  & Something  & Something  & Something  \\ \hline
6   & Something  & Something  & Something  & Something  \\ \hline
    &            &            &            &            \\ \hline
7   & Something  & Something  & Something  & Something  \\ \hline
8   & Something  & Something  & Something  & Something  \\ \hline
9   & Something  & Something  & Something  & Something  \\ \hline
10  & Something  & Something  & Something  & Something  \\ \hline
11  & Something  & Something  & Something  & Something  \\ \hline
12  & Something  & Something  & Something  & Something  \\ \hline
\end{tabularx}
    \end{table}
\end{document}

enter image description here

3
  • 1
    And to correct a bit more the “ugly design”, you can use the booktab package (for instance).
    – NBur
    Commented Feb 5, 2021 at 10:12
  • Of course I don't want an ugly ass table xD The table was just generated in a website, only for this question. Well my problem is a have a lot of pages each concerning different topics and in the end of each of these pages there is an table. Some of the tables are inches too large to go on the same page as the text and other tables are very small so the page looks empty. So I thought I could automatic resize them, such that the filled as much as possible and the big ones get a little smaller to be contained at the page. I didn't thought so much of the impact of the fonts. Commented Feb 5, 2021 at 10:12
  • But I'm aware now, so thank you all for that. And especially thank you for the code to extend in width and height without changing the font. Commented Feb 5, 2021 at 10:15
1

If you really want a resize of the whole table, but with some automation, have a look at the fitbox package: you can specify min and max lengths to adjust the content.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphics}
\usepackage{float}

\usepackage{lipsum}
\usepackage{fitbox}

\begin{document}
    
    \section{Text}
    \lipsum[2]
    
    \section{Table}
    \begin{table}[H]
%       \resizebox{0.8\columnwidth}{!}{
        \fitbox[maxwidth=\columnwidth]{
            \begin{tabular}{lllll}
                \hline
                \multicolumn{1}{|l|}{1}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{2}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{3}  & \multicolumn{1}{c|}{Something} & \multicolumn{1}{c|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{4}  & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{5}  & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
                \multicolumn{1}{|l|}{6}  & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} & \multicolumn{1}{l|}{Something} \\ \hline
            \end{tabular}
        }
    \end{table}
    \begin{table}[H]
        \fitbox[maxwidth=\textwidth]{
            \begin{tabular}{|l|c|}
                \hline
                1  & Something\\% & Something & Something & Something \\ \hline
                2  & Something\\% & Something & Something & Something \\ \hline
                3  & Something\\% & Something & Something & Something \\ \hline
                4  & Something\\ \hline
            \end{tabular}}
    \end{table}

\end{document}

enter image description here

You must log in to answer this question.

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