4

In a Nutshell:

How can you provide word wrap in left-aligned table cell when you are using the package bookstabs?

My Problem in Detail:

I am writing a scientific paper to be (hopefully) published in an ACM magazine. The document class includes the package booktabs: https://ctan.org/pkg/booktabs and the style guide does not allow to use other table styles.

Before that, in a draft document, I had this table, using the package tabularray:

\documentclass{article}
\usepackage{tabularray} % <-- line 2
\begin{document}
\begin{table}
    \begin{tblr}{X[l]X[l]rrr} % <-- line 5
        \hline
        Authors/Editors & Title & Pages & Year & Ref. \\
        \hline
        David Salomon and Giovanni Motta & 
        Handbook of Data Compression & 1,370 & 2010 & [1] \\
        Colt McAnlis and Aleks Haecky & 
        Understanding Compression: Data Compression 
        for Modern Developers & 241 & 2016 & [2] \\
        \hline
    \end{tblr} % <-- line 15
\end{table}
\end{document}

It produces this table:

table made with package tabularray
fig 1: table made with package tabularray

Then I tried it in the ACM template that uses booktabs and I failed. Here is an example code using this package. Only lines 2, 5 and 15 are different (marked with comments):

\documentclass{article}
\usepackage{booktabs} % <-- line 2
\begin{document}
\begin{table}
    \begin{tabular}{llrrr} % <-- line 5
        \hline
        Authors/Editors & Title & Pages & Year & Ref. \\
        \hline
        David Salomon and Giovanni Motta & 
        Handbook of Data Compression & 1,370 & 2010 & [1] \\
        Colt McAnlis and Aleks Haecky & 
        Understanding Compression: Data Compression 
        for Modern Developers & 241 & 2016 & [2] \\
        \hline
    \end{tabular} % <-- line 15
\end{table}
\end{document}

Here is the result:

table made with package booktabs
fig 2: table made with package booktabs and llrrr

There is no word wrap and the table is too wide to fit on the page. It is cut off at the right side.

With tabularray I can specify word wrap in a left-aligned table cell with the parameter X[l] (in line 5). When I try this with booktabs I get an error message ("LaTeX Error: Illegal character in array arg") and table cells without word wrap and centered text:

second version using booktabs
fig 3: table made with package booktabs and X[l]X[l]rrr

When I replace X[l] with l I get what you can see in figure 2 (without an error).

On https://ctan.org/pkg/booktabs is a documentation of the package booktabs which is 18 pages long, but it nowhere mentions column widths, word wrap or similar topics. In fact all 18 pages deal only with one topic: the thickness of horizontal lines, which is not very helpful.

Here is my question:

Please can you tell me, what I must do to create a table with word wraps as shown in my first example, but for the package bookstabs?

2
  • 1
    Your problem has nothing to do with booktabs: you used no paragraph-like column specifiers. Unrelated: with booktabs, you should not use \hline but one of \toprule, \bottomruleor \midrule in order to have some vertical padding.
    – Bernard
    Apr 14, 2022 at 17:14
  • @Bernard: Yes, you are right. I used \toprule etc. but I found out that this had no influence on my problem and I wanted to keep my examples as simple as possible. Apr 15, 2022 at 14:14

4 Answers 4

6

The problem is not with you using booktabs but that you have not specified how you want the columns to be set. Use p{width} for a column with multiple text lines.

% booktabsprob.tex  SE 640799

\documentclass{article}
\usepackage{booktabs} % <-- line 2
\begin{document}
\begin{table}
%    \begin{tabular}{llrrr} % <-- line 5
    \begin{tabular}{lp{4cm}rrr} % <-- line 5
        \hline
        Authors/Editors & Title & Pages & Year & Ref. \\
        \hline
        David Salomon and Giovanni Motta & 
        Handbook of Data Compression & 1,370 & 2010 & [1] \\
        Colt McAnlis and Aleks Haecky & 
        Understanding Compression: Data Compression 
        for Modern Developers & 241 & 2016 & [2] \\
        \hline
    \end{tabular} % <-- line 15
\end{table}
\end{document}

enter image description here

4

With tabularray and its library booktabs (which load booktabs package too):

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs} % <---

\begin{document}
\begin{table}
    \begin{tblr}{colsep = 4pt,
                 colspec = {@{} X[0.8,l]X[1.2,l]rrr @{}}
                 }
    \toprule
Authors/Editors & Title & Pages & Year & Ref.   \\
    \midrule
David Salomon and Giovanni Motta 
                & Handbook of Data Compression 
                        & 1,370 & 2010 & [1]    \\
Colt McAnlis and Aleks Haecky 
                & Understanding Compression: Data Compression
        for Modern Developers 
                        &   241 & 2016 & [2]    \\
    \bottomrule
    \end{tblr}
\end{table}
\end{document}

enter image description here

3

Here is a possible solution, using tabularx:

    \documentclass{article}
    \usepackage{tabularx}
    \usepackage{booktabs} % <-- line 2

    \begin{document}

    \begin{table}
        \begin{tabularx}{\textwidth}{XXrrr} % <-- line 5
            \toprule
            Authors/Editors & Title & Pages & Year & Ref. \\
            \midrule
            David Salomon and Giovanni Motta &
            Handbook of Data Compression & 1,370 & 2010 & [1] \\ \addlinespace
            Colt McAnlis and Aleks Haecky &
            Understanding Compression: Data Compression
            for Modern Developers & 241 & 2016 & [2] \\
            \bottomrule
        \end{tabularx} % <-- line 15
    \end{table}

    \end{document} 

enter image description here

3

You asked,

How can you provide [line wrapping] in left-aligned table cell when you are using the package booktabs?

Enabling automatic line wrapping is completely distinct from any features provided by the booktabs package. As you've discovered, the l column type does not provide automatic line breaking. I suggest you load the tabularx package, use its eponymous tabularx environment instead of tabular, set the overall width of the tabularx environment to \textwidth, and employ the package's X column type for the first two columns.

Relative to the tabularx-based approach pursued by @Bernard in this answer, the approach pursued below (a) gets rid of the whitespace padding at the left- and right hand edges of the table, (b) doesn't fully justify the cell contents in columns 1 and 2, (c) center-sets rather than right-aligns the contents of columns 3 thru 5, and (d) employs the S column type of the siunitx package to align the numbers in column 3 on their (implicit) decimal markers.

enter image description here

\documentclass{article}
\usepackage{booktabs} 
\usepackage{tabularx,ragged2e}
\newcolumntype{L}{>{\RaggedRight}X}
\usepackage{siunitx} % for S column type
\begin{document}

\begin{table}
    \begin{tabularx}{\textwidth}{@{} 
          LL 
          S[table-format=4.0,group-separator={,},group-minimum-digits=4]
          cc @{}} 
        \toprule
        Authors/Editors & Title & {Pages} & Year & Ref. \\
        \midrule
        David Salomon and Giovanni Motta & 
        Handbook of Data Compression & 1370 & 2010 & [1] \\
        \addlinespace
        Colt McAnlis and Aleks Haecky & 
        Understanding Compression: Data Compression 
        for Modern Developers & 241 & 2016 & [2] \\
        \bottomrule
    \end{tabularx} 
\end{table}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .