[Tex/LaTex] How to make really wide tables narrower

tableswidth

Is there a way to fix extremely wide tables?

I have the following table:

\begin{tabular}{lllll}
            \toprule
                &  Manual Tests & Randoop 2.5 Minutes With GUI Code &  Randoop 2.5 Minutes Without GUI Code &  Randoop 5 Minutes Without GUI Code \\ \midrule
    \% Covered Instructions &  \textbf{82.40\%} &  21.25\% &  21.30\% &  \textbf{22.10\%} \\ \bottomrule
    \end{tabular}

And it looks like so:

look

Is there a way to make the cells slimmer? If relevant I am using scrreprt.

Best Answer

You can use p{width} column type for a fixed column, text will wrap automatically. For example:

\begin{tabular}{*{5}{p{3cm}}}
\toprule
  &  Manual Tests & Randoop 2.5 Minutes With GUI Code &  Randoop 2.5 Minutes Without GUI Code &  Randoop 5 Minutes Without GUI Code \\ \midrule
\% Covered Instructions &  \textbf{82.40\%} &  21.25\% &  21.30\% &  \textbf{22.10\%} \\ \bottomrule
\end{tabular}

You can also use \shortstack and use \\ for manual line break. For example:

\begin{tabular}{ccccc}
\toprule
  &  Manual Tests & \shortstack{Randoop 2.5 Minutes\\ With GUI Code} &  \shortstack{Randoop 2.5 Minutes\\ Without GUI Code} &  \shortstack{Randoop 5 Minutes\\ Without GUI Code} \\ \midrule
\% Covered Instructions &  \textbf{82.40\%} &  21.25\% &  21.30\% &  \textbf{22.10\%} \\ \bottomrule
\end{tabular}

\shortstack here can be replaced by a small tabular envronment, for better vertical alignment:

\newcommand{\minitab}[2][c]{\begin{tabular}{#1}#2\end{tabular}}

\begin{tabular}{ccccc}
\toprule
  &  Manual Tests & \minitab{Randoop 2.5 Minutes\\ With GUI Code} &  \minitab{Randoop 2.5 Minutes\\ Without GUI Code} &  \minitab{Randoop 5 Minutes\\ Without GUI Code} \\ \midrule
\% Covered Instructions &  \textbf{82.40\%} &  21.25\% &  21.30\% &  \textbf{22.10\%} \\ \bottomrule
\end{tabular}

Furthermore, you can use makecell package for more flexible commands. \shortstack or \minitab can be replaced by \makecell, \thead etc.

% \usepackage{makecell}
\begin{tabular}{ccccc}
\toprule
  &  Manual Tests & \thead{Randoop 2.5 Minutes\\ With GUI Code} &  \thead{Randoop 2.5 Minutes\\ Without GUI Code} &  \thead{Randoop 5 Minutes\\ Without GUI Code} \\ \midrule
\% Covered Instructions &  \textbf{82.40\%} &  21.25\% &  21.30\% &  \textbf{22.10\%} \\ \bottomrule
\end{tabular}