7

I have a table in a document in which I am not sure what the final font size is going to be. The font size will probably be 10, 11 or 12. The tables are fine at font size 10, but at 11 or 12 one or more of them is too wide.

I would like to have code in the table that will downsize the table slightly only if it is too wide, i.e.: only if it is wider than the column width. If the table already fits into the column then it should be left alone.

Is it possible to do this without writing a lot of complicated code. An MWE that illustrates this issue is given below. When the font size becomes 12, I want the table to adjust downwards to the column width:

\documentclass[10pt]{article}
\usepackage{graphics}

\usepackage[a4paper, left=50mm, right=50mm, top=50mm, bottom=50mm]{geometry}

\usepackage{caption}
\captionsetup[table]{skip=2pt}

\begin{document}

\begin{table}[H]
    \centering
    \caption{Elapsed Time (Seconds)}
        %\resizebox{\columnwidth}{!}{%
    \begin{tabular}{@{\vrule height 10.5pt depth4pt  width0pt}llllllll}
        & \multicolumn7c{Problem Size} \\
        \cline{2-8}
        Method      & 2   & 10 & 90  & 250 & 900     & 2000  & 5000\\
        \hline
        Connoisseur & 0.5 & 3  & 21  & 49  & 120     & 323     & 600\\ 
        Cowboy      & 2   & 7  & 56  & 122 & 345     & 678     & 1545\\ 
        Explorer    & 0.5 & 2  & 19  & 45  & Crashed & Crashed & Crashed\\ 
        Handyman    & 6   & 31 & 258 & 600 & Crashed & Crashed & Crashed\\ 
    \end{tabular} \\
    \label{tab:elapsed}
\end{table}

\end{document}

As you may note, I tried \resizebox, but that increases the table width when it is narrower than the column width.

Thank you.

3

\resizebox makes the dimensions of the box to be scaled available as \width, \height, and \depth. This can be used to only scale the box, if it is wider than the column width.

\documentclass[10pt]{article}
\usepackage{graphics}

\usepackage[a4paper, left=50mm, right=50mm, top=50mm, bottom=50mm]{geometry}

\usepackage{caption}
\captionsetup[table]{skip=2pt}

\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}

\begin{table}
    \centering
    \caption{Elapsed Time (Seconds)}
    \resizebox{%
      \ifdim\width>\columnwidth
        \columnwidth
      \else
        \width
      \fi
    }{!}{%
      \begin{tabular}{
        @{
          \vrule height 10.5pt depth4pt  width0pt\relax
          \kern.5\tabcolsep
        }
        l
        S[table-format=1.1]
        S[table-format=2]
        S[table-format=3]
        S[table-format=3]
        S[table-format=3]
        S[table-format=4]
        S[table-format=4]
      }
        & \multicolumn7c{Problem Size} \\
        \cmidrule{2-8}
        Method      & 2   & 10 & 90  & 250 & 900     & 2000  & 5000\\
        \midrule
        Connoisseur & 0.5 & 3  & 21  & 49  & 120     & 323     & 600\\
        Cowboy      & 2   & 7  & 56  & 122 & 345     & 678     & 1545\\
        Explorer    & 0.5 & 2  & 19  & 45  & {Crashed} & {Crashed} & {Crashed}\\
        Handyman    & 6   & 31 & 258 & 600 & {Crashed} & {Crashed} & {Crashed}\\
      \end{tabular}%
    }
    \label{tab:elapsed}
\end{table}

\end{document}

Result

  • The example uses the horizontal lines (with vertical spacing) of package booktabs.

  • The numbers in the table are aligned at the decimal marker (package siunitx).

  • \\ after the table without following another line is removed.

3

I would design table for 12 pt so that it fitt text width ... than they will looks ok also for smaller fonts to :-)

MWE:

\documentclass[12pt]{article}
\usepackage{graphics}
\usepackage[showframe,
            a4paper, margin=50mm]{geometry}% <-- changed
\usepackage{caption}
\captionsetup[table]{skip=2pt}

\usepackage{siunitx}
\begin{document}

\begin{table}[htb]
    \centering
    \setlength\tabcolsep{0pt}
\caption{Elapsed Time (Seconds)}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}l
                              S[table-format=1.1]
                              S[table-format=2.0]
                         *{3}{S[table-format=3.0]}
                         *{2}{S[table-format=4.0]}
                             }
    &   \multicolumn{7}{c}{Problem Size} \\
        \cline{2-8}
Method      & 2   & 10 & 90  & 250 & 900     & 2000  & 5000\\
\hline
Connoisseur & 0.5 & 3  & 21  & 49  & 120     & 323     & 600\\
Cowboy      & 2   & 7  & 56  & 122 & 345     & 678     & 1545\\
Explorer    & 0.5 & 2  & 19  & 45  & {Crashed} & {Crashed} & {Crashed}\\
Handyman    & 6   & 31 & 258 & 600 & {Crashed} & {Crashed} & {Crashed}\\
    \hline
\end{tabular*}
    \label{tab:elapsed}
\end{table}

\end{document}

Above MWE gives:

enter image description here

In case, that you reduce font size to 10pt, above MWE will gives:

enter image description here

In table design I use S column type from siunitx package.

1

Warning

First off, let me just warn you of a very unelegant output. Resizing of text will look very inconsistent. There is a reason that most fonts are not meant to be displayed in say 9.328-size also, as it might change the look if it when printed or displayed on a screen.

Explenation

We can use a \savebox to check the size of something before we print it. I have created a macro which compares a given length (e.g. \textwidth) to some content (e.g. tabular). The macro saves the content into a box, stores the width of it in a length for later use, and compares this width against the given length in the argument. From there we have to options. If the width of the content is lower than the given length, we don't need to resize it, just display it, with \usebox, which retrieves the content and displays it. On the other hand, if the width is wider than the given length, we introduce rizing of the box, using \resizebox which you are already familiar with.

Output

enter image description here

Code

\documentclass[11pt]{article}
\usepackage{graphicx}
%\usepackage{showframe} % Puts a frame to see textarea and such. Enable only during testing
\usepackage{ifthen}

\usepackage[a4paper, left=50mm, right=50mm, top=50mm, bottom=50mm]{geometry}

\usepackage{caption}
\captionsetup[table]{skip=2pt}

\newsavebox{\jamBox}
\newlength{\jamWidth}
\newcommand{\jamIfToBig}[2]{%
    % #1: Size to check against, like \textwidth
    % #2: The content, like a tabular
    \savebox{\jamBox}{#2}%
    \settowidth{\jamWidth}{\usebox{\jamBox}}%
    %\the\jamWidth -- \the#1
    \ifthenelse{\jamWidth < #1}%
        {\usebox{\jamBox}}%
        {\resizebox{#1}{!}{\usebox{\jamBox}}%
    }%
}

\begin{document}

\begin{table}[hbt]
    \centering
    \caption{Elapsed Time (Seconds)}
        \jamIfToBig{\textwidth}{%
    \begin{tabular}{@{\vrule height 10.5pt depth4pt  width0pt}llllllll}
        & \multicolumn7c{Problem Size} \\
        \cline{2-8}
        Method      & 2   & 10 & 90  & 250 & 900     & 2000  & 5000\\
        \hline
        Connoisseur & 0.5 & 3  & 21  & 49  & 120     & 323     & 600\\ 
        Cowboy      & 2   & 7  & 56  & 122 & 345     & 678     & 1545\\ 
        Explorer    & 0.5 & 2  & 19  & 45  & Crashed & Crashed & Crashed\\ 
        Handyman    & 6   & 31 & 258 & 600 & Crashed & Crashed & Crashed\\ 
    \end{tabular}%
    }
    \label{tab:elapsed}
\end{table}

\begin{table}[hbt]
    \centering
    \huge
    \caption{Wider table}
        \jamIfToBig{\textwidth}{%
    \begin{tabular}{@{\vrule height 10.5pt depth4pt  width0pt}llllllll}
        & \multicolumn7c{Problem Size} \\
        \cline{2-8}
        Method      & 2   & 10 & 90  & 250 & 900     & 2000  & 5000\\
        \hline
        Connoisseur & 0.5 & 3  & 21  & 49  & 120     & 323     & 600\\ 
        Cowboy      & 2   & 7  & 56  & 122 & 345     & 678     & 1545\\ 
        Explorer    & 0.5 & 2  & 19  & 45  & Crashed & Crashed & Crashed\\ 
        Handyman    & 6   & 31 & 258 & 600 & Crashed & Crashed & Crashed\\ 
    \end{tabular}%
    }
    \label{tab:elapsed}
\end{table}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.