[Tex/LaTex] Resizing or scaling table only if it is larger than column width

fontsizetableswidth

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.

Best Answer

\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.

Related Question