2

I am using booktabs library to make my table in latex. However, I noticed that due to the size of the table it goes out of the right margin. I tried to follow several recommendations from several posts but none did work for me. My code is the following:

\begin{table}[!htbp]
\centering
\caption{Classification, FID and IS performance for all the experimental protocol scenarios. }
\label{er:tbl2}
{\small
\begin{tabular}{*9c}
\toprule
 Case &  \multicolumn{4}{c}{dv1} & \multicolumn{4}{c}{dv2}\\
 & cla& score1& score2& score3& cla& score1& score2& score3\\
\midrule
\textbf{Claas1}   &  {49.34$\%$} &   &  &    & {44.73$\%$}  &  &   &  \\
\textbf{Claas2}   & \textbf{52.52$\%$} & \textbf{59.44} & \textbf{2.16} & 
         \textbf{2.16} &  \textbf{47.11$\%$} & \textbf{49.77} & \textbf{2.21} 
         & \textbf{2.21}\\
\textbf{Claas3}   &  49.92$\%$ & 60.13 & 2.01 & 2.01 & 46.23$\%$ & 50.33 & 
          2.05 & 2.05\\
\textbf{Claas4}  &  50.29$\%$ & 60.10 & 2.00 & 2.00  & 46.55$\%$ & 49.95 & 
         2.01 & 2.01\\
\bottomrule
\end{tabular}
}
\end{table}

How can I reduce the size or stretch the table within the range of my template's page?

  • Could you please add the documentclass you are currently using? – leandriis Feb 28 '20 at 13:08
  • Probably \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}*9c@{}} might help – leandriis Feb 28 '20 at 13:10
  • for start insert \setlength\tabcolsep{5pt} before \begin{tabular}, however your code for table can be further improved ... – Zarko Feb 28 '20 at 13:12
  • @Zarko your command push a bit more within the range, but still. – konstantin Feb 28 '20 at 13:14
  • 1
    @konstantin, it may be with your document class, which is unknown to us. With standard article this is not a case. Please extend your document to complete small document beginning with \documentclass{...} and ending with \end{document}, which reproduce your problem. – Zarko Feb 28 '20 at 13:45
  • 1
    @konstantin, any news? you got (so far) three answers, which solve your problem, but not upvote any of them nor accept ones which best fit your expectation ... – Zarko Mar 29 '20 at 23:21

3 Answers3

2

The code in example 2 ensure that the table is within margins. However, in example 1, I suggest some (in my opinion) improvements to your table. Right aligned figure columns, the per cent sign in the heading and increased space between table and caption:

Example 1

enter image description here

\documentclass{article}
\usepackage{array, booktabs, caption}
\usepackage[skip=1ex]{caption}

\setlength{\tabcolsep}{0pt}

\begin{document}

\begin{table}[!htbp]
\centering
\caption{Classification, FID and IS performance for all the experimental protocol scenarios. }
\label{er:tbl2}
{\small
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}l*{8}{r}}
\toprule
 Case &  \multicolumn{4}{c}{dv1} & \multicolumn{4}{c}{dv2}\\
 \cmidrule{2-5}\cmidrule{6-9}
 & cla (\%)& score1& score2& score3& cla (\%)& score1& score2& score3\\
\midrule
\textbf{Claas1}   &  {49.34} &   &  &    & {44.73}  &  &   &  \\
\textbf{Claas2}   & \textbf{52.52} & \textbf{59.44} & \textbf{2.16} & 
         \textbf{2.16} &  \textbf{47.11} & \textbf{49.77} & \textbf{2.21} 
         & \textbf{2.21}\\
\textbf{Claas3}   &  49.92 & 60.13 & 2.01 & 2.01 & 46.23 & 50.33 & 
          2.05 & 2.05\\
\textbf{Claas4}  &  50.29 & 60.10 & 2.00 & 2.00  & 46.55 & 49.95 & 
         2.01 & 2.01\\
\bottomrule
\end{tabular*}
}
\end{table}

\end{document}

Exemple 2

enter image description here

\documentclass{article}
\usepackage{array, booktabs, caption}

\setlength{\tabcolsep}{0pt}

\begin{document}

\begin{table}[!htbp]
\centering
\caption{Classification, FID and IS performance for all the experimental protocol scenarios. }
\label{er:tbl2}
{\small
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}*{9}{c}}
\toprule
 Case &  \multicolumn{4}{c}{dv1} & \multicolumn{4}{c}{dv2}\\
 & cla& score1& score2& score3& cla& score1& score2& score3\\
\midrule
\textbf{Claas1}   &  {49.34$\%$} &   &  &    & {44.73$\%$}  &  &   &  \\
\textbf{Claas2}   & \textbf{52.52$\%$} & \textbf{59.44} & \textbf{2.16} & 
         \textbf{2.16} &  \textbf{47.11$\%$} & \textbf{49.77} & \textbf{2.21} 
         & \textbf{2.21}\\
\textbf{Claas3}   &  49.92$\%$ & 60.13 & 2.01 & 2.01 & 46.23$\%$ & 50.33 & 
          2.05 & 2.05\\
\textbf{Claas4}  &  50.29$\%$ & 60.10 & 2.00 & 2.00  & 46.55$\%$ & 49.95 & 
         2.01 & 2.01\\
\bottomrule
\end{tabular*}
}
\end{table}

\end{document}
Sveinung
  • 20,355
1

As you have many columns, the simplest is here to reduce the value of \tabcolsep (6pt by default). I took the opportunity to improve the layout of the table, using three (trimmed) \cmidrules, \multirow for the first cell of the first column and added some vertical padding to the cells and caption. Also a supplementary empty column to separate the two groups of columns:

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs,array, caption, multirow, makecell}%
\captionsetup{ justification=centering, skip=4pt}
\usepackage{showframe} 
\renewcommand{\ShowFrameLinethickness}{0.3pt}

\begin{document}

\begin{table}[!htbp]
\centering
\setlength{\tabcolsep}{4pt}
\setcellgapes{3pt}\makegapedcells
\let\cmidrulewidth\lightrulewidth
\caption{Classification, FID and IS performance for all the experimental protocol scenarios. }
\label{er:tbl2}
\begin{tabular}{>{\bfseries}c*9c}
\toprule
\multirowcell{2}[0pt][l]{Case} & \multicolumn{4}{c}{dv1} & & \multicolumn{4}{c}{dv2}\\
 & cla& score1& score2& score3& & cla& score1& score2& score3\\
\cmidrule(r){1-1}\cmidrule(lr){2-5}\cmidrule(l){7-10}
Claas1 & 49.34$\%$ & & & & & {44.73$\%$} & & & \\
Claas2 & \textbf{52.52$\%$} & \textbf{59.44} & \textbf{2.16} &
         \textbf{2.16} & & \textbf{47.11$\%$} & \textbf{49.77} & \textbf{2.21}
         & \textbf{2.21}\\
Claas3 & 49.92$\%$ & 60.13 & 2.01 & 2.01 & & 46.23$\%$ & 50.33 &
          2.05 & 2.05\\
Claas4 & 50.29$\%$ & 60.10 & 2.00 & 2.00 & & 46.55$\%$ & 49.95 &
         2.01 & 2.01\\
\bottomrule
\end{tabular}
\end{table}

\end{document}

enter image description here

Bernard
  • 271,350
1

For completnes: with use siunitx package for S columns, etoolbox and xparse for determining numbers in the boldface shape and shortness for \multicolumn cells:

Edit: solution now consider handling of the \bfseries in the new LaTeX (see answer of Ulrike Fischer).

\documentclass{article}
\usepackage[skip=1ex, labelfont=bf]{caption}
\usepackage{booktabs, multirow}

\usepackage{siunitx}
\usepackage{etoolbox}
\newrobustcmd\B{\DeclareFontSeriesDefault[rm]{bf}{b}\bfseries}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
    {\multicolumn{#1}{c}{#2}}

\begin{document}
    \begin{table}[!htbp]
\centering
\caption{Classification, FID and IS performance for all the experimental protocol scenarios. }
\label{er:tbl2}
    \sisetup{detect-weight, 
             mode=text,
             table-format=2.2} 
\setlength\tabcolsep{0pt}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}
                      >{\bfseries}l
                                  S[table-space-text-post=\,\%]<{\,\%}
                                  S
                             *{2}{S[table-format=1.2]}
                             *{2}{S}
                                  S[table-format=1.2]
                                  S[table-format=1.2,
                                    table-space-text-post=\,\%]<{\,\%}
                             }
    \toprule
\multirow{2.4}{*}{Case}
        &   \mcc[4]{dv1}  
        &   \mcc[4]{dv2}                                \cr
        \cmidrule(l){2-5}\cmidrule(l){6-9}
        & \mcc{cla} & {score 1} & {score 2} & {score 3} 
        & {cla}     & {score 1} & {score 2} & \mcc{score 3}  \cr
   \midrule
Claas 1 &   49.34   &           &           &   
        &   44.73   &           &           &   \mcc{}  \cr
Claas 2 &\B 52.52   &\B 59.44   &\B 2.16    &\B 2.16
        &\B 47.11   &\B 49.77   &\B 2.21    &\B 2.21    \cr
Claas 3 &   49.92   &   60.13   &   2.01    &   2.01 
        &   46.23   &   50.33   &   2.05    &   2.05    \cr
Claas 4 &   50.29   &   60.10   &   2.00    &   2.00  
        &   46.55   &   49.95   &   2.01    &   2.01    \cr
    \bottomrule
\end{tabular*}
\end{table}
\end{document}

enter image description here

Zarko
  • 296,517