BOOKTABS 表中的表格儲存格中的自動換行

BOOKTABS 表中的表格儲存格中的自動換行

簡而言之:

當您使用該套件時,如何在左對齊的表格儲存格中提供自動換行bookstabs

我的問題詳細資訊:

我正在寫一篇科學論文(希望)發表在 ACM 雜誌上。文檔類別包括包booktabshttps://ctan.org/pkg/booktabs且樣式指南不允許使用其他表格樣式。

在此之前,在草稿文件中,我使用了以下套件的表格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}

它產生這個表:

使用 tabularray 套件製作的表格
圖1:用包包製作的表格tabularray

然後我在使用的ACM模板中嘗試了一下booktabs,但失敗了。這是使用此套件的範例程式碼。僅第 2、5 和 15 行不同(已註):

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

結果如下:

用套件 booktabs 製作的表格
圖2:用包包booktabsllrrr

沒有自動換行,表格太寬,無法在頁面上顯示。它在右側被切斷。

tabularray可以使用參數X[l](第 5 行)在左對齊表格儲存格中指定自動換行。當我嘗試這樣做時,booktabs我收到一條錯誤訊息(“LaTeX 錯誤:數組 arg 中的非法字元”)並且表格單元格沒有自動換行和居中文字:

使用 booktabs 的第二個版本
圖 3:用包包booktabsX[l]X[l]rrr

當我替換X[l]為時,l我得到了您在圖 2 中看到的內容(沒有錯誤)。

https://ctan.org/pkg/booktabs是該套件的文檔booktabs,長達 18 頁,但其中沒有提及列寬、自動換行或類似主題。事實上,所有 18 頁都只涉及一個主題:水平線的粗細,這沒有太大幫助。

這是我的問題:

請您告訴我,我必須做什麼才能創建一個帶有自動換行的表格,如第一個範例所示,但對於套件bookstabs

答案1

問題不在於您使用,booktabs而是您沒有指定如何設定列。用於p{width}具有多個文字行的列。

% 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}

在此輸入影像描述

答案2

及其tabularraybooktabs(也載入booktabs套件):

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

在此輸入影像描述

答案3

這是一個可能的解決方案,使用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} 

在此輸入影像描述

答案4

你問,

當您使用該套件時,如何在左對齊的表格儲存格中提供[換行] booktabs

啟用自動換行與該套件提供的任何功能完全不同booktabs。正如您所發現的,l列類型不提供自動換行。我建議您加載該tabularx包,使用其同名tabularx環境而不是tabular,將環境的整體寬度設置tabularx為,並為前兩列\textwidth使用包的列類型。X

相對於tabularx@Bernard 所追求的 -based 方法這個答案,以下採用的方法 (a) 消除了表格左右邊緣的空白填充,(b) 沒有完全證明第 1 列和第 2 列中的單元格內容合理,(c) 中心集而是然後右對齊第3 列到第5 列的內容,並且(d) 使用套件S的列類型siunitx將第 3 列中的數字與其(隱式)小數標記對齊。

在此輸入影像描述

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

相關內容