0

I have a table with long text and found a way to get rid of the issue where one has to do line-breaks manually. Here is the code for a very simple version of this layout:

\usepackage{booktabs} % for toprule, midrule and bottomrule

\begin{table*}[t]
  \centering
  \begin{tabular}{l l}
    \toprule
    Category & Description \\
    \midrule
    \parbox[t]{.10\textwidth}{A} & \parbox[t]{.90\textwidth}{PUT DESCRIPTION HERE. PUT DESCRIPTION HERE. PUT DESCRIPTION HERE. PUT DESCRIPTION HERE. PUT DESCRIPTION HERE.}\\
    \bottomrule
  \end{tabular}
  \caption{A table that adapts}
\end{table*}

I often create tables that have this general format: two columns that expand over the full text width of the page and that divide into two parts (like here, 10-90, but also adapted based on the size of the text in the first column).

Is there a way to turn this into a command (or is there perhap already a command) so that I do not have to write the parbox for every cell?

Also, it would be nice to simplify this so that I only have to declare the size of one cell (10%) to define the remainder (90%) implicitly for all cells.

Any ideas how to do this efficiently?

RalfB
  • 825

2 Answers2

1

The easiest way is to use ordinary p type columns and use \newline instead of \\ to introduce a manual line break in the cell:

\documentclass[]{article}

\usepackage{booktabs} % for toprule, midrule and bottomrule

\begin{document}
\begin{table*}[t]
  \centering
  \begin{tabular}{ p{.1\textwidth} p{\dimexpr.9\textwidth-4\tabcolsep\relax} }
    \toprule
    Category & Description \\
    \midrule
    A & PUT DESCRIPTION HERE. PUT DESCRIPTION HERE.\newline PUT DESCRIPTION HERE. PUT DESCRIPTION HERE. PUT DESCRIPTION HERE.\\
    \bottomrule
  \end{tabular}
  \caption{A table that adapts}
\end{table*}

\end{document}
Skillmon
  • 60,462
0

Two solutions based on tabularx, one with fixed column widths ratio (had to be changed by trial and error with the default margins to 1:7), another with the l column specifier for the first column.

Note that in the first solution with the relative column widths ratio, the sum of the coefficients of \hsize have to be equal to the total number of X columns.

\documentclass[]{article}
\usepackage{tabularx}
\usepackage{booktabs} % for toprule, midrule and bottomrule

\begin{document}

\begin{table*}[! htb]
  \centering
  \begin{tabularx}{\linewidth}{>{\hsize=0.25\hsize}X>{\hsize=1.75\hsize\arraybackslash}X}
    \toprule
    Category & Description \\
    \midrule
    A & PUT DESCRIPTION HERE. PUT DESCRIPTION HERE.\newline PUT DESCRIPTION HERE. PUT DESCRIPTION HERE. PUT DESCRIPTION HERE.\\
    \bottomrule
  \end{tabularx}
  \caption{A table that adapts}
\end{table*}
\vspace{1cm}

\begin{table*}[!htb]
  \centering
  \begin{tabularx}{\linewidth}{l>{\arraybackslash}X}
    \toprule
    Category & Description \\
    \midrule
    A & PUT DESCRIPTION HERE. PUT DESCRIPTION HERE.\newline PUT DESCRIPTION HERE. PUT DESCRIPTION HERE. PUT DESCRIPTION HERE.\\
    \bottomrule
  \end{tabularx}
  \caption{A table that adapts}
\end{table*}

\end{document} 

enter image description here

Bernard
  • 271,350
  • Perhaps you should mention that while setting the relative widths this way one must pay attention to make sure the total used multiples of \hsize match the number of X type columns. It is also advised to set \linewidth=\hsize in the preamble. – Skillmon Sep 24 '18 at 18:31
  • @Skillmon: I've updated my answer for the first point.For your second point, why is it advised? I didn't see it in the documentation, never used it and had no problem. Are there special situations where it could be problematic? – Bernard Sep 24 '18 at 19:26
  • From tabularx documentation, section 4.3: "`{>{\hsize=.5\hsize\linewidth=\hsize}X

    {\hsize=1.5\hsize\linewidth=\hsize}X}" (end of citation). One might use code inside of a column which depends on the\linewidthinstead of the\hsize`.

    – Skillmon Sep 24 '18 at 19:28